Criticism

Hello All,

I am currently learning C++ after about a year of Java and would like some criticism (try not to be to harsh :) ). I wrote a small random guessing game for the command line. Here is the source, also the if statement in the check answer function could probably be shortened anyone know how to do that?

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <iostream>
#include <string>

using namespace std;

void won() {
	cout << "Congradulations You have won!!!" << endl << "Do you want to play again?> ";
	string pg;
	cin >> pg;
	if (pg != "y") {
		exit(0);
	}
}
	

bool check_ans(int gu, int c) {
	if (gu <= c + 3 && gu >= c - 3) {
		return true;
	}else if (gu == 51) { //leave the game
		cout << "\nBye!!!\n";
		exit(0);
	} else {
		 if (gu > c) {
			cout << "You went too high\n";
		} else {
			cout << "You are too low!\n";
		}
		return false;
	}
}

int main() {
	int g; //guess variable
	bool hasWon = false;
	srand(time(NULL)); //start the random seed
	cout << "Welcome to the random game" << endl << "you have three guess's at the correct answer" << endl; //print opening
	cout << "Your answer must be within three of the correct answer" << endl << "The answer will range from 1 to 50\n"; //print opening (still)
	while (!hasWon) {
		int ran= rand() % 50 + 1; //initialize the random number
		cout<< ran << "\nYour first guess> ";
		cin >> g;
		hasWon = check_ans(g, ran);
		if (hasWon) {
			won();
			hasWon = false;
			continue;
		}
		cout << "Your second guess> ";
		cin >> g;
		hasWon = check_ans(g, ran);
		if (hasWon) {
			won();
			hasWon = false;
			continue;
		}
		cout << "Your third guess> ";
		cin >> g;
		hasWon = check_ans(g, ran);
		if (hasWon) {
			won();
			hasWon = false;
			continue;
		}
		
		cout << "The correct answer was " << ran << endl;
	}
	return 0;
}
Last edited on
You have essentially copy-pasted the same block of code (lines 28-41) three times. This should suggest that a loop would be more appropriate.

abs( x - y ) <= 3

is true if x and y are within 3 of each other.
You don't need to check every case manually:
if (g <= c + 3 && g >= c - 3)

You have a while loop, which I agree might be useful, but you aren't using it at all right now. Notice that your code to ask for input and congradulate (or not) is almost exactly the same each time. A while loop, or a do-while loop would be useful to save having to type the code three times. Try to solve the problem, if you gave the user an unlimited number of guesses.
Ok thank you all for that I think that I see what can be improved/changed I'll fix it and repost
I have edited the source I forgot to put in the rules that the user has only three chances :)
1
2
3
4
5
int guessCount = 0;
do {
    ++guessCount;
    // here goes code that checks one guess only
} while (guessCount < 3  &&  !check_ans(g, ran) );
Ah, Simple logical error,

Thanks
Topic archived. No new replies allowed.