比较两个字符串后卡在无限循环中
Stuck in infinite loop after comparing two strings
我创建了一个函数,用于将用户输入的字符串与最初从文件中提取的结构数组中的字符串进行比较。
我正在测试的字符串列表是"奥斯汀、达拉斯、华盛顿特区和芝加哥"。除了华盛顿特区之外,每个城市都在运作,这使得程序陷入了无限循环。我尝试调试和重写部分代码,但同样的事情不断发生。
正在读取的文本文件
Austin,Houston,109,140
Washington D.C.,Seattle,139,421
Austin,New York,94,1511
Dallas,Austin,93,74
Chicago,Las Vegas,149,1039
第一个城市是离开的城市。第二个城市是到达城市。然后第一个数字是飞行费用,第二个数字是距离。
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct data {
string departureCity;
string arrivalCity;
int cost;
int distance;
};
void readFlights(data flightList[], int& SIZE) {
ifstream inData("flights.csv"); // Opens csv file
string flightCost;
string flightDistance;
int i = 0;
// Goes through the csv file and assigns each string and int into a struct array
// Having issues where it reads an additional line even though it should read eof
while (!inData.eof()) {
getline(inData, flightList[i].departureCity, ',');
getline(inData, flightList[i].arrivalCity, ',');
getline(inData, flightCost, ',');
flightList[i].cost = stoi(flightCost);
getline(inData, flightDistance);
flightList[i].distance = stoi(flightDistance);
i++;
}
inData.close(); // Closes csv file
SIZE = i-1;
}
void printFlightsFrom(data flightList[], int SIZE) {
string userInput;
int i = 0; //array counter
bool cityCheck = false; //Checks to see if the users city was used.
cout << "Enter a city you are looking to depart from." << endl;
cin >> userInput;
cout << endl;
do {
if (!userInput.compare(flightList[i].departureCity)) {
cout << "Departure City: " << flightList[i].departureCity
<< " Arrival City: " << flightList[i].arrivalCity
<< " Cost: " << flightList[i].cost
<< " Distance: " << flightList[i].distance << endl;
cityCheck = true;
}
i++;
} while (i < 5);
if (cityCheck == false) {
cout << "You have entered an unavailable city. Returning to main menu." << endl;
}
}
int main() {
int SIZE = 100;
data flightList[SIZE]; //Creates an array of size 100 for storing the file data
readFlights(flightList, SIZE); //function that opens a file and writes the contents to a struc array
int menu_choice = 0;
do {
cout << "Select an action:" << endl;
cout << "2) Show the flights that depart from a given city" << endl;
cout << "4) Exit the program" << endl;
cin >> menu_choice;
switch (menu_choice) {
case 2: printFlightsFrom(flightList, SIZE);
break;
case 4: cout << "goodbye!" << endl;
break;
default: cout << "Invalid choice" << endl;
}
}while (menu_choice != 4);
return 0;
}
在进行调试时,"华盛顿特区"从未被识别为一个城市,因此程序会回退到主程序,并在menu_choice内继续通过切换。它再也不会要求用户输入,并不断抽出主线。
任何帮助都非常感谢
更新:虽然字符串"华盛顿特区"仍然无法正确比较,但我已将无限循环问题缩小到 do while 循环。在 menu_choice 变量中键入字符后,它显示无效选择,然后永远不会让用户有机会输入新menu_choice。它再次键入所有内容,然后当然是"无效选择",然后无限期地继续循环。
cin >> userInput
实际上读取空间,因此它只读取"华盛顿",让"D.C."进入输入缓冲区。
后来,cin >> menu_choice
假装一个数字,但缓冲区中仍然有"D.C.",因此 cin 设置为"无效"。从那时起,每次读取都会失败。
- 如果您需要读取也应该包含空格的字符串,请使用 std::getline 如
std::getline(std::cin, userInput)
. - 尝试读取数字时,请检查失败(用户可以输入任何内容...
检查无效读取的方法是这样的
while(!(cin>>menu_choiche))
{
cout << "invalid choiche - retryn";
cin.clear(); cin.ignore(0u-1,'n');
}
cin >> menu_choice
返回cin
,如果无效,则转换为false
。因此,循环在失败时执行。 它清除无效状态并丢弃任何新行(从而丢弃任何错误数据(。
相关文章:
- 过载'operator new'如何导致无限循环?
- C++正则表达式无限循环
- 程序在尝试猜测它选择的随机数时进入无限循环?
- 遍历链表时的无限循环
- 循环链表:无限循环
- 比较两个字符串后卡在无限循环中
- 在做一段时间内检查字符的无限循环
- 逐字读取文本文件中的每一行并转换为 int(无限循环或崩溃?
- 在这个无限循环中,当输入 0 时终止,当输入 int 范围之外的任何内容时,程序行为不正常
- 可能的无限循环
- 加密字符串但接收无限循环
- 当我的 if 语句在字符串中找到空格时,在无限循环中切换
- 开关大小写语句中函数中的字符串输入会导致无限循环和绕过输入
- 输入 2 个非常基本的 C 字符串时的无限循环
- 输入验证,需要数字,而不是字符串.代码导致无限循环
- C++用户输入到字符串数组的无限循环
- C++字符串替换函数无限循环
- 无限循环记录字符串
- 为什么字符串中的空格使我的代码无限循环
- 当输入字符串而期望输入数字时,使用cin进行无限循环