Why isn't my program working?

Every time i try to run it, it just says windows is fixing the problem or something like that. I'm trying to get the user to ask for numbers until the average of the numbers is greater than 250. Here is my program. All help is appreciated. Thanks.

#include <iostream>

using namespace std;

main() {
int num;
int total = 0;
int count = 0;
int average;

cout << "Enter number: ";
cin >> num;

while (total + num > 250){
total += num;
count++;
cout << "Enter number: ";
cin >> num;
}

average = total / count;

cout << count << " numbers were read in. " << count << "The average is: " <<
average << endl;
}
did you mean total + num < 250?
I modified your program a little:

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
#include "StdAfx.h"
#include <iostream>

using namespace std;

int main() {
	
int num;
int total = 0;
int count = 0;
int average;

cout << "Enter number: ";
cin >> num;
total += num;

while ((total + num) < 250) {
	cin >> num;
	total += num;
	count++;
}

	average = total / count;

	cout << count << " numbers were read in. The average is: " <<
	average << endl;

return 0;
}


First, you forgot to add an int to main (it should be "int main()" not just "main"). You also forgot to add "return 0;" at the end. Your while loop should have said:

while ((total + num) < 250) {

The "<" was placed in the wrong direction. You should also have total + num in ()'s (don't remember what their called). You also don't need to display "Enter number:" twice, though it's not wrong. I also removed the second "count" when the average is displayed, because it seemed kind of odd that you would want that to be there?
Last edited on
To be fair to the OP, if "void main()" were still exceptable, which it IS NOT, then he wouldn't need a "return" anything at the end of his program.

It's also good practice to sync the "std::cin" buffer if you're going to use it in any type of loop like that, although Visual Studio might do that for you by default.
AFAIK it's ok to ommit "return 0;" from main(). I'm not sure whether this is a guarantee from the standard or just something most compilers implemented.
Stupebrett,

I tried your way of doing it and when i compiled it, after enter a number, the program just ended. Why might that be?
http://www.cplusplus.com/forum/beginner/1988/

Please read the whole thing.
I use getche(); at the end of the program to make console stay opened.Use it likes this:

1
2
3
4
5
6
7
8
9
10
11
#include <conio.h>

int main()
{
//..........
//..........your code
//.........
//.........
getche();
return 0;
}


I think its ok for learning purposes,as I do.
Last edited on
I think its ok for learning purposes,as I do.

No it's not. There is a perfectly portable (well, maybe a bit of an overstatement. But it will work on most platforms where it actually makes sense) way that does pretty much the same thing, with the difference that it's standard c++ and only requires the <iostream> header.

1
2
3
4
5
6
7
8
#include <iostream>
//stuff
int main(int argc, char** argv)
{
    //stuff
    cin.ignore(cin.rdbuf()->in_avail()+1);
    return 0;
}
You don't need to use the entire std namespace, when it looks like just cin and cout will do fine.
using std::cin; using std::cout;

main() should return an int, and that int should be 0:
1
2
3
4
5
int main()
{
    //code
    return 0;
}


Why do you initialize some variables but not all? Initialize them all to 0:
1
2
3
4
int num=0;
int total=0;
int count=0;
int average=0;


This line from the OP:
 
while (total + num > 250){

Tests the current total, not the average, and loops only if the total is GREATER than 250.

This line from the first response:
 
while ((total + num) < 250) {

Still only tests the current total, not the average, though it corrects the > to <.

To ensure that your while loop executes at least once, make it a do...while loop, make it test the average, and make it loop if the average is less than 250:
1
2
3
4
5
6
7
8
do {
    cout << "Enter number: ";
    cin >> num;

    total+=num;
    count++;
    average = total/count;
} while(average<250);


You also have an extra "<< count <<" in your final cout statement:
 
cout << count << " numbers were read in. " << count << "The average is: " <<... 

should be changed to:
 
cout << count << " numbers were read in.  The average is: " << average...


Also, did you want your 250 cap to be a hard cap (meaning the final average MUST be less than 250) or a soft cap (meaning the final average can be the amount that meets or breaks 250)? If you want a hard cap, you would have to add a test that would prevent values from changing if the new average goes over 250.
 
if(average>250) { total-=num; count--; average = total/count;  break; }

This line undoes the last set of changes and exits the loop with the previous values for total, count, and average. This line could be added to the end of the while loop to implement a hard cap, or you can run the test earlier:
1
2
3
4
5
6
7
8
9
do {
    cout << "Enter number: ";
    cin >> num;

    if( (total+num / count+1) > 250 ) { break; }
    total+=num;
    count++;
    average = total/count;
} while(average<250);

This will pretest the math and break the loop before any values are changed. Optionally, you could make this code much cleaner by just using a function that takes and returns values and encapsulates the arithmetic and tests within it:
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
#include <iostream>
using std::cin;    using std::cout;
bool Calculate(int&,int&,int&);

int main() {
    int total=0, count=0, average=0;
    
    for(bool done=false;!done;) {
        done = Calculate(total,count,average);
    }
    cout << count << " numbers were read in.  The average is: " << average << ".n";
    return 0;
}

bool Calculate(int& total, int& count, int& average) {
     int num=0, tempTotal=total, tempCount=count, tempAverage=average;
     
     cout << "Enter number: ";
     cin >> num;
     
     tempTotal+=num;
     tempCount++;
     tempAverage=tempTotal/tempCount;
     
     if(tempAverage>250) { return true; }
     else {
          total=tempTotal;
          count=tempCount;
          average=tempAverage;
          return false;
     }
}
Last edited on
Topic archived. No new replies allowed.