While loop problem

Sep 30, 2015 at 11:59pm
...
Last edited on Oct 2, 2015 at 1:20am
Oct 1, 2015 at 12:24am
You have several problems with this, but first off, that is not really a loop. What would happen if there were 200 students?
Write a program which repeatedly asks the user to enter students' score in a test until the user enters -1.

The first line tells you what to use for a condition to end the loop. so...
1
2
3
4
while(grade != -1)
{

}

With that you should be able to think of some ways to accomplish the rest. There should only be one line asking for a grade and one to get the input, for example.
Oct 1, 2015 at 1:54am
closed account (Eybjz8AR)
admkrk's idea would only work if you entered -1 on the last input. I would first make your main loop something like while(x == 0) and have it loop for ever then after every cin >> grade;
i would do an if statement saying

1
2
if (grade == -1)
      break;


this would break your main loop then you can continue on with the calculations. how ever you would need to store all you values in an array which would be cool if you are conferrable with them



Last edited on Oct 1, 2015 at 1:54am
Oct 1, 2015 at 5:11am
For @OP, here's another piece of food for thought, since I haven't seen it in your code yet. What if I enter -50?
Oct 1, 2015 at 8:08am
admkrk's idea would only work if you entered -1 on the last input.

Is not that the idea?

how ever you would need to store all you values in an array

Why? There is no specification to track individual grades. A running total along with a count of the number of grades is all that is asked for. There is no need to use an array to do either.

What if I enter -50?

Simple exercises like this assume correct input. Validating input only adds to the complexity when getting the logic right is the main goal.

My answer was not meant to solve every problem with garen's code, only to get him started thinking in the right direction. Assuming his code did work, the average would be -1, regardless of grades entered or number of grades entered. If you want to help him, please do not distract him with irrelevant ideas.
Oct 1, 2015 at 10:59am
Decently commented 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
#include <iostream>
using std::cout; using std::cin; using std::endl;

int main() 
{
	int highest = 0, 	//highest score
	score = 0, 		//current score
	sum = 0, 			//total score
	count = 0;		//number of scores
	
	for(;;)
	{
		cout << "Enter Student " << count+1 << "\'s score:";		//count+1, so we start at Student 1
		cin >> score;
		
		if(score == -1)		//note the break before adding the score to the total
			break;		//this is so that -1 isn't added to the total which messes up the total and the average
		
		sum += score;		//keep track of the total score
					//'?' known as the conditional or ternary operator; makes simple if-else statements in one line
		(score > highest) ? (highest = score) : 0;		//if current score entered is greater than highest, store the current score entered
		
		count++;		//increment count
	}
	
	cout << "\nTotal score is: " << sum << endl;
	cout << "Average is: " << sum/count << endl;
	cout << "Highest score is: " << highest << endl;
	
	return 0;
}


edit: Formatting broke :/
https://ideone.com/3hgOq6
Last edited on Oct 1, 2015 at 11:16am
Oct 1, 2015 at 3:23pm
Oct 1, 2015 at 8:44pm
Simple exercises like this assume correct input.

No they don't. There's a reason why @OP's professor put the exact instructions "grade must be from 0 to 100". The only thing they might possibly assume is correct data type. So they're gonna stick to ints if using ints, floats if using floats, etc.

Oct 2, 2015 at 1:21am
@garen: please don't delete your posts after receiving help, it's incredibly rude to everyone who helped you and to anyone finding this via search.
Topic archived. No new replies allowed.