Displays Only 1 of 10 Test Scores

Hello, everyone!

I'm doing a C++ program where you have to prompt the user to input ten test scores via an array of type double. Then, that array has to be passed to a function that searches for the largest number of the ten and returns that number to the main function.

For now, I'm focusing on the first part of the program: getting the ten numbers. I added a cout statement that would display the ten numbers to the user, but when it goes to do so, it only shows the 10th test score.


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
  
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;
int main()
{

const int tscores = 10;
double results[tscores];

cout << "Please enter ten test scores from your class." << endl;

cout << "Enter your 1st test score: " << endl;
cin >> results[tscores];
cout << "Enter your 2nd test score: " << endl;
cin >> results[tscores];
cout << "Enter your 3rd test score: " << endl;
cin >> results[tscores];
cout << "Enter your 4th test score: " << endl;
cin >> results[tscores];
cout << "Enter your 5th test score: " << endl;
cin >> results[tscores];
cout << "Enter your 6th test score: " << endl;
cin >> results[tscores];
cout << "Enter your 7th test score: " << endl;
cin >> results[tscores];
cout << "Enter your 8th test score: " << endl;
cin >> results[tscores];
cout << "Enter your 9th test score: " << endl;
cin >> results[tscores];
cout << "Enter your 10th test score: " << endl;
cin >> results[tscores];

cout << "The test scores are" << results[tscores] << "." << endl;

return 0;

}


It's frustrating because I can't pinpoint the issue; I'd sincerely appreciate it if anyone here could.

You are getting the score but putting all them in the same spot. So you put the 10th score at the very last so it only displays the 10th score.
Array is a list of elements so you need to put scores next to each other.

For part 1, it would be better to use a 'for loop'

1
2
3
4
5
6
7
double getScore;
for (int i = 0; i < 10; i++)   // Loop for 10 times
{
 cout << " Enter score for test: " << i+1 << endl;   // Ask to enter test score
 cin << getScore;       // get score
 results[i] = getScore;   // put the score into results array at position 'i' 
}



For part 2, you can user the for loop to look at every element in an array and find the highest and lowest score.
http://www.cplusplus.com/doc/tutorial/arrays/
Well, I tried that, but I must've messed something up, because I received these errors:

Prog4.cpp: In function ‘int main()’:
Prog4.cpp:22:10: error: invalid types ‘int[int]’ for array subscript
Prog4.cpp:28:42: warning: name lookup of ‘i’ changed [enabled by default]
Prog4.cpp:20:5: warning: matches this ‘i’ under ISO standard rules [enabled by default]
Prog4.cpp:24:10: warning: matches this ‘i’ under old rules [enabled by default]

And this is the 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
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;
int main()
{

double getScore; //Array for test scores
int i;
int results;
results[i]=getScore;
{
for (int i=0; i <=10; i++)//For loop for ten scores

cout << "Please enter ten test scores from your class." << endl;

cout << "Enter your 1st test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 2nd test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 3rd test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 4th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 5th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 6th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 7th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 8th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 9th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 10th test score: " << i+1 << endl;
cin >> getScore;


cout << "The test scores are" << getScore << "." << endl;

return 0;
}
}
On line 11 you declare results to be of type int. On line 12 you try to treat results as if it was an array. If it were an array, the line would still be nonsensical - both i and getScore have indeterminate values, so you would likely be accessing memory you don't own and assigning some junk value to it.

Line 16 is the body of the for loop. None of the rest of the code is governed by it. The i defined in the for loop hides the i defined before the for loop. If results were an array of 10 elements and your intention was to access each element, valid indexes are 0 to 9, so your for condition would be wrong. Of course, that isn't what your for loop does. Right now all it would do (if it compiled) is print "Please enter ten test scores from your class" eleven times.

If lines 18 to 37 were governed by your for loop, they would each be repeated 11 times, prompting you to enter 110 scores and attempting to store all of them in the same double variable (resulting in the variable being overwritten 109 times with all but the last score lost.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    const std::size_t size = 10;
    double scores[size] = {};

    // display the values in scores:
    for (std::size_t i = 0; i < size; ++i)
        std::cout << scores[i] << ' ';
    std::cout << '\n';

    // get new values for scores:
    for (std::size_t i = 0; i < size; ++i)
    {
        std::cout << "Score " << i + 1 << ": ";
        std::cin >> scores[i];
    }

    // display the new values:
    for (std::size_t i = 0; i < size; ++i)
        std::cout << scores[i] << ' ';
    std::cout << '\n';
}
Last edited on
Forgive me. I'm trying to fix the code bit by bit.

Now, here are the new errors I'm getting:


Prog4.cpp: In function ‘int main()’:
Prog4.cpp:25:10: error: invalid types ‘int[int]’ for array subscript
Prog4.cpp:27:42: error: name lookup of ‘i’ changed for ISO ‘for’ scoping [-fpermissive]
Prog4.cpp:27:42: note: (if you use ‘-fpermissive’ G++ will accept your code)

