Beginning C++ Through Game Programming Third Edition

Hey all, so in this ebook, end of chapter one, theres an exercise of the following:

3. Write a program that gets three game scores from the user and displays
the average.

so this is what i'm doing, and i've got everything down, BUT.
everytime it gets to the final, uhm, status i guess?

it closes. it's not only a problem with this, but every example that requires input.

this is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	int number1, number2, number3, totalaverage; //Defining the integers for input
	cout << "Welcome to my Exercise!\n";
	cout << "Please enter a random number: \n";
	cin >> number1;
	cout <<"\nPlease enter another random number: \n";
	cin >> number2;
	cout <<"\nPlease enter your final number: \n";
	cin >> number3;

	totalaverage = number1 + number2 + number3;

	cout << "Your total average is: " << totalaverage;
	cin.get();
	return 0;
}


so what happens is, after entering the final number (number3) the program closes, i added cin.get(); there, but it doesnt do anything, so whats going on? :/ this isnt the only code that does it, even the ebooks lost fortune example closes after i enter my last name. thankyou!
Well, first problem here (not related to your question) is that you are not finding the average. You found the sum. In case you don't know, average is the sum of all the elements divided by the number of elements. So you need to divide number1, numbe2, and number3 by 3, because you have 3 numbers. And for your problem, what IDE are you using?
cin>> leaves newlines in the stream, so the final cin.get(); simply gets the newline that's been left there.

One way to ensure this doesn't happen is to #include <limits> and to call

cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

after each call to cin>>. What this does is it goes through the stream and it deletes the first newline it sees. The std::numeric_limits<std::streamsize>::max() is to ensure that it can find the newline despite large input.

Another thing you can do is to use getline to get all the numbers as strings, and then use a stringstream to convert each string to an integer. getline uses '\n' as the default delimiter, so it doesn't cause newlines to be left in the stream.

http://www.cplusplus.com/reference/string/getline/
http://www.cplusplus.com/reference/iostream/stringstream/
@ResidentBiscuit; im currently trying to figure out how to display the divided by, i tried

totalaverage = number1 + number2 + number3 / 3;

but if i do 4 + 4 + 4 it gives me 9. which isnt the average.
and im using Microsoft Visual C++ 2010

@shacktar your post went right over my head, i didnt know a word of what your talking, i started c++ yesterday and am only on chapter 1.

the system("PAUSE"); works, but been told its bad, all i want is a simple way for the program to stop/pause where its supposed to display the total and then wait till closed :/
Joel Male.. To get totalaverage, the line should read

totalaverage = ( number1+number2+number3)/3

Without the parentheses, only number3 is divided by 3, and then added to the other numbers.

I would also declare totalaverage as a float, since the division most likely would not result in a integer number, but with one that has a decimal.
Ok in VC you can go Tools > Settings > Expert settings. This will allow you to do "Run without debugging" Or something a long those lines. That will keep your program running. Until you get a better understanding of c++ and programming in general, just stick to this for keeping your program up. Don't use system anything. There is a good reason behind it, but you can learn that later.

With the average, if you do it like that it will not work due to order or precedence. The computer will run that with number3/3 first, which won't do what you want. What you'll want to do is this:

totalaverage = (number1 + number2 + number3) / 3
@whitenite1 thank you! i knew it was the correct symbol, just forgot about the precedence, and i added

"float totalaverage;" and took it out as an int, i hope thats correct, or would it be double totalaverage;?

@ResidentBiscuit i did that, clicked run without debugging and for some reason, now nothing works, it opens and just says Click any key to continue, showing nothing of my exercise, the new code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
#include <string>

using std::cout;
using std::cin;
using std::endl;
using std::string;

int main()
{
	int number1, number2, number3; //Defining the integers for input
	float totalaverage;
	cout << "Welcome to my Exercise!\n";
	cout << "Please enter a random number: \n";
	cin >> number1;
	cout <<"\nPlease enter another random number: \n";
	cin >> number2;
	cout <<"\nPlease enter your final number: \n";
	cin >> number3;

	totalaverage = ( number1+number2+number3) / 3;

	cout << "Your total average is: " << totalaverage;
	return 0;
}


i dont mean to ask for more help, i just cant work out why its not following the int main() function, im not getting any errors nor warnings :/, any idea?
You won't really NEED double for this. I use double for for any variable with decimals, but it's not really needed in most cases. I just ran it in my VC++ 2010, ran fine. Is it the program built on your end?

EDIT:
Ok, so i don't really use VC++ often so I didn't catch this earlier. You're running a CLR Console app aren't you? You should move this to Win32 Console app projects. Just go to new project >> Win32 console application. After that, it's gonna bring up a new project wizard, make sure to have console application checked and empty project checked. Don't use the precompiled header, that will cause you problems right now
Last edited on
@ResidentBiscuit ahh, yes, it worked, not sure what happened, maybe the change in the settings, it wasnt a CLR Console though, that i know of, fairly sure i wouldve clicked Win32 console, but i created a new project, copy and pasted and it worked, i guess ill stick to run without debugging till i learn more c++ and learn how it all works.

i really appreciate your help sir!
No problem. Best of luck towards you
Add cin.sync(); before cin.get();
Topic archived. No new replies allowed.