Jump to content
  • Advertisement
Sign in to follow this  
Sarxous

Cannot get average of three variables.

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

If you intended to correct an error in the post then please contact us.

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

Share this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
Share on other sites
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();

Share this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!