这是我想要的:
声明一个大小为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感谢但现在它给了我其他错误 –