【发布时间】:2020-04-26 19:25:23
【问题描述】:
我有一个使用 getline 填充字符串数组的程序。当我运行这个程序时,它从 getline(cin) 输出正确的值,然后程序崩溃,发出错误消息 “进程返回 -1073741819 (0xC0000005)” 我知道这段代码必须处理访问内存位置。我尝试过使用指针,但我尝试过的一切都导致程序以同样的方式崩溃。 您能否就我能做些什么来解决这个问题提供建议?
我在 Windows 10 上为这个程序使用 Code::Blocks
编辑:我将字符串放在一个数组中以测试相似词的数量
int wordCount = 0;
cout << "Enter the number of words in your email: ";
cin >> wordCount; //size of array determined by wordCount, which is retrieved from the user
string testEmail[wordCount] = { }; // declaring and initializing the array
cout << "Enter the email you wish to test: ";
getline(cin >> std::ws, testEmail[wordCount]); // this will populate the array from user input
cout << "The email that will be tested is: ";
for(int i=0; i<wordCount; i++) // for loop used for testing
cout << testEmail[i] << " ";
【问题讨论】:
-
您希望
getline(cin >> std::ws, testEmail[wordCount]);做什么?与your Rubber Duck讨论这个。 -
代码块的使用(或不使用)与问题无关。
-
Getline 会从用户那里得到输入。当它检测到空格时,它会将单词作为字符串存储在数组中。它会为其余的输入做到这一点。我从未使用 getline 填充数组,我不确定使用字符串执行此操作并在空白处创建新条目的属性。
-
getline不是这样工作的。该行丢弃前导空格,然后将一行读入超出范围的testEmail[wordCount]。在不知道自己在做什么并希望它有效的情况下,您无法通过将东西放在一起进行编程。通过good book 之类的方式彻底学习该语言。