退出简单while循环时出现问题
Having problems from exiting a simple while loop
因此,我正试图编写一个程序,该程序从用户那里获取值,并且应该在用户键入q后停止,但由于某些原因,该程序无法运行,即使在我使用q或q进行测试后,该程序也不会停止运行。
vector <string> names;
vector <int> ticPurch;
int numOfRaff;
string nameOfpart;
cout << "Enter the name of the participant, enter q to quit " << endl;
cin >> nameOfpart;
while(nameOfpart != "q" || nameOfpart != "Q"){
names.push_back(nameOfpart);
cout << "Please enter the number of tickets the participant bought " << endl; //Getting input and storing them in vector
cin >> numOfRaff;
ticPurch.push_back(numOfRaff);
cout << endl;
cout << "Enter the name of the participant, enter q to quit " << endl;
cin >> nameOfpart;
}
您使用的是或语句||
。当然,字符串将不等于一个或不等于另一个。
您需要使用和&&
。如果它不是"q"AND它不是"q",则循环。
您还应该检查cin
是否有效。您的整个if
语句可能是:
while(cin && nameOfpart != "q" && nameOfpart != "Q") {
如果输入"q",则为"q"!="Q"是真的,然后它继续,反之亦然。你应该使用
while(nameOfpart != "q" && nameOfpart != "Q")
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 在java中解决这段代码时面临循环中的问题
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 在 for 循环中查找问题时遇到困难
- 嵌套for循环C++的问题(初学者)
- 退出简单while循环时出现问题
- 循环无限运行C++解决骑士之旅问题
- Visual Studio 2017循环自动向量化问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 嵌套While循环不起作用(C++问题)
- 使用 GMP 的 while 循环出现问题
- 遇到此问题时遇到困难:允许用户输入数组的值并使用 for,而循环也输出输入的最大数字
- OpenMP for 循环并行性问题
- 这个返回元素位置的基于循环的函数有什么问题?
- 为什么循环会导致指针出现问题?
- 使用基于范围的循环迭代矢量时出现问题
- 在 c++ 中循环遍历数组时遇到问题
- 运行无限循环的最小二叉树问题
- 在C++中循环访问自定义结构列表的小问题
- 在 for 循环中更新两个变量时遇到问题C++