Public Group

# Cannot get average of three variables.

This topic is 4479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm a complete newbie to this and this would probably pass as the first program I'm writing without the assistance of a book. What I'm trying to do is get the mean of the variables "gameSCORE1", "gameSCORE2", and "gameSCORE3". The problem is right after I input the numbers for the gamescore variables the program automatically terminates. How can I get the program to work. Your help would be appreciated.
Quote:
 //Game Score Program #include #include using namespace std; int main() { int gameSCORE1; int gameSCORE2; int gameSCORE3; cout << "Please enter a number between 1 and 50... " << endl; cin >> gameSCORE1; cout << "Please enter a number betwen 51 and 100... " << endl; cin >> gameSCORE2; cout << "Please enter a number between 101 and 150..." << endl; cin >> gameSCORE3; int average = gameSCORE1 + gameSCORE2 + gameSCORE3 / 3; cout << "The average of these numbers is: " << average << endl; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; }

##### Share on other sites
Google for order of operations. C++ follows the same rules as general math does.

CM

##### Share on other sites
I think std::cin.get() would work to wait for a keypress.

Also the real error is your average calculation. C/C++ follows the math order of operators. Therefore gameSCORE1 + gameSCORE2 + gameSCORE3 / 3 will not be the average.

You add up gameSCORE1, gameSCORE2 and a third of gameSCORE3. Use brackets like this:

int iAverage = ( gameSCORE1 + gameSCORE2 + gameSCORE3 ) / 3;

##### Share on other sites
Even using the...
Quote:
 int iAverage = ( gameSCORE1 + gameSCORE2 + gameSCORE3 ) / 3;

The program still terminates. I'm starting to think it has something to do with the..
Quote:
 cin.ignore(cin.rdbuf()->in_avail() + 1);

code which I just copied from Beginning Cplusplus Game programming. Endurion, you said something about "cin.get()" how do I use it?

##### Share on other sites
Quote:
 Original post by SarxousEndurion, you said something about "cin.get()" how do I use it?

Try simply replacing your
cin.ignore(cin.rdbuf()->in_avail() + 1);
with
cin.get();

##### Share on other sites
Still no luck. Right after I input the third variable the command window just disappears.

##### Share on other sites
try 2

cin.get();
cin.get();

Also, try running your program straight from the command shell

##### Share on other sites
Hmm, try using the command prompt and run your programm manually. The problem is obviously just that the program closes too fast.

##### Share on other sites
Try this:

//Game Score Program#include <iostream>#include <string>using namespace std;int main(){int gameSCORE1 = 0;int gameSCORE2 = 0;int gameSCORE3 = 0;cout << "Please enter a number between 1 and 50... " << endl;cin >> gameSCORE1;cout << "Please enter a number betwen 51 and 100... " << endl;cin >> gameSCORE2;cout << "Please enter a number between 101 and 150..." << endl;cin >> gameSCORE3;float average = (gameSCORE1 + gameSCORE2 + gameSCORE3 )/ 3.0f;cout << "The average of these numbers is: " << (int)average << endl;cout << "Press enter to exit..." << endl;//cin.ignore(cin.rdbuf()->in_avail() + 1);char c[10];cin.read( &c[0], 2 );return 0;}

##### Share on other sites
If you are using Visual C++, just "Start without Debugging". Otherwise, you can either learn to use the command prompt to launch the program OR just add system("pause") at the end of the program. Other people will tell you not to use it and come up with a lot of reasons. Something about portability or standards-compliance or bad style. So don't use system("pause") if you are going to be rolling out your score-averaging example program on mulitple platforms.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 10
• 9
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
633690
• Total Posts
3013340
×