比较两个字符串后卡在无限循环中

Stuck in infinite loop after comparing two strings

本文关键字:无限循环 字符串 两个 比较      更新时间:2023-10-16

我创建了一个函数,用于将用户输入的字符串与最初从文件中提取的结构数组中的字符串进行比较。

我正在测试的字符串列表是"奥斯汀、达拉斯、华盛顿特区和芝加哥"。除了华盛顿特区之外,每个城市都在运作,这使得程序陷入了无限循环。我尝试调试和重写部分代码,但同样的事情不断发生。

正在读取的文本文件

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。因此,循环在失败时执行。 它清除无效状态并丢弃任何新行(从而丢弃任何错误数据(。