【发布时间】:2014-10-28 22:05:58
【问题描述】:
我正在尝试编写一个程序来存储名称的 char 数组。
这是我的代码
#include <iostream>
#include <string.h>
using namespace std;
char **names;
char *input_name;
int main() {
names = new char*[10];
for(int i=0; i<10; i++){
names = new char[60];
cout << "Input name" << i << ": \n";
cin >> input_name;
strcpy(names[i],input_name);
cout << names[i] << "\n";
}
return 0;
}
首先我收到cannot convert ‘char*’ to ‘char**’ in assignment
names = new char[60]; 错误。
另外,得到invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
strcpy(names[i],input_name); 错误
如果有人可以修改我的代码并帮助我,我将不胜感激
谢谢
【问题讨论】:
-
不应该
names = new char[60];是names[i] = new char[60];吗?其他错误是前一个错误的副作用。 -
如果名称超过 59 个字符,祝您好运。
-
@alvits 谢谢 :),但我仍然在输入方面遇到错误。我的代码在 IDEOne - ideone.com/PAMwgw
-
您的
input_name未初始化。在您的情况下,将其设置为简单的字符串而不是指针会更容易。char input_names[60];. -
如果你不想使用
std::string,为什么还要使用C++?