Storing multiple input inside a variable with out an array or vector.

Write a complete C++ program that reads in a list of scores until the user enters a negative number. It will add all the given scores up and subtract the biggest and smallest scores to get a true score total.

I am honestly so lost on the part that it requires you to store multiple inputs inside possible one variable. I know that in C++ you can only have one thing stored into one variable.

Note: please do not use array or vector. And please write it in pseudo code if possible(I want to understand whats going on and write the final codes myself)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
using namespace std;

int main()
{
	int score, total;
	for (int counter = 0; score > -1; counter++)
	{
		cout << "Please enter non-negative score:";
		cin >> score;

		if (score < 0) 
		{
			break;
		}
		total = score + score;
	}
	cout << "true total score is: " << total << endl;
	return 0;
}
Hi,

First, initialize your total to 0 : int total(0);

Second, replace your line 16 by total+=score; if you want to get the sum of all your scores.

Third, to keep in memory your smallest and biggest scores, int maxS and minS. On the first iteration, give them the value of the score.
On the other iterations, if score<minS, then minS=score (and do the opposit for maxS).

After all this, if the input is :
2 5 1 4 6 3
You should obtain :
total=21
minS=1
maxS=6

trueTotal=14

Is this what you're expecting ?
It runs partially, the program does not reject the max and min for the variable, so it only add up all the numbers




#include <iostream>
using namespace std;

int main()
{
int score;
int total, TrueTotal, Max, Min;
total = 0;
TrueTotal = 0;
for (int counter = 0; score > -1; counter++)
{
cout << "Please enter non-negative score:";
cin >> score;

if (score < 0)
{
break;
}
Max = score;
if (score > Max)
{
Max = score;
}
Min = score;
if (score < Min)
{
Min = score;
}
total +=score;
TrueTotal = total - (Max + Min);
}
cout << "true total score is: " << total << endl;
return 0;
}
Last edited on
EDIT: in case of confusion, this is all referring to the initial post without edit or subsequent posts!!!

You are currently (nearly correctly) keeping a running tally of the total. You forgot to initialise it (to 0), however.

Also keep a running tally of the minimum and maximum values entered. You can subtract them from the total at the end. @Zaap's explanation is fine for this and he/she got their post in first.


BTW, it's not crucial to the operation of your code - just strange - that:
(1) you are doing tests for continuation both as the central item in your for-loop statement and as a test leading to break;
(2) you are incrementing a counter that you don't use and, indeed, couldn't use outside the for-loop, as its scope is confined to that loop; in anticipation of what your next question might well be I would be inclined to fix this.
Last edited on
1
2
3
4
5
Max = score;
if (score > Max)
{
    Max = score;
}

Here (and for the Min case too) you set your Max to score before the test, so, even if score<Max, you set Max=score;

What you should do is :
1
2
3
4
5
6
7
8
9
10
11
bool firstTime(true);
for(condition){
    if(firstTime){
        Max=score;
        Min=score;
        firstTime=false;
    } else{
         if(score<Min) Min=score;
         if(score>Max) Max=score;
    }
}


Another strat that I would recommend here is to modify your loop like this :

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

int main()
{
	int score, total(0);
        cout << "Please enter non-negative score:";
	cin >> score;
        Min=score;
        Max=score;
	while(score>-1)
	{
               if(Min>score) Min=score;
               if(Max<score) Max=score;
		total+=score;
 		cout << "Please enter non-negative score:";
		cin >> score;
	}
        total-=(Max+Min);
 	cout << "true total score is: " << total << endl;
	return 0;
}


NOTE : if there is something you don't understand in my code, ask me.

NOTE 2 : if the first score entered by the user is less than 0, it does s***
Last edited on
THANK YOU so much, it works now. I have learned form this, thanks again.


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>
using namespace std;
int main()
{
	int score, total, TrueTotal, Max, Min;
	total = 0;
	TrueTotal = 0;
	score = 0;
	bool firsttime;
  cout << "Please enter non-negative score:";
	cin >> score;
	while (score > -1)
	{
		cout << "Please enter non-negative score:";
		cin >> score;

		if (score < 0) 
		{
			break;
		}

		if (firsttime)
		{
			Max = score;
			Min = score;
			firsttime = false;
		}

		else
		{
			if (score < Min) 
				Min = score;
			if (score > Max) 
				Max = score;
		}
		total +=score;
		TrueTotal = total - (Max + Min);
	}
	cout << "true total score is: " << total << endl;
	return 0;
}
Don't forget to set firstTime to true.

You can also use constructor to set a default value to your variable, it's better than int a=3 :

int a(3); //a has 3 as value
bool b(true); //b has true as value
Topic archived. No new replies allowed.