這是我想要的:
聲明一個大小爲15的字符數組來存儲來自用戶的字符(字符串輸入)值。現在執行以下任務:如何將char類型數組傳遞給函數並將第一個char數組複製到第二個圖表數組並使用C++進行比較?
- 將數組傳遞給函數copy()。
- 在上面的函數中定義另一個相同大小的數組。將第一個數組的值複製到第二個數組並顯示在控制檯上。
- 從函數Copy()傳遞兩個數組到函數compare()。如果條件滿足,那麼在該函數中比較兩個數組並且顯示消息「等於」。
這是我的代碼
#include "stdafx.h"
#include <iostream>
using namespace std;
#include <iomanip>
using std::setw;
void mycopy(char array);
int main(){
//Using Loop to input an Array from user
char array[15];
int i;
cout << "Please Enter your 15 characters" << endl;
cout << "**************************************************" << endl;
for (i = 0; i < 15; i++)
{
cin >> array[i];
}
// output each array element's value
cout << "Please Enter your 15 characters" << endl;
cout << "**************************************************" << endl;
cout << "Element" << setw(13) << "Value" << endl;
for (int j = 0; j < 15; j++) {
cout << setw(7) << j << setw(13) << array[j] << endl;
}
mycopy(array[15]);
return 0;
}
void mycopy(char array[15]) {
char array1[15];
strncpy_s(array1, array, 15);
cout << "The output of the copied Array" << endl;
cout << "**************************************************" << endl;
cout << "Element" << setw(13) << "Value" << endl;
for (int j = 0; j < 15; j++) {
cout << setw(7) << j << setw(13) << array1[j] << endl;
}
}
上面的代碼是通過在陣列的功能複製()和第一數組的值複製到第二字符數組但由於傳遞了無效參數,代碼會生成一個異常。正如我已經搜索堆棧溢出,但我沒有找到任何類似的問題,可以解決我的問題。提前致謝。
您忘了在'mycopy()'的原型後面放置一個分號,它位於'main()'之上。 – Mark
在原型中忘記了'*' – Tyger
@Mark感謝但現在它給了我其他錯誤 –