# Cannot get average of three variables.

## 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.

##### Share on other sites
Yeah, I think Endurion has it! I've stumbled on this problem in the past, and then realised it was just the program finishing normally and then closing down.

##### Share on other sites
Add a breakpoint on the last statement.

##### Share on other sites
Quote:
 Original post by ToohrVykAdd a breakpoint on the last statement.

That will not work in release version [smile] or from command prompt.

##### Share on other sites
Thanks for the help guys. Steamers modified code worked perfectly, as did Danikar's "double cin.get()" in the sake of simplicity I went with Danikar's method.

##### Share on other sites
There was actually three problems:
1. The program closing too fast
2. Order of operations
3. integer division

The first problem kept you from seeing the answer.
The other two problems would have given you the wrong answer.

Streamer's code fixed all three.

##### Share on other sites
Quote:
 Original post by Matt AppleStreamer's code fixed all three.

But introduced all manner of artifacts that are not part of the program proper, in order to fix the first.

I know, I know. It's "easier" this way. But I think learning how to deploy correctly pays off in the long run. Yeah, "you'll get to it eventually." Why not now, and save yourself all those unnecessary lines of code in the future? [smile]

##### Share on other sites
Quote:
 Original post by OluseyiBut introduced all manner of artifacts that are not part of the program proper, in order to fix the first.

Providing a better alternative is more productive than complaining about a sub-optimal solution.

##### Share on other sites
Ok maybe I'm crazy, or just such a beginner that I'm missing the bigger picture, but why not just use the system("PAUSE") function? I mean, it may not always be the answer but it works well for me and it also appears it is less work.

You dont need to use string if you use system("PAUSE"), but simply #include the stdlib.h

* edit sorry, missed the point of " char c[10];cin.read( &c[0], 2 ); " *

//Game Score Program#include <iostream>#include <stdlib.h> 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;cout << "The average of these numbers is: " << (int)average << endl;cout << "The program will now end." << endl;system ("PAUSE");return 0;}

-PB

##### Share on other sites
When you want to pause the output...

Couldn't one just do this instead.
char c;
cin >> c;

Or is this a really bad way to do it?

##### Share on other sites

-Linux does not have a pause command, so you've made an otherwise cross-platform program Windows-specific *just to pause the program*

-If somebody puts a file called pause.exe in your programs directory, that program will run instead.

-If somebody runs your program from the command line (the way command-line programs are meant to be run), the pause message will be very annoying.

-You fire up an instance of the cmd shell for no good reason.

There are probably other reasons too. Someone should make an FAQ entry on this...

##### Share on other sites
system("pause") isnt really less work if you just use a #define PAUSE (cin.get() or whatever you use to pause)

##### Share on other sites
Quote:
 Original post by SarxousThanks for the help guys. Steamers modified code worked perfectly, as did Danikar's "double cin.get()" in the sake of simplicity I went with Danikar's method.

in general the way to clear the input stream is (although i haven't used cin in a while ;)):

cin.clear(); //clears the fail flags
while(cin.get() != '\n') {} //clears the input stream up to the newline

##### Share on other sites
you need to add them in paranthesis and then divide by 3 :P

##### Share on other sites
Quote:
Original post by streamer
Quote:
 Original post by ToohrVykAdd a breakpoint on the last statement.

That will not work in release version [smile] or from command prompt.

If the program is launched from the command prompt, then there is no need for an additional cin.get(). The result is still displayed in the command window.

Moreover, adding a cin.get() (or whatever other "wait for user input" system) at the end of a console program is of no use. The only reason why you add it is because you launch the program using the IDE (which will close the window, so you can't see your result at the end of the program). Just launch the program from the command prompt and it will not disapear. In debug mode, one should add a breakpoint at the end of the program to control its ending.

Regards,

##### Share on other sites
OK, I think its pretty clear that the OP knows that for an official release, you shouldn't halt for input at the end. When score averager 1.0 is released, I'm sure he'll remove it.

In the mean time, halting for input may well be a more convenient alternative for debugging. The benefits of debugging from within the IDE should be obvious, so I have no idea why people even suggest running from the command prompt. Adding a breakpoint to the end causes your app to lose focus, which is highly annoying if you keep VSs window maximized. cin.get(); requires you to press enter. system("PAUSE"); is non-portable and can cause subtle bugs, but always continues on the first key pressed.

Choose the method that works best for you. If neccessary, wrap it in a _DEBUG block so it gets stripped from a release build.

CM

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628403
• Total Posts
2982477

• 9
• 10
• 9
• 21
• 24