Program gets stuck in infinite loop
Can anyone figure out why this program gets stuck in certain instances? ...Whenever you enter a destination with more than one word for the city name (eg "San Antonio") it goes into an infinite loop of the menu function... I've been wracking my brain, but I can't figure it out!
Thanks,
-Sean
Code:
#include <iostream>
#include <string>
using namespace std;
void initalize(string passengers[][6][4]);
void menu(string passengers[][6][4]);
void enterpass(string passengers[][6][4]);
void dispsingle(string passengers[][6][4]);
void dispall(string passengers[][6][4]);
void initalize(string passengers[][6][4])
{
int a,b;
for(a=0;a<10;a++)
{
for(b=0;b<6;b++)
{
passengers[a][b][0] = "\0";
}
}
}
void menu(string passengers[][6][4])
{
int choice=0;
while (choice == 0)
{
cout << "Menu" << endl << "1) Enter passenger" << endl << "2) Display single passenger info" << endl << "3) Display all passengers" << endl << "4) Exit program" << endl;
cout << "Selection: ";
cin >> choice;
switch (choice)
{
case 1:
{
enterpass(passengers);
choice = 0;
break;
}
case 2:
{
dispsingle(passengers);
choice = 0;
break;
}
case 3:
{
dispall(passengers);
choice = 0;
break;
}
case 4:
{
cout << "Thank you for using Lab 5!" << endl;
break;
}
}
}
}
void enterpass(string passengers[][6][4])
{
int row=0, column=0;
while (row != -1 && column != -1)
{
cout << endl << "Which passenger? (Format: row column) Use 0 0 to stop ";
cin >> row >> column;
row -= 1;
column -=1;
if (row > 9 || row < -1)
{
cout << "Invalid row" << endl;
}
else if (column > 5 || column < -1)
{
cout << "Invalid column" << endl;
}
else if (row > -1)
{
cout << endl << "First name: ";
cin >> passengers[row][column][0];
cout << "Last name: ";
cin >> passengers[row][column][1];
cout << "Social Security Number: ";
cin >> passengers[row][column][2];
cout << "Destination city: ";
getline(cin, passengers[row][column][3], '\n');
}
}
}
void dispsingle(string passengers[][6][4])
{
int row=-1, column=-1;
cout << endl << "Enter row and column: ";
cin >> row >> column;
row -=1;
column -= 1;
if (row < 0 || column < 0 || row > 9 || column > 5)
{
cout << "Invalid Passenger!";
return;
}
else
{
cout << endl << "Displaying information for row " << row+1 << ", column " << column+1 << ":" << endl;
cout << "First name:\t\t\t" << passengers[row][column][0] << endl;
cout << "Last name:\t\t\t" << passengers[row][column][1] << endl;
cout << "Social Security Number:\t\t" << passengers[row][column][2] << endl;
cout << "Destination:\t\t\t" << passengers[row][column][3] << "\n\n";
}
}
void dispall(string passengers[][6][4])
{
int a, b;
for (a=0;a<10;a++)
{
if (passengers[a][0][0] != "\0" || passengers[a][1][0] != "\0" || passengers[a][2][0] != "\0" || passengers[a][3][0] != "\0" || passengers[a][4][0] != "\0" || passengers[a][5][0] != "\0")
{
cout << "Row " << a+1 << ":\t";
}
for (b=0;b<6;b++)
{
if (passengers[a][b][0] != "\0")
{
cout << passengers[a][b][0] << " " << passengers[a][b][1] << " ";
}
if (b == 5)
{
cout << endl;
}
}
}
}
int main()
{
string passengers[10][6][4];
initalize(passengers);
menu(passengers);
return 0;
}