Sign in to follow this  
Sarxous

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


Link to post
Share on other sites
Quote:
Original post by Matt Apple
Streamer'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 this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
But 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 this post


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


Link to post
Share on other sites
About system("pause"),

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


Link to post
Share on other sites
Quote:
Original post by Sarxous
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.


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


Link to post
Share on other sites
Quote:
Original post by streamer
Quote:
Original post by ToohrVyk
Add 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 this post


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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this