Storing input data on an array

I'm working on a program that finds the average of four numbers, so that they can be stored on an array. The program is complete, but with one fatal flaw: I think the do-while loop is written in a way that keeps giving me a garbage output. I've tried various ways of writing the do-while code, but I still can't get it quite right. This is what it looks like so far, if you could just give me a clue as to what I'm doing wrong.

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
//Input exam grade with a numerical value.
//Compute numerical values into its designated grade.
//Output exam grade with a letter grade.
 
#include <iostream>
#include <cstring>
using namespace std;
int main() 

{
 int i;
 float grade[4];

 int sum = 0;
 int average = 0;
 int flag = 0;

do
{
	flag = 0;
	cout << "Please enter your four exam grades: \n";
		for (i = 1; i < 4; i++)
		{
		
		do 
			{
			cin >> grade[i];
			cout << "You've entered invalid data!" << endl;
			flag = 1;
			}while ((grade[i] < 0) || (grade[i] > 100));}

}while (flag == 1);

 sum = grade[i] + grade[i] + grade[i], grade[i];	
 average = sum/4;
 cout << "Exam Grades: " <<  grade[i] << "," << grade[i] << "," << grade[i] << "," << grade[i] << endl;


 char letter;
 letter = 'A';
 letter = 'B';
 letter = 'C';
 letter = 'D';
 letter = 'F';

 if(average >= 90)
 letter = ('A');

 else if(average >= 80)
 letter = ('B');

 else if(average >= 70)
 letter = ('C');

 else if(average >= 60)
 letter = ('D');

 else if (average < 60)
 letter = ('F');

 int weighted1 = 0;
 int weighted2 = 0;
 int weighted3 = 0;
 int weighted4 = 0;
 int weighted_average = 0;
 
 weighted1 = (grade[i] * .20);
 weighted2 = (grade[i] * .20);
 weighted3 = (grade[i] * .20);
 weighted4 = (grade[i] * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

 
  cout << "This is your letter grade: " << grade[4] << endl;
  cout << "This is your average: " << weighted_average << endl;
	system ("pause");

  return 0;
}
Last edited on
To access arrays you start from 0, not 1
'grade' has 4 elements, and they will be stored at indexes (indices?) 0, 1, 2, 3
On line 34 you access grade[4] four times (because the loop stops when i == 4), which leads to reading the value stored in memory right after the last element of the array.
Also, you're not adding all the values of the array, but the same value three ties (three because before the fourth there is a comma).
Thanks I didn't even notice the comma when I proofread it. When I changed int = 1 to int = 0, it ended the garbage outputs, but now it gave me this...

You've entered invalid data!
You've entered invalid data!
You've entered invalid data!
You've entered invalid data!
Please enter your four exam grades:
int = 1 is not a valid instruction. What variable did you change? It's better if you post the new code.

Also I honestly can't imagine why it would print "You've entered invalid data!" before "Please enter your four exam grades:"
Nether can I. Here's the new code.

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
//Input exam grade with a numerical value.
//Compute numerical values into its designated grade.
//Output exam grade with a letter grade.
 
#include <iostream>
#include <cstring>
using namespace std;
int main() 

{
 int i;
 int grade[4];

 int sum = 0;
 int average = 0;
 int flag = 0;

do
{
	flag = 0;
	cout << "Please enter your four exam grades: \n";
		for (i = 0; i < 4; i++)
		{
		
		do 
			{
			cin >> grade[i];
			cout << "You've entered invalid data!" << endl;
			flag = 1;
			}while ((grade[i] < 0) || (grade[i] > 100));}

}while (flag == 1);

 sum = grade[i] + grade[i] + grade[i] + grade[i];	
 average = sum/4;
 cout << "Exam Grades: " <<  grade[i] << "," << grade[i] << "," << grade[i] << "," << grade[i] << endl;


 char letter;
 letter = 'A';
 letter = 'B';
 letter = 'C';
 letter = 'D';
 letter = 'F';

 if(average >= 90)
 letter = ('A');

 else if(average >= 80)
 letter = ('B');

 else if(average >= 70)
 letter = ('C');

 else if(average >= 60)
 letter = ('D');

 else if (average < 60)
 letter = ('F');

 int weighted1 = 0;
 int weighted2 = 0;
 int weighted3 = 0;
 int weighted4 = 0;
 int weighted_average = 0;
 
 weighted1 = (grade[i] * .20);
 weighted2 = (grade[i] * .20);
 weighted3 = (grade[i] * .20);
 weighted4 = (grade[i] * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);

 
  cout << "This is your letter grade: " << grade[4] << endl;
  cout << "This is your average: " << weighted_average << endl;
	system ("pause");

  return 0;
}
Here's an alternate code that solves the problem, but it tends to repeat the first line of data multiple times.


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
#include <iostream>
#include <iomanip>
#include <cstring>
using namespace std;
int main() 

{
 int sum = 0;
 int average = 0;
 int flag = 0;


 const int size =4;
	int i;
	int exam[4];
	for (i = 0; i < 4; i++)

do
{
	flag = 0;
	cout << "Please input your first exam grade and press enter: \n";
		cin >> exam[1];
		if ((exam[1] < 0) || (exam[1] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your second exam grade and press enter: \n";
		cin >> exam[2];
		if ((exam[2] < 0) || (exam[2] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your third exam grade and press enter: \n";
		cin >> exam[3];
		if ((exam[3] < 0) || (exam[3] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your final exam grade and press enter: \n";
		cin >> exam[4];
		if ((exam[4] < 0) || (exam[4] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);



 sum = exam[1] + exam[2] + exam[3] + exam[4];
 average = sum/4;
 cout << "Exam Grades: " <<  exam[1] << "," << exam[2] << "," << exam[3] << "," << exam[4] << endl;


 char grade;
 grade = 'A';
 grade = 'B';
 grade = 'C';
 grade = 'D';
 grade = 'F';
 

 if(average >= 90)
 grade = ('A');

 else if(average >= 80)
 grade = ('B');

 else if(average >= 70)
 grade = ('C');

 else if(average >= 60)
 grade = ('D');

 else if (average < 60)
 grade = ('F');

 
 int weighted1 = 0;
 int weighted2 = 0;
 int weighted3 = 0;
 int weighted4 = 0;
 int weighted_average = 0;
 
 weighted1 = (exam[1] * .20);
 weighted2 = (exam[2] * .20);
 weighted3 = (exam[3] * .20);
 weighted4 = (exam[4] * .40);
 weighted_average = (weighted1 + weighted2 + weighted3 + weighted4);
 
  cout << "This is your letter grade: " << grade << endl;
  cout << "This is your average: " << weighted_average << endl;
	system ("pause");

  return 0;
}
You are still accessing exam[4]. The last valid index for an array of 4 elements is 3. And the loop was fine before (after you made it start from 0).
Last edited on
Yeah, I realized it was repeating the first one because of the for (i = 0; i < 4; i++). I think everything looks okay, but is technically in an array because it had to ask four times?

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235

/Input exam grade with a numerical value.
//Compute numerical values into its designated grade.
//Output exam grade with a letter grade.


#include <iostream>
#include <iomanip>
using namespace std;
int main() 

{
 int sum = 0;
 int average = 0;
 int flag = 0;


 const int size =5;
	int i;
	float exam[size];
do
{
	flag = 0;
	cout << "Please input your first exam grade and press enter: \n";
		cin >> exam[0];
		if ((exam[0] < 0) || (exam[0] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your second exam grade and press enter: \n";
		cin >> exam[1];
		if ((exam[1] < 0) || (exam[1] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your third exam grade and press enter: \n";
		cin >> exam[2];
		if ((exam[2] < 0) || (exam[2] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your final exam grade and press enter: \n";
		cin >> exam[3];
		if ((exam[3] < 0) || (exam[3] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);



 sum = exam[0] + exam[1] + exam[2] + exam[3];
 average = sum/4;
 cout << "Exam Grades: " <<  exam[0] << setw(5) << exam[1] << setw(5) << exam[2] << setw(5) << exam[3] << endl;


 char grade;
 grade = 'A';
 grade = 'B';
 grade = 'C';
 grade = 'D';
 grade = 'F';
 

 if(average >= 90)
 grade = ('A');

 else if(average >= 80)
 grade = ('B');

 else if(average >= 70)
 grade = ('C');

 else if(average >= 60)
 grade = ('D');

 else if (average < 60)
 grade = ('F');

 
 int weighted[4];
 int weighted_average = 0;
 
 weighted[0] = (exam[0] * .20);
 weighted[1] = (exam[1] * .20);
 weighted[2] = (exam[2] * .20);
 weighted[3] = (exam[3] * .40);
 weighted_average = (weighted[0] + weighted[1] + weighted[2] + weighted[3]);
 
  cout << "This is your letter grade: " << grade << endl;
  cout << "This is your average: " << weighted_average << endl;
  system ("pause");

  return 0;
}//Input exam grade with a numerical value.
//Compute numerical values into its designated grade.
//Output exam grade with a letter grade.
 
//Nicholas Terra
//7-12-13

#include <iostream>
#include <iomanip>
using namespace std;
int main() 

{
 int sum = 0;
 int average = 0;
 int flag = 0;


 const int size =5;
	int i;
	float exam[size];
do
{
	flag = 0;
	cout << "Please input your first exam grade and press enter: \n";
		cin >> exam[0];
		if ((exam[0] < 0) || (exam[0] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your second exam grade and press enter: \n";
		cin >> exam[1];
		if ((exam[1] < 0) || (exam[1] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your third exam grade and press enter: \n";
		cin >> exam[2];
		if ((exam[2] < 0) || (exam[2] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);

do
{
	flag = 0;
	cout << "Please input your final exam grade and press enter: \n";
		cin >> exam[3];
		if ((exam[3] < 0) || (exam[3] > 100))

		{
			cout << "You've entered invalid data!" << endl;
			flag = 1;
		}
}while (flag == 1);



 sum = exam[0] + exam[1] + exam[2] + exam[3];
 average = sum/4;
 cout << "Exam Grades: " <<  exam[0] << setw(5) << exam[1] << setw(5) << exam[2] << setw(5) << exam[3] << endl;


 char grade;
 grade = 'A';
 grade = 'B';
 grade = 'C';
 grade = 'D';
 grade = 'F';
 

 if(average >= 90)
 grade = ('A');

 else if(average >= 80)
 grade = ('B');

 else if(average >= 70)
 grade = ('C');

 else if(average >= 60)
 grade = ('D');

 else if (average < 60)
 grade = ('F');

 
 int weighted[4];
 int weighted_average = 0;
 
 weighted[0] = (exam[0] * .20);
 weighted[1] = (exam[1] * .20);
 weighted[2] = (exam[2] * .20);
 weighted[3] = (exam[3] * .40);
 weighted_average = (weighted[0] + weighted[1] + weighted[2] + weighted[3]);
 
  cout << "This is your letter grade: " << grade << endl;
  cout << "This is your average: " << weighted_average << endl;

  return 0;
}
I don't quite understand your question.

Could you rephrase?
Last edited on
My original question or the one I asked most recently?
Most recent.
I was wondering if my code is still considered an array because this is code applies.
1
2
3
const int size =5;
	int i;
	float exam[size]; 


If it weren't an array, it would've been written like this.

1
2
3
4
exam0 = 0;
exam1 = 0;
exam2 = 0;
exam3 = 0;

Last edited on
Yes, that's still an array.
Topic archived. No new replies allowed.