Infinite Loop

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;

//symbolic constants
const double IN_STATE = 418.35;
const double OUT_STATE = 688.65;
const double GRADUATE = 554.00;

cout << "Welcome to Ramapo College's tuition processing program." << endl;

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');
}
}
Last edited on
Please use code tags:
http://www.cplusplus.com/articles/jEywvCM9/
16
17
18
19
20
21
22
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?
what do you mean by code tags? I'm sorry this is my first computer science course
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 looked at it but i'm confused on how to do it im sorry
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <string>
#include <iostream>

const int BUFFER_SIZE = 256;

using namespace std;

int main()
{

	//Variable Declerations
	char studentTuition = 'n';
	char status = 'n';
	string statusName = "";
	unsigned short credits = 0;
	unsigned short tuition = 0;
	unsigned short totalTuition = 0;

	//symbolic constants
	const double IN_STATE = 418.35;
	const double OUT_STATE = 688.65;
	const double GRADUATE = 554.00;

	cout << "Welcome to Ramapo College's tuition processing program." << endl;

	do
	{
	//Initialize whether to process student's tuition y/n
		system("CLS");
		cout << "Would you like to process a student's tuition (y/n)? y\b";

		if((studentTuition = tolower(cin.get())) == '\n')
		{
			studentTuition = 'y';
		}
		else
			cin.ignore(BUFFER_SIZE, '\n');

		if(studentTuition == 'y')
		{
			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: ";
			status = tolower(cin.get());			
			} 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;
				while(!(cin >> credits))
				{
					cin.clear();
					cin.ignore(BUFFER_SIZE, '\n');
					cout << "Please enter the number of credits for which"
						" the student is registering (1-20)?" << endl;
				}
				cin.ignore(BUFFER_SIZE, '\n'); 
			} 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;
			cin.ignore(BUFFER_SIZE, '\n');
		}
		else
		{
			break;
		}	

	} while (studentTuition == 'y');

	return EXIT_SUCCESS;
}
Last edited on
Topic archived. No new replies allowed.