[Request] Code Review + Some Questions

Cheers everyone, Newbie on C++ board!

I'm a beginner with C++, currently working through a C++ beginners book teaching game programming which also contains small review exercises. This is the first exercise i did - and - yay! it works! But is it also good code?

- Is it good or bad to declare variables in the way i did? Or should i rather write a line for every single int variable instead?

- Should i rather write a separate line for calculations before they are being used or is it O.k. the way i did in my code?

Everything works and there are no compiler errors. I just cannot help but feel that doing it this way could potentially lead to problems in bigger projects. In case of this exercise the variables should remain uninitialized until user input and subsequent calculation of final score and average score takes place.

I just can't help but feel that just because it works in this specific case it could lead to trouble in upcoming bigger projects. Of course i would write extra lines when i know that some variable or whatever does need to hold a constant value. But there might also be cases coming up where i would declare variables and then use them all over the place, only to find that they might need a constant value - what then? What would happen in such cases?

-

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
  // gameScores.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>

using namespace std;

int main()
{	
	int finalScore, avgScore, pl1, pl2, pl3;

	cout << "Please enter the score player 1 achieved:" << endl;
	cin >> pl1;
	cout << "Now enter the score player 2 achieved: " << endl;
	cin >> pl2;
	cout << "Finally enter the score player 3 achieved: " << endl;
	cin >> pl3;

	cout << "The final score off all three player is: " << (finalScore = pl1 + pl2 + pl3) << endl;

	cout << "The average score off all three players equals: " << (avgScore = finalScore / 3) << "." << endl;

    return 0;
}


Thanks for taking your time to read and hopefully shed some light on this newbies coding adventure darkness!

Regards
Misenna
Anytime you start adding numbers to the end of variables you should consider using a vector or an array instead. In this simple program though you really only need one "score" variable, if you total them as you retrieve them from the user.

Also I don't recommend doing calculations in your print statements. Keep your calculations as separate lines.

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
#include <iostream>

using namespace std;

int main()
{	
   const int NUM_SCORES = 3;
   int totalScore = 0, avgScore = 0;
   int score;

   for(int i = 0; i < NUM_SCORES; ++i)
   {
      cout << "Please enter the score player " << i + 1 << " achieved: ";
      cin >> score;
      totalScore += score;
   }
   
   // Don't forget integers don't have fractions. 1/2 yields zero.
   avgScore = totalScore / 3;
   
   cout << "The final score off all three player is: " << totalScore  << endl;
   cout << "The average score off all three players equals: " << avgScore  << "." << endl;

    return 0;
}


As shown I'd also initialize the variables not retrieved from the user.
Thank you very much for both your explanation & code!

I was trying to come up with a way to only just use score once instead of p1, p2 ... and failed. I have yet to learn about arrays, loops and vector operations. Your code is (thankfully) clear and basic enough for me to easily understand how this works and what each statement means. I really learned something and will certainly keep your suggestions in mind.

Only just one more question: Why initialize something with a value of 0 in C++? Is it not so that C++ assigns a value of 0 if no other value is assigned? At least i think i read something along these lines in my book but with little explanation or pros/cons for/against doing it. Webwise opinion is divided - some say this leads to errors of some sort, others say it makes code faster, for me all that counts is to be aware of pitfalls and to avoid them. (Best way - solid foundation - and asking many such stupid questions).

Regards
Misenna

Unless you initialize a variable with a value, its value is undefined, and attempting to reference that value is undefined behavior.
Topic archived. No new replies allowed.