I am trying to do this C++ homework, my
second one, i am very new to this and have no
idea what is going on i would appreciate any
help. thank you. below is the homework
problem and what i have so far. i know that i
need to create a loop for prompting the user to
enter a random number and the program is
suppose to prompt the user until -999 is
entered at that point the program will compute
the sum, count, average, smallest and largest
number entered values.
So far i have this:

``````int main()
{
// Identifying Variables

int sum, count, average,
smallestnumber, largestnumber;
int x=0;
while(x!=-999);
x++;
cout <<" enter a number (-999 to quit)";
cin >> x;

return 0;
}``````

but this is just returning a never ending loop of
enter a number(-999 to quit) without prompting
the user to input any value.

## All 31 Replies

>>while(x!=-999);
Remove the semicolon at the end of that line.

Next, when there are multiple statements in a loop the loop must be enclosed in { and } brackets

``````while(x!=-999)
{
// blabla
}``````

Also, you will need to initialize all those variables to -0 so that they do not contain just random values. For example `int sum = 0;`

``````while(x!=-999);  //this semicolon ends your while right there
x++;                  //without the semicolon only this would be part of the
//while loop
cout <<" enter a number (-999 to quit)";
cin >> x;``````

Solution:

``````while(x!=-999)
{
x++;
cout <<" enter a number (-999 to quit)";
cin >> x;
}``````

EDIT: AD beat me to it lol

thanks it worked. now i just needed to know how do i go about stopping the program if the user inputs -999 do i need and if else statement or can i change something in the loop that will make it stop if x=-999. secondly how do i create a function to calculate the sum, average, count, smallest and largest numbers entered?
thank you

thanks it worked. now i just needed to know how do i go about stopping the program if the user inputs -999 do i need and if else statement or can i change something in the loop that will make it stop if x=-999.

Look at that while statement -- when someone changes the value of x to -999 then the while statement will stop and program execution will jump to the next line after the end of the while loop.

secondly how do i create a function to calculate the sum, average, count, smallest and largest numbers entered?
thank you

Its not a function. Just keep track of the sum, count, smallest and largest numbers inside the loop, just after the `cin >> x;` line. The average is calculated after the loop finishes, which is `average = (float)sum / count;`

Also, you don't need that `x++;` line -- just delete it.

OK now i have something like this:

``````int sum=0;
int count=0;
int average=0;
int smallestnumber=0;
int largestnumber=0;
int x=0;
while(x!=-999)
{

cout <<" Enter a number (-999 to quit)";
cin >> x;

int sum(x);
cout << "sum of the numbers is"<< sum;

}``````

as you mentioned to keep track of the sum, count etc after the cin>>x; line, now it is not going with the loop it just says enter a number and displays the sum of that number. now if i put closed braces after the cin>>x line and open braces before the int sum(x) line then i am back to where i started because then it is in the same loop of asking the user to input a number until -999 is entered and then the program quits and doesn't go back to calculating the sum.

Ok, first off, please read this before posting code again:
http://www.daniweb.com/forums/misc-explaincode.html

Second, you need to accumulate the numbers that are entered by the user. Depending on what you have learned so far, you can either use an accumulator (the numbers entered by the user are added to this variable with each iteration of the loop) or you could use an array, then add all the elements in the array, get the average, etc...

>>int sum(x);

That is just initializing a variable named sum to be the same as the value of variable x. That is not what you want. sum needs to be the total of all previous values plus the value of variable x. sum was already declared at the beginning of your program so you don't have to declare it again.

That line should be like this: `sum = sum + x;` , or another way of putting it `sum += x;` . Either way is correct, so you can choose either way of coding it.

``````int sum=0;
int count=0;
int average=0;
int smallestnumber=0;
int largestnumber=0;
int x=0;
while(x!=-999)
{

cout <<" Enter a number (-999 to quit)";
cin >> x;
}
{
sum = sum + x;
cout << "sum of the numbers is"<< sum;

}``````

ok i had figured out the sum= sum+ x but now it just doesn't go with the loop anymore it asks to enter a number and then displays the sum of the number. i want it to prompt the user for an infinite amount of times to enter any number and when -999 is entered then display the sum, count and average etc of all the numbers entered.

