while(1) loop problem

I am trying to create a simple grade storing console. The console allows you to enter grades or view them. When I try to view the grades I have entered, it always asks me if I would like to enter or view grades, like in the beginning. help, please!
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Source Code
#include <iostream>
using namespace std;
//These are all the variables I want to store.
int enterorsee;
int whichstudent;
int whichstudentsee;
string name1;
string name2;
string name3;
int whichassign;
int assign1grade1;
int assign1grade2;
int assign1grade3;
int assign1pp1;
int assign1pp2;
int assign1pp3;
int assign2grade1;
int assign2grade2;
int assign2grade3;
int assign2pp1;
int assign2pp2;
int assign2pp3;
int assign3grade1;
int assign3grade2;
int assign3grade3;
int assign3pp1;
int assign3pp2;
int assign3pp3;
int Finalgrade1;
int Finalgrade2;
int Finalgrade3;
int i;
//This is where I will put all my code
int main()
{
cout<< "Welcome to the GMC. Type in 1 to enter grades or 2 to look at them. ";
while (1) {
cout<< "Would you like to enter grades or look at existing ones?\n ";
cin >> enterorsee;
if (enterorsee == 1) {
cout << "Which student would you like to enter grades for?\n";
cin >> whichstudent;
if (whichstudent == 1) {
if (name1 == "") {
cout<< "What is Student Ones name?\n";
cin >> name1;
}

else {
cout << "Which assignment would you like to enter?\n";
cin >> whichassign;
if (whichassign == 1) {
cout << "What did this student get on assignment 1?\n";
cin >> assign1grade1;
cout << "Out of how many points was this assignment?\n";
cin >> assign1pp1;
}
if (whichassign == 2) {
cout << "What did this student get on assignment 2?\n";
cin >> assign2grade1;
cout << "Out of how many points was this assignment?\n";
cin >> assign2pp1;
}
if (whichassign == 3) {
cout << "What did this student get on assignment 3?\n";
cin >> assign3grade1;
cout << "Out of how many points was this assignment?\n";
cin >> assign3pp1;
}

}
}
if (whichstudent == 2) {
if (name2 == "") {
cout<< "What is Student Twos name?\n";
cin >> name2;
}
else {
cout << "Which assignment would you like to enter?\n";
cin >> whichassign;
if (whichassign == 1) {
cout << "What did this student get on assignment 1?\n";
cin >> assign1grade2;
cout << "Out of how many points was this assignment?\n";
cin >> assign1pp2;
}
if (whichassign == 2) {
cout << "What did this student get on assignment 2?\n";
cin >> assign2grade2;
cout << "Out of how many points was this assignment?\n";
cin >> assign2pp2;
}
if (whichassign == 3) {
cout << "What did this student get on assignment 3?\n";
cin >> assign3grade2;
cout << "Out of how many points was this assignment?\n";
cin >> assign3pp2;
}

}
}
if (whichstudent == 3) {
if (name2 == "") {
cout<< "What is Student Threes name?\n";
cin >> name3;
}
else {
cout << "Which assignment would you like to enter?\n";
cin >> whichassign;
if (whichassign == 1) {
cout << "What did this student get on assignment 1?\n";
cin >> assign1grade3;
cout << "Out of how many points was this assignment?\n";
cin >> assign1pp3;
}
if (whichassign == 2) {
cout << "What did this student get on assignment 2?\n";
cin >> assign2grade3;
cout << "Out of how many points was this assignment?\n";
cin >> assign2pp3;
}
if (whichassign == 3) {
cout << "What did this student get on assignment 3?\n";
cin >> assign3grade3;
cout << "Out of how many points was this assignment?\n";
cin >> assign3pp3;
}



if (enterorsee == 2) {
cout<<"Which student's grades would you like to see?\n";
cin >> whichstudentsee;
if (whichstudentsee == 1) {
cout << name1;
cout << " got a";
cout << assign1grade1 / assign1pp1 * 100;
cout << " percent on his/her first assignment,\n";
cout << name1;
cout << " got a";
cout << assign2grade1 / assign2pp1 * 100;
cout << " percent on his/her second assignment,\n";
cout << name1;
cout << " got a";
cout << assign3grade1 / assign3pp1 * 100;
cout << " percent on his/her third assignment,\n";
cout << "And at the moment has a ";
cout << (assign1grade1+assign2grade1+assign3grade1) / (assign1pp1+assign2pp1+assign3pp1) * 100;
cout << " in this class.\n";
}
if (whichstudentsee == 2) {
cout << name2;
cout << " got a";
cout << assign1grade2 / assign1pp2 * 100;
cout << " percent on his/her first assignment,\n";
cout << name2;
cout << " got a";
cout << assign2grade2 / assign2pp2 * 100;
cout << " percent on his/her second assignment,\n";
cout << name2;
cout << " got a";
cout << assign3grade2 / assign3pp2 * 100;
cout << " percent on his/her third assignment,\n";
cout << "And at the moment has a ";
cout << (assign1grade2+assign2grade2+assign3grade2) / (assign1pp2+assign2pp2+assign3pp2) * 100;
cout << " in this class.\n";
}
if (whichstudentsee == 3) {
cout << name3;
cout << " got a";
cout << assign1grade3 / assign1pp3 * 100;
cout << " percent on his/her first assignment,\n";
cout << name3;
cout << " got a";
cout << assign2grade3 / assign2pp3 * 100;
cout << " percent on his/her second assignment,\n";
cout << name3;
cout << " got a";
cout << assign3grade3 / assign3pp3 * 100;
cout << " percent on his/her third assignment,\n";
cout << "And at the moment has a ";
cout << (assign1grade3+assign2grade3+assign3grade3) / (assign1pp3+assign2pp3+assign3pp3) * 100;
cout << " in this class.\n";
}

}
}
}
}

}


}
Last edited on
With that amount of if statements it's pretty hard to tell what's going on.
Please put [ Code] [ /Code] statements about your code so it is easier to read.
I put the tags :)
Well one of the main problems is your lack of indentation right now (which is actually probably causing the problem because of your {} not matching up correctly.

For example you should do
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
while (1) 
{
	cout<< "Would you like to enter grades or look at existing ones?\n ";
	cin >> enterorsee;

	if (enterorsee == 1)
	{
		cout << "Which student would you like to enter grades for?\n";
		cin >> whichstudent;

		if (whichstudent == 1)
		{
			if (name1 == "")
			{
				cout<< "What is Student Ones name?\n";
			}
		}
	}

(This might not be exactly how you have your code set up right now but you get the idea). It's almost impossible for people to review your code when it is all in 1 line and your teacher wont like it either.
hard to read without indentation, but what i see so far is that the number of open and close curly brackets between line 41 to 131 don't match up. Your indentation at line 132 should line up with the one at 41, so your code starting at line 132 if(enterorsee == 2) wil never happen because it is inside the if(enterorsee == 1) condition.
Thank you all for the help. I found out one of the brackets was in the wrong place and that was made all the difference.
You need to learn how to use arrays and functions.

You're doing the same time over and over. The only thing that changes is the student number and the assignment number. Breaking your program into functions helps to reduce the problem massively nested ifs.
For example:
1
2
3
4
if (enterorsee == 1) 
    enter_student();
if (enterorsee == 2) 
    see_student();

Each function does one thing.

Instead of creating a separate variable for each grade. Use an array.
1
2
3
4
const int MAX_GRADES = 3;
int grade[MAX_GRADES];
...
    cin >> grade[whichassign];






Topic archived. No new replies allowed.