Cannot get average of three variables.

Started by
23 comments, last by Conner McCloud 17 years, 7 months ago
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 <iostream> #include <string> 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; }
Advertisement
Google for order of operations. C++ follows the same rules as general math does.

CM
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;

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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?
Quote:Original post by Sarxous
Endurion, 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();
Still no luck. Right after I input the third variable the command window just disappears.
try 2

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

Also, try running your program straight from the command shell
Hmm, try using the command prompt and run your programm manually. The problem is obviously just that the program closes too fast.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

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;}
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.
skulldrudgery--A tricky bit of toil

This topic is closed to new replies.

Advertisement