Did I do this properly?

I've recently been reading a book on Beginning C++ and came across an exercise in the book that it asks you to do. For this task, I had to create a program that asks for 3 Game Scores and finds the average of them. I've already created it and it works properly, I just want to make sure I'm writing the code properly and not putting things in places they shouldn't be.

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
27
28
29
30
31
#include "stdafx.h"
#include <iostream>
using namespace std;

int main()
{

	int gameScore1;
	int gameScore2;
	int gameScore3;


	cout << "Enter the score you received for your first game: \n";
	cin >> gameScore1;

	cout << "Enter the score you received for your second game: \n";
	cin >> gameScore2;

	cout << "Enter the score you received for your third game: \n";
	cin >> gameScore3;

	int gameTotal = (gameScore1 + gameScore2 + gameScore3);
	int scoreAverage = (gameTotal / 3);

	cout << "The average of your game scores is: " << (scoreAverage) << endl;

	
	return 0;


}


Thanks!
Your code is fine. And in fact it's very clean for beginner code. Nicely formatted and easy to read. I've seen much worse come out of people with more experience. Well done.

The only things I feel are worth noting are:

- You have unnecessary parenthesis on line 22, 23, and 25. It doesn't hurt to have them there... they're just not strictly necessary. Especially on line 25... having parenthesis around a single identifier (scoreAverage) is a little weird.


- stdafx.h is a weird MSVS thing. It has to do with precompiled headers. I'd recommend for future projects, you choose the "empty project" option in Visual Studio so it doesn't automatically add that.
1. The stdafx thing
2. Some of us prefer the extended "std::" instead of "using namespace std"
Disch - Thanks so much! I appreciate it. I'm hoping to continue my learning and major in Computer Science to become a Software Engineer for a gaming company.

EsseGeEich - Do you recommend always using "std::"? If it's more preferred I'd rather use it.
I only suggest it for what could be bigger projects.
If you're doing it for homeworks or similars, that's okay.
You can read more about that here:
http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

Here's a side effect of the "using namespace std":
http://stackoverflow.com/questions/2712076/how-to-use-an-iterator/2712125
Topic archived. No new replies allowed.