And here's the 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

#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;
int main()
{

double getScore; //Array for test scores
int results;
cout << "Please enter ten test scores from your class." << endl;

{
for (int i=0; i<10; i++)//For loop for ten scores
results[i]=getScore;

cout << "Enter your 1st test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 2nd test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 3rd test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 4th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 5th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 6th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 7th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 8th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 9th test score: " << i+1 << endl;
cin >> getScore;
cout << "Enter your 10th test score: " << i+1 << endl;
cin >> getScore;


cout << "The test scores are" << getScore << "." << endl;

return 0;
}
}

Hey, I'm new in cpp, but I tried to fix your code, its basic, but I hope its usefull.

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
#include <iostream>
#include <vector>
using namespace std;
 
int main()
{	
			
	vector<double> result;
 
	int num_scores;
	cout << "Number of scores: " << flush;
	cin >> num_scores;
 
	result.resize (num_scores);
 
	for (vector<double>::size_type i = 0; i < num_scores; i++)
	{
    	cout << "Enter mark for test #" << i+1
        	 << ": " << flush;
    	cin >> result[i];
	}

	for (vector<double>::size_type i = 0; i < num_scores; i++)
	{
    	cout << "Score number #" << i+1 << " Result: " << result[i] << "\n"; 
        
	}
	
	int max = result[0];
	for (vector<double>::size_type i = 0; i < num_scores; i++)
	{
		if (result[i] > max) {
			max = result[i];
		}
		
	}
	cout << "Highest number: " << max << endl;
	
    system("pause");
	return 0;
}
Last edited on
Hey, all. Okay, now I'm here:

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
#include <iostream>
#include <vector>

using namespace std;
int main()
{

vector <double> result;//Will hold the test scores

int tscores; //'tscores' stands for test scores
tscores = 10;
result.resize (tscores);

for (vector <double> ::size_type i=0; i < 10; i++)
{
cout << "Enter score for test #" << i+1
        << ":" << flush;
cin >> tscores;
}

int max = result[10];
int i;
for (vector<double>::size_type i;+0; i < tscores); i++)
{
        if (result[10] > max) {
                max = result[10];
        }
}
cout <<"The highest test score is " << max << endl;

return 0;


These are the errors I'm getting:

Prog4.cpp: In function ‘int main()’:
Prog4.cpp:33:55: error: expected ‘;’ before ‘)’ token
Prog4.cpp:41:9: error: expected ‘}’ at end of input

Do you guys notice anything out of the ordinary? I can't pinpoint a thing!

Did you run the code I posted earlier, before you adjusted it? If it doesn't work, then it could be your compiler.

I see you understand the first part of the code. I might need to explain a little bit about the last part.
1
2
3
4
5
6
7
8
9
10
11
12
13
	int max = result[0]; /*The 0 defines the first answer, so in this case the answer you filled in at 'Enter score for test #1'.
The number you filled in there, will be automaticly converted to the interger called max.  */
	for (vector<double>::size_type i = 0; i < num_scores; i++)
	{
		if (result[i] > max) { 
			max = result[i];
		}/* The loop started and the first round result[i] is equal to result[0], the same as the amount of max, so nothing happens. 
After that, result[i] will be result[1], which corresponds for the answer at #2.
When this answer is higher, the interger max will be changed to the answer at #2. 
If its lower or the same, nothing will happen. This loop will repeat until you have had all the scored */
		
	}
	cout << "Highest number: " << max << endl;

I hope this explains why you shouldn't change int max = result[0]; to int max = result[10]; because then you will start at #11, which you don't even have:P
Okay, I switched it back, but I still have the same two errors. I can't figure out why
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
#include <iostream>
#include <vector>

using namespace std;
int main()
{

vector <double> result;//Will hold the test scores

int tscores; //'tscores' stands for test scores
tscores = 10;
result.resize (tscores);

for (vector <double> ::size_type i=0; i < 10; i++)
{
cout << "Enter score for test #" << i+1
        << ":" << flush;
cin >> result[i]; //You changed this to tscore, causing max to be always equal to 0. Took me a while to find it ;p
}
	
int max = result[0];
for (vector<double>::size_type i = 0; i < 10; i++) // You retyped this part wrong, thats why your script gave errors.
	{
	if (result[i] > max)
		{
			max = result[i];	
		}
	}
		


cout <<"The highest test score is " << max << endl;

return 0;}


I removed the int i, don't know why you added that:P Should work now.
Last edited on
Quick question:

I'm supposed to use arrays. Is there any way to switch to arrays without causing more errors?
I can't help you with that, I have no experience with arrays yet.
No, you're fine! You've helped me so much. My textbook just puts arrays before vectors.

Thank you so much!
I didn't know what a vector was until I stumbled on your problem, I just googled a solution and modified it to fix your problem, I learned a lot from doing this as well haha :P
Maybe you can do the same with arrays idk.
I should've thought of that before! XD
Topic archived. No new replies allowed.