it just doesn't go with the loop anymore

Sure it does. You want to keep a running total, so you want to add into your sum during each cycle. Trace it out by hand to convince yourself (which is a good thing to do with any programming problem).

1. sum = 0, you enter 10, sum now = old value of sum + x = 0 + 10 = 10
2. next cycle sum = 10, you enter 25, sum now = 10+25 = 35
So it's the loop that keeps adding that new x value on.

Also, please use the code tags to surround any code that you post:

i give up. just emailed my instructor, probably turn this assignment late. program not doing what is being asked. find the original problem below. thanks for all your help.

Develop a program to find the average of a sequence of integers entered by the user.
The program will continue to prompt the user for numbers until -999 is entered.
After -999 is entered to indicate that all of the values have been entered,
display the sum, count, the smallest number entered, the largest number entered,
and the average of all the numbers entered. Use these numbers for testing: 10 4 6 11 15 23 2 18
As always, submit your source code and a copy of the output. Since you may only submit a
single file to COL, add the two files to a zip file and submit that.
Here is an example of what your execution should look like:
Enter a number (-999 to quit): 10
Enter a number (-999 to quit): 4
Enter a number (-999 to quit): 6
Enter a number (-999 to quit): 11
Enter a number (-999 to quit): 15
Enter a number (-999 to quit): 23
Enter a number (-999 to quit): 2
Enter a number (-999 to quit): 18
Enter a number (-999 to quit): -999
The sum of your numbers is: ddddd
The count of numbers you entered is: ddd
The average of your numbers is: dddd.dddd
The largest numnber you entered is: dddd
The smallest number you entered is: dddd

You aren't that far away from it, unless its due in like 30 minutes, but ok suit yourself.

it is due tomorrow at 5:45pm. the program doesn't even get to the sum part it just quits when i enter -999.

Did you move the sum into the loop like I was saying? If so, print it out after the loop is finished. Boom that's done. You've got count I think, right. Then you've got average. Keep putting couts in your code in places where you want to check values.

well if it is in the loop like this: then if i input say 5 it would say the sum of the number is 5 and it would say enter a number, if i enter 5 it would give me 10. however that is not what the problem is asking the program to do it is asking the program to first prompt the user to input any amount of numbers and then once the user inputs -999 then the program should terminate and give the user the sum of the numbers entered, average, count etc.

``````int sum=0;
int count=0;
int average=0;
int smallestnumber=0;
int largestnumber=0;
int x=0;
while(x!=-999)
{

cout <<" Enter a number (-999 to quit)";
cin >> x;

sum = sum + x;
cout << "sum of the numbers is"<< sum;

}

return 0;

}``````

once the user inputs -999 then the program should terminate and give the user the sum of the numbers entered, average, count etc.

The loop should terminate and it should _display_ the information. Nothing says it has to be calculated at that point. In fact it would be ludicrous to ask you to get all the sums right at that point without having an array in which to store numbers. You're doing the right thing.
Now increment your counter variable within your loop.

I would move line 18 down after line 20 so that the sum is only printed after you enter -999. That's where you calculate and print the average too.

