How to print out all scores user inputs? For loop

for(counter=2;counter<=20;counter++)
{
cin>>score;
if(score>max)
{
max2=max;
max=score;
}
}
cout<<max<<" = First highest score"<<endl;
cout<<max2<<" = Second highest score"<<endl;


I can't figure out how to print out all the scores the user inputs. Would I have to declare each score? If so how?
hello wonsahm chung

you should have used array. your counter value keeps on increasing, but every time the user inputs, it is stored in "score", thus overwriting the previous "score" value. and why did you start the counter from 2? :|
Oh sorry lol this is before the for loop
cout<<"Enter the twenty SAT scores :"<<endl;
cin>>score;

And I haven't learned array yet, I dunno why I didn't think of the overwriting of the score value. Hmmmm
This is actually the whole program
#include <iostream>
using namespace std;
int main()
{
int max=0,max2=0,score,counter;
int c6=1,c5=0,c4=0,c3=0,c2,c1;

cout<<"Enter the twenty SAT scores :"<<endl;
cin>>score;

for(counter=2;counter<=20;counter++)
{
cin>>score;
if(score>max)
{
max2=max;
max=score;
}
if(score>=700)
c6++;
else if(score<700&&score>=600)
c5++;
else if(score<600&&score>=500)
c4++;
else if(score<500&&score>=400)
c3++;
else if(score<400&&score>=300)
c2++;
else if(score<300&&score>=200)
c1++;

}
cout<<max<<" = First highest score"<<endl;
cout<<max2<<" = Second highest score"<<endl;
cout<<c6<<" "<<c5<<" "<<c4<<" "<<c3<<" "<<c2<<" "<<c1<<endl;

system("pause");
return 0;

}
I have to put them each into categories
how to print out all the scores the user inputs

i've read your code and for the matter above, you'd have to learn array. assuming you take in 50 inputs, without array, you'd have to declare "int a1" all the way to "int a50". then cout a1 to a50. its not impossible, but not a good idea.

int c6=1,c5=0,c4=0,c3=0,c2,c1;
why does c6 initializes as 1, and the rest as 0? they all should be zero lol.

1
2
3
4
5
6
cout<<"Enter the twenty SAT scores :"<<endl;
cin>>score;

for(counter=2;counter<=20;counter++)
{
cin>>score;

if you use this way, notice that the first score does not enter the for loop, and therefore, the first score is not taken into consideration, regardless of its value.
and, why initialize "counter" from 2 to 20?
I've set them that way because the output would come out all weird and crazy, its very weird. I'm using Bloodshed dev C++ from that program in my post this is the output I get with c6=1, and counter being set to 2, I needed 20 inputs and setting counter=2 makes it 20.

This is the output. Now I'm having trouble getting the 2nd highest score, it keeps getting set to 0

Enter the twenty SAT scores :
750
750
750
650
650
650
550
550
550
450
450
450
350
350
350
250
250
250
250
250
750 = First highest score
0 = Second highest score
3 3 3 3 3 5
Press any key to continue . . .
Actually you are right I restarted dev C++ and it came out to 4 for c6,
well, you should try to get visual c++ express 2010. its better than dev c++ in my opinion. though its large lol and if you're a programmer, get visual studio 2010.

1
2
3
4
5
if(score>max)
{
	max2=max;
	max=score;
} 

this condition never really works. first thing to consider was, what if there was two same scores, and both of them are the highest? logic-wise for me, the first highest score and the second are the same. and, ive tried checking around, but the second highest score is always not zero... i guess somethings wrong with your dev c++?

i have some free time in hand, so i pretty much coded your whole program. take a look at it. i tried it on visual c+ and its working fine.
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
#include <iostream>

using namespace std;

void main ()
{
	int max1=0, max2=0;
	int score=0, counter=0;
	int c6=0, c5=0, c4=0, c3=0, c2=0, c1=0;
	
	for (counter=0; counter<20; counter++)
	{
		cout << "Enter number " << counter+1 << " score: ";
		cin >> score;

		if (score >= max1)
		{
			max2=max1;
			max1=score;
		}
		else if (score < max1 && score > max2)
		{
			max2=score;
		}

		if(score >= 700)
			c6++;
		else if(score < 700 && score >= 600)
			c5++;
		else if(score < 600 && score >= 500)
			c4++;
		else if(score < 500 && score >= 400)
			c3++;
		else if(score < 400 && score >= 300)
			c2++;
		else if(score < 300 && score >= 200)
			c1++;
	}

	cout << endl << "The highest score\t\t: " << max1 << endl;
	cout << "The second highest score\t: " << max2 << endl << endl;
	cout << "Number of students with the score of" << endl;
	cout << "- More than 700\t\t: " << c6 << endl;
	cout << "- Between 699 and 600\t: " << c5 << endl;
	cout << "- Between 599 and 500\t: " << c4 << endl;
	cout << "- Between 499 and 400\t: " << c3 << endl;
	cout << "- Between 399 and 300\t: " << c2 << endl;
	cout << "- Between 299 and 200\t: " << c1 << endl << endl;
}
I have been trolling stalking the site all night (heh heh, rhymes) and thought I'd throw a link in for the OP as well as my thoughts for an IDE

http://cplusplus.com/articles/36vU7k9E/

Code::Blocks. It's not huge, it is simple. Once you get further into the programming language, then making the jump to VC could be a good idea, but I would say that for a beginner, it's a bit much (but not necessarily unreasonable).

Also:

@OP

Please use [code][/code] tags

@Ng Han Seng

Please don't make a habit of the following:

void main ()

main should always return an int

Doing the work for other people (especially without explanatory comments either in the code or elsewhere in the post). You're not really giving all that much help to the OP (remember, programming isn't just writing the code, it's problem solving as well).
Last edited on
i know that main should always be int, but i only practice this when the return value is not really important, or usually on small programs. thanks for the advice though.

well, standing at only 6 posts, including this, i'm practically new in this forum. thanks for the advice.
Topic archived. No new replies allowed.