Hi, I am trying to write a program but I am getting an infinite loop and have no idea why, we just studied loops today so i'm new at this. can anyone find out where i am making a mistake?
//Variable Declerations
char studentTuition;
char status;
string statusName;
unsigned short credits;
unsigned short tuition;
unsigned short totalTuition;
do
{
//Initialize whether to process student's tuition y/n
cout << "Would you like to process a student's tuition (y/n)?" << endl;
cin >> studentTuition;
studentTuition = tolower (studentTuition);
}while (!( studentTuition == 'y' || studentTuition != 'n'));
while ( studentTuition == 'y' || studentTuition != 'n' )
{
do
{
//Input whether student in-state i/I, out of state o/O or qualified graduate g/G
cout << "Please enter whether this student is in-state (i/I), out-of-state (o/O) or qualified graduate$
cin >> status;
status = tolower (status);
}while (!( status == 'i' || status == 'o' || status == 'g'));
while ( status == 'i' || status == 'o' || status == 'g')
{
do
{
//Input number of credits (1-20)
cout << "Please enter the number of credits for which the student is registering (1-20)?" << endl;
cin >> credits;
}while (!( credits >= 1 && credits <= 20));
//compute status name and tuition based on credits
switch (status)
{
case 'i' : statusName = "In-state";
tuition = IN_STATE;
break;
case 'o' : statusName = "Out-of-state";
tuition = OUT_STATE;
break;
case 'g' : statusName = "qualified RCC graduate";
tuition = GRADUATE;
break;
}
//compute tuition based on credits
totalTuition = credits * tuition;
//output
cout << "The tuition for " << credits << " at " << statusName << " rate is $ " << totalTuition << endl; $
do
{
//update whether to process student's tuition y/n
cout << "Would you like to process a student's tuition (y/n)?" << endl;
cin >> studentTuition;
studentTuition = tolower (studentTuition);
}while ( studentTuition == 'y' || studentTuition != 'n');
}
}
do
{
//Initialize whether to process student's tuition y/n
cout << "Would you like to process a student's tuition (y/n)?" << endl;
cin >> studentTuition;
studentTuition = tolower (studentTuition);
}while (!( studentTuition == 'y' || studentTuition != 'n'));
Let's look at this loop condition. If you enter 'y', the loop will end. If you enter 'n', the loop will continue. If you enter anything else, the loop will end. Why do you have that ! there? Why are you using || instead of &&?
Remember, the loop condition is what makes the loop keep going, not what makes it stop.
I stopped reading the code there due to the lack of code tags. Where specifically are you having infinite loop problems?
Code tags are just what you are using to make the code look nicer on this website, it's not part of the language or anything, just read the link. It explains it.
I think the last do-while loop is causing your problems. It you answer 'y' or anything other than 'n', the loop will repeat and prompt the user whether to process tuition. I think you may have been trying to repeat the program if they user enters 'y'. If so, something like this might work: