C++ 如何为c+;中的2D c字符串数组收集用户输入+;?
我正在做一个家庭作业,希望我收集用户输入来填充一个最多20个句子的2D c字符串数组,每个句子最多81个字符。虽然我知道如何初始化数组,但我似乎无法提前停止从用户那里收集输入,并且被迫不断地按enter键来终止程序C++ 如何为c+;中的2D c字符串数组收集用户输入+;?,c++,arrays,char,c-strings,C++,Arrays,Char,C Strings,我正在做一个家庭作业,希望我收集用户输入来填充一个最多20个句子的2D c字符串数组,每个句子最多81个字符。虽然我知道如何初始化数组,但我似乎无法提前停止从用户那里收集输入,并且被迫不断地按enter键来终止程序 int main() { char sentences[20][81]; cout << "Enter up to 20 sentences - when done, Press ENTER: "; input(sentences);
int main()
{
char sentences[20][81];
cout << "Enter up to 20 sentences - when done, Press ENTER: ";
input(sentences);
cout << "\nPress any key to continue...";
_getch();
return 0;
}
void input(char str[20][81]) //This is our function to collect user sentence input
{
for (int i = 0; i < 20; i++)
{
cin.getline(str[i], 81, '\0');
if (str[i] == "\0")
break;
}
}
intmain()
{
字句[20][81];
无法输入新行字符什么是\n不是\0std::getline()
的第三个参数的默认值是\n
。如果我在您的位置,我不会更改它。请记住,下面的检查是if(str[I][0]='\0')
。比较是在if(str[I]=“\0”)中进行的
比较两个不是您想要的指针。(如果由于=
的相应重载,至少有一个参数是std::string
,则存在一个适当的=
。但是,对于char[]
或char*
,必须改用strcmp()
,或者只是比较单个字符。)试图用“\n”代替“\0”不起作用,当我开始收集输入时,我必须在输入函数返回到main之前多次按enter键。。。