c++简单代码中的字符串数组
String array in c++ simple code
这是我用c++编写的带有do-while循环的完整程序,它第一次运行正常,但当它循环时就不能正常运行。
这个程序将名称存储到一个数组中并打印出来。数组大小是50,所以我想在数组中存储50个名称。
任何帮助都将不胜感激。谢谢
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void start(int c);
string NameArray [50];
char response;
int main() {
int count=1;
do {
count = count - 1;
start(count);
cout << "do you want to add another name? ";
cin>> response;
count = count + 2;
cout<< endl;
} while (tolower(response)=='y');
cout<< "program Ends" <<endl;
system ("pause");
return 0;
}
void start(int count) {
cout<< "Enter your First and Last name: ";
getline(cin, NameArray[count]);
cout<< NameArray[count] <<endl;
cout<< endl;
}
cin>> response
将检索输入的一个字符,并将换行符留在输入流中。这将导致您的start()
函数在提示输入您的姓名后立即返回,而getline()
将向NameArray
返回一行空的输入。
您也可以使用getline()
以字符串形式检索响应,然后检查行的第一个char
//...
cout << "do you want to add another name? ";
std::string line;
std::getline(std::cin, line);
response = line[0];
//...
其他人也提到了风格问题,但眼前的问题是:
cout << "do you want to add another name? ";
cin>> response;
count = count + 2;
cout<< endl;
} while (tolower(response)=='y');
在start()
函数中,使用getline()
可以正确地读取完整的一行,但在这里,cin >> response;
会在输入缓冲区中留下一行换行符和其他内容。相反,试试这个:
std::string response;
//...
cout << "do you want to add another name? ";
getline(cin, response); // Changed here
count = count + 2;
cout<< endl;
} while (tolower(response[0])=='y'); // And changed here
首先使用显式循环和break,而不是do while,因为这可以避免缓冲区溢出并整理代码。我还添加了jxh响应中的修复程序。像这样的东西。(未测试)
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
void start(int c);
string NameArray [50];
std::string response;
int main() {
for(int count=0; count<50; ++count) {
start(count);
cout << "do you want to add another name? ";
std::getline(cin,response);
if (tolower(response[0])=='y') break;
}
cout<< "program Ends" <<endl;
system ("pause");
return 0;
}
void start(int count) {
cout<< "Enter your First and Last name: ";
std::getline(cin, NameArray[count]);
cout<< NameArray[count] <<endl;
cout<< endl;
}
但实际上,固定长度数组是一个问题,我会通过使用向量来完全避免。那你就可以完全摆脱伯爵了。代码看起来更像这样:
#include <iostream>
#include <string>
#include <vector>
std::string ask_user_for_name();
std::vector<string> NameArray;
int main() {
do {
NameArray.push_back( ask_user_for_name() );
std::string response;
cout << "do you want to add another name? ";
std::getline(cin,response);
if (tolower(response[0])=='y') break;
} while(true);
cout<< "program Ends" <<endl;
system ("pause");
return 0;
}
std::string ask_user_for_name() {
cout<< "Enter your First and Last name: ";
std::string retval;
getline(cin, retval);
cout<<retval<<std::endl<<std::endl;
return retval;
}
伙计们把这件事搞得太复杂了。你打给cin的第二个电话正在读取一条换行符并立即返回。最快的修复方法是:在getline调用之前添加cin.ignore():
cin.ignore();
getline(cin, NameArray[count]);
相关文章:
- 将C#字符串数组传递给C++
- 如何为 C 型字符串数组编写 getter 和 setter?
- 有没有办法使用 strcpy 将字符串数组复制到另一个字符串或其他数组中?
- 尝试将 c 字符串数组与分隔符连接起来
- 将字符串数组传递给接受常量字符**的函数
- 返回 C++ 中的字符串数组
- 如何从COM模块中的函数返回字符串数组?
- 无法将字符串数组声明为类成员而不是字符 (C++)
- 删除字符串数组
- 如何将字符串数组返回到 java JNI
- 将字符串数组作为函数参数传递
- C++将字符串数组的元素存储到变量中
- 循环访问还包含未使用元素的字符串数组
- 字符串数组上的 sizeof 运算符以 C++ 为单位给出不同的输出
- 乘以字符串/数组和全局数组
- 递归二进制搜索与字符串数组
- 如何初始化一个标准::字符串数组?
- 无法在声明时使用初始值设定项列表初始化常量字符*/字符串数组的向量
- C++字符串数组的动态向量
- 给定一个等长字符串数组