So I am doing this problem also. When I display the average, it keeps coming out as an integer when I need a decimal. Here is what I have (it is only the end of the code, and I haven't touched it up yet, I am just trying to get all the calculations to be correct).

``````if (digit==-999);
{double average=(sum+999)/(count-1);
cout<<sum+999;
cout<<count-1;
cout<<average;
}``````

I was going to not even make a variable called average, but when I tried to display the "sum" divided by the "count" it came out as an integer, so I tried what I have pasted in code above, and the average still comes out as an integer. Any suggestions?

You have to cast either the numerator or the denominator (or both) to a double otherwise it does the integer arithmetic first and stores it in the double. `double average=((double)(sum+999))/(count-1);` I put some extra parens on there just to be sure.

Yeah that worked, thanks. Now the hard part is displaying a max and min value, I have a feeling I might have screwed myself over by recognizing -999 as an integer being entered and adding 999 to the sum.

First think of how you would find the minimum and maximum values without the -999 present and then figure out how to exclude it (-999) from your scheme to get the max and min.

Think if you were walking along a list of numbers and you could only carry one in each hand, how would you keep track of max and min?

well if it is in the loop like this: then if i input say 5 it would say the sum of the number is 5 and it would say enter a number, if i enter 5 it would give me 10. however that is not what the problem is asking the program to do it is asking the program to first prompt the user to input any amount of numbers and then once the user inputs -999 then the program should terminate and give the user the sum of the numbers entered, average, count etc.

``````int sum=0;
int count=0;
int average=0;
int smallestnumber=0;
int largestnumber=0;
int x=0;
while(x!=-999)
{

cout <<" Enter a number (-999 to quit)";
cin >> x;

sum = sum + x;
cout << "sum of the numbers is"<< sum;

}

return 0;

}``````

I realize this might be too late, but.

Don't give up. For a beginner, logic can be tough, you just need to keep at it. Like anything else it gets easier to understand the more you do it, so practice. That's why you have homework, it's guided practice. All you have to do is think about how you would do something, then explain it to the computer. It's like translating your thoughts from English (or whatever your native tongue may be) to something else, like Spanish, only you're translating to computer speak (in this case, C++).

All that aside:
Just because you are generating the sum within the loop it doesn't mean you need to display it within the loop...

Within a block of code you can do as much, or as little, as you like as long as it makes logical sense (and meets the program specifications/requirements). For your situation, it's a simple matter of moving 1-line out of the loop's scope so that it executes at a later time (after the loop completes) instead of during each iteration. Take another look at AD's post for more info.

okay. thanks for all your guys help. i figured everything else out except how to find the count of the number of values inputed by the use. can someone please help me with that. thank you

Increment your count variable inside of your while loop (with `count++;` ). Each time the loop goes around the counter gets pushed up by 1. Just remember what value count started out as to get an accurate reading.

Everything is working for except i don't need -999 to be part of those calculations like the sum, average, count etc. so how do i exclude -999 from these calculations. -999 should just be there when the user inputs all the numbers and it should do calculations of all the numbers before it excluding itself.

``````#include <iostream>
using namespace std;

int main()
{

int sum=0;
int count=0;
int average=0;
int smallestnumber=0;
int largestnumber=0;
int x=0;

while(x!=-999)
{
cout <<" Enter a number (-999 to quit)";
cin >> x;
sum=sum + x;
if (count==0){
largestnumber = x;
smallestnumber = x;
}
if (x > largestnumber){
largestnumber = x;
}
if (x < smallestnumber){
smallestnumber = x;
}
count++;

}

cout << "The sum of the vlaues entereed is" << sum << endl;
cout << "The number of values entered is" << count << endl;
average = sum/count;
cout << "The average of values entered is" << average << endl;
cout << "The maximum value entered is" << largestnumber << endl;
cout << "The minimum value entered is" << smallestnumber << endl;

return 0;

}``````

Add another `if()` and enclose your 3 case analysis ifs and count within it. That way you can completely bypass them if necessary.

``````if( /* check the input value against -999*/) {
/* case 1 */
if (){
}

/* case 2 */
if() {
}

/* case 3 */
if() {
}

/* accumulate your count */
}``````

Add it back in again to your sum. You'll need to make one small change to your if statement (the one to find the minimum) so that it doesn't get -999 each time. `average = sum/count;` Also see post #20 ^^^^ why this is going to be problematic.

EDIT: Fbody's approach will do too but it doesn't affect the last concern about average = sum/count;

Add it back in again to your sum. You'll need to make one small change to your if statement (the one to find the minimum) so that it doesn't get -999 each time. `average = sum/count;` Also see post #20 ^^^^ why this is going to be problematic.

EDIT: Fbody's approach will do too but it doesn't affect the last concern about average = sum/count;

Was editing when you posted, please see revised ^^^ :)

Yeah, no that's what i saw. It's probably a cleaner approach. It's all good. I was just warning OP about the cast.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.