Hi im pretty new to c++ and I was hoping you guys could review this code I wrote.
its a game that makes the user guess a randomly generated number and then you have x amount of times to guess right. been using this to practice classes and keeping main() clean. Also I know the pointer is unnecessary, I put it there for practicing passing by reference.
heres the header
rand.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Class declarations
#ifndef RAND_H
#define RAND_H
#include <string>
class game
{
public:
int randGen(int seed, int maxNum, int choice);
int randGame(int setting, int num); // if returns 1 call win() if 2 call lose()
int challengeSet(int *hardness); // returns chalenge level to main
void win();
void lose();
};
#endif
#include <iostream>
#include <string>
#include "rand.h"
#include "randGen.h"
usingnamespace std;
int difficulty; // for difficulty setting
int finalDiff;
int winOrLose;
int main()
{
int cont;
int seedNum = 0;
int maxNumber = 99;
int choice;
int rNumber;
game randG;
cout << "You will guess a number from 1-???" << endl;
cout << "Press any key to begin.........";
cin >> cont;
cout << "Firstly would you like to seed a number, or just use the regular algoritham?" << // choice to seed a number
endl << "press 1 or 2 respectively" << endl;
cin >> choice;
if(choice == 1)
{
cout << "Enter the seed number you would like to use: "; // seed num
cin >> seedNum;
}
do
{
cout << endl << "Now enter the maximum number you want to guess to (minimum is 100): "; // max number to generate
cin >> maxNumber;
}
while (maxNumber < 100);
rNumber = randG.randGen(seedNum, maxNumber, choice);
cout << endl << "Please make a choice" << endl <<
"1. For Easy." << endl <<
"2. For Medium" << endl <<
"3. For Hard.";
finalDiff = randG.challengeSet(&difficulty);
winOrLose = randG.randGame(finalDiff, rNumber);
if (winOrLose == 1)
{
randG.win();
}
else
{
randG.lose();
}
system("pause");
return 0;
}
Your commenting....ish awful! ;) Write a comment about what each file does at the top of each file.
Try and summarise any blocks of code as well while you're at it. Perhaps put a comment next to the includes to explain what they do (the none standard ones I mean). Also explain your variables, what do they do?
Can you put your variables any nearer to where they are going to be used?
Line 28 RandMain.cpp use \n instead of endl, use endl to flush once you have finished with the text.
Line 29 shouldn't cin >> choice be one column to the left.
I assume you're not worried about pedantry when creating a thread like this. I'll take a few passes at it and point out some things.
First, consistency. Ideally, the header file would be named the same as your implementation file (with the only difference being the file extension, naturally). Also, it is a good habit to name the header and implementation after the class whose code it contains.
Second, naming. This isn't FORTRAN, you have plenty of room to give your classes, functions, and variables descriptive names. Instead of class game, say something like class GuessingGame. Rename your .h and .cpp accordingly.
Third, whitespace. Try to use spaces instead of tabs. Not everyone's tab widths are the same and it can make for some funky indentation if you move the code around, especially if you're mixing in spaces.
Fourth, stay away from system("pause");. There is an entire thread dedicated to this stickied to the first page of the Beginners forum.
Fifth, try to avoid using namespace std; I think it would be better if you called out using std::cout; using std::cin; and using std::endl; There may be name collisions when you pull in the entire namespace (be especially careful with the word rand).
Lol I know the commenting is pretty bad Ravenshade, I'm still working on good commenting style.
Thanks booradley, I'll edit my code a bit, and get into these habits for future programs.
About whitespaced: there is good rule "Ident with tabs, align with spaces". That means that you will press tab several time at the beginning of line depending on identation level and will not use them again till the end of the line. e.g:
Using tabs to ident and spaces to align here. Tabs in this forum are 8-spaces by default
1 2 3 4
{
std::cout << "Hello " <<
"world";
}
(Using spaces to emulate 4-space tabs) Note how code stays aligned after switching to 4-space tabs.
1 2 3 4
{
std::cout << "Hello " <<
"world";
}
In next snipped I have used 8-space tabs (and some spaces) to align. Notice, how it is magled after switching to 4-space tabs:
1 2 3 4
{
std::cout << "Hello " <<
"world";
}
Ident style isn't consistent too. Choose one ( en.wikipedia.org/wiki/Indent_style ) and stick to it (I suggest K&R).
Try to stay away from global variables. I don't see, why you used it here.
You shouldn't `srand()' at each number generation.
Just once in the whole program should be enough.
Another thing is that all generators will share the seed.
You can use your object state.
In `challengeSet()', ¿what would be the returned value if you put an invalid choice?
should loop instead of recurse there.