My First Program

Here is the filefront... http://www.filefront.com/17053523/GuessTheNumber.zip

It's a guess the number game, pretty simple. Any critiques on the code are welcome.
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Guess The Number Game.

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int number()
{
srand((unsigned)time(0));
int x = rand() % 25 + 1;
return x;

}

int main() {

	int actualNumber = number();
	int playersGuess;

	cout << "Let's play Guess My Number! \n";
		cout << "Guess a number from 1-25:  ";
		cin >> playersGuess;

		if (playersGuess == actualNumber)
		{
			cout << "You Won!";
		}
			if (playersGuess > actualNumber)
			{
				cout << "That's too high!";
			}
				if (playersGuess < actualNumber)
				{
					cout << "That's too low!";
				}
					if (playersGuess > 25 || playersGuess < 1)
					{
						cout << "You have to guess between 1 and 25.";
					}
					else
						cout << "";
		cout << "Try Again! : ";
		cin >> playersGuess;
		{
			if (playersGuess == actualNumber)
				{
					cout << "You Won! ";
				}
					if (playersGuess > actualNumber)
					{
						cout << "That's too high! ";
					}
						if (playersGuess < actualNumber)
						{
							cout << "That's too low! ";
						}
							if (playersGuess > 25 || playersGuess < 1)
							{
								cout << "You have to guess between 1 and 25. ";
							}
							else
								cout << "" ;
		}
		cout << "Final Guess: ";
		cin >> playersGuess;

		{
			if (playersGuess == actualNumber)
				{
					cout << "\n You Won!";
				}
			else
			{
				cout << "The number was : " ;
				cout << actualNumber;
		 	}


		return 0;
		}
}
Last edited on
4, oh I should have yelled 2.
You're indentation is a bit off. The if statement on lines 29, 33, 37 and 41 should be in line with the one on line 25. You would only indent like you have done if each if-statement was inside the previous if-statements code block, or scope. The same applies to 46 through to 62.

Task
* Re-write the way you accept input to account for any errors that may occur. e.g. an int accepting a char.

** Re-write the program so that it is all in one loop.
Okay, here is the new version with looping and correct indentation.
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
// Guess The Number Game.

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int number()
{
srand((unsigned)time(0));
int x = rand() % 25 + 1;
return x;

}

int main() {

	int actualNumber = number();
	int playersGuess;

		cout << "Let's play Guess My Number! \n";
		cout << "Guess a number from 1-25:  ";

	for (int counter =0; counter < 3; counter++)
	 {
		cout << "\n Guess: ";
		cin >> playersGuess;

		if (playersGuess == actualNumber)
		{
			cout << "You Won!";
		}

		if (playersGuess > actualNumber)
		{
		        cout << "That's too high!";
		}

		if (playersGuess < actualNumber)
		{
			cout << "That's too low!";
		}

		if (playersGuess > 25 || playersGuess < 1)
		{
		        cout << "You have to guess between 1 and 25.";
		}
	 }

				cout << "\n The number was : " ;
				cout << actualNumber;

				return 0;
			}

Last edited on
use an if..elseif..elseif..else construct since each guess can only meet one of the prescribed conditions. Test your program by entering a character instead of a number and see what happens. Once you realize the problem, read the info at this link to fix the problem.
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3
Also, just to make it easier on the eyes, if you have one line of code after an if statement, for example:
1
2
3
4
if (playersGuess > 25 || playersGuess < 1)
{
        cout << "You have to guess between 1 and 25.";
}


you can just write:
 
if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25.";}


Also, including an end of line statement might be something you'll want once this executes:
if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25.\n";}
or
if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25."<<endl;}

What exactly is your question?
Last edited on
stavros wrote:
if (playersGuess > 25 || playersGuess < 1){cout << "You have to guess between 1 and 25.";}

This is definately not easier on the eyes. JustAWalrus, if you're going to only execute one command after your if-statement, you do not need the curly braces; however it does not hurt to do so.

stavros wrote:
What exactly is your question?
JustAWalrus wrote:
Any critiques on the code are welcome.


EDIT* kempofighter's post is very important and should not be overlooked!
Last edited on
I'd say that if (playersGuess > 25 || playersGuess < 1) would better not be part of the main control construct of your loop, as it shouldn't reduce your counter. My suggestion is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int counter=0; counter<3; counter++)
{
    while (true)
    {
        cout << "\n Guess: ";
        cin >> playersGuess;

        if (playersGuess > 25 || playersGuess < 1)
            cout << "You have to guess between 1 and 25." << endl;
        else
            break;
    }

    //rest of the loop here...
}
If you read the link that I posted there is a way to test for valid input as well as a range. You can combine the two concepts with an embedded loop. So within the for loop you write an embedded while loop that asks for input until the user enters something valid. There is a small while loop in the example code that you can copy and paste easily. Then adapt it for the specific range test that you need.
Ah, right. To be honest, I didn't bother to click on the link kempofighter suggested, hahaha. I just read his post and assumed that the link only provided a method to check for non-numeric entries. Well, it turns out that my post is redundant... Just check kempofighter's link.
I don't usually +1 something, but kempofighter's link is possibly the most useful thing I have ever seen -- and I've seen table saws that refuse to cut your finger off!

(Also the link will not cut your finger off)
Okay, I tried all of your suggestions and I'm pretty sure everything in this one works..
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
// Guess The Number Game

#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;

int number()
{
srand((unsigned)time(0));
int x = rand() % 25 + 1;
return x;

}

int main() {

 int actualNumber = number();
 int playersGuess = 0;

 cout << "Let's play Guess My Number! \n";
 cout << "Guess a number from 1-25!";

	for (int counter =0; counter < 3; counter++)
{
	cout << "\n Guess: ";
	cin >> playersGuess;

	if (playersGuess == actualNumber) { cout << "You Won!"; }
	else if  (playersGuess > 25 || playersGuess < 1)
		 {std::cout << "Invalid Responce!";
		 std::cin.clear();
		 std::cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');}
	else if (playersGuess > actualNumber) {cout << "That's too high! ";}
	else if (playersGuess < actualNumber) {cout << "That's too low! ";}

}

	cout << "\n The number was : " ;
	cout << actualNumber;
	return 0;
	}


Couple things. Since you added "using namespace std;" to the top of the page you don't need to put std:: before commands in that namespace. Also, add "#include <limits>" to the top of the page.

After "cin >> playersGuess;" add "cin.ignore();" to clear out the ENTER input.

As you may already know you can put multiple parameters into your cout << command. i.e. "cout << "\n The number was : " << actualNumber;"

Assuming you're not running the program in debug mode, you need to place a cin.get(); before return 0; or else the program will quit out on you.
Thank you to everyone to helped me. Here is the final code (assuming there aren't any other issues)
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
 
// Guess The Number Game. JustAWalrus
#include <ctime>
#include <iostream>
#include <limits>
#include <cstdlib>
using namespace std;

int randomnumber()
{
   srand((unsigned)time(0));
   int x = rand() %25 + 1;
   return x;
}

int main ()
{
	int actualNumber = randomnumber();
	int playersGuess = 0;

	cout << "Want to play Guess My Number?";
	cout << "\n Guess a number between 1 and 25.";

	for (int guessess = 0; guessess < 3 ; guessess++)
	{
		cout << "\n Guess : ";
		cin >> playersGuess;

		if (playersGuess == actualNumber) { cout << "You Won!"; break; }
		else if  (playersGuess > 25 || playersGuess < 1)
		        {cout << "Invalid Responce!";
			cin.clear();
		        cin.ignore(numeric_limits<streamsize>::max(),'\n');}
		else if (playersGuess > actualNumber) {cout << "That's too high! ";}
		else if (playersGuess < actualNumber) {cout << "That's too low! ";}

	}
	cin.ignore();
	if (playersGuess != actualNumber){
	cout << "\n The number was : "  << actualNumber << "\n";}
	cin.get();
	return 0;
}
Last edited on
Wow, it looks so pretty as compared to before- nice! =D

Also, typically you want to avoid the use of using namespace std.
You still haven't completed the first task i gave you in my first post which is very important.
You still haven't completed the first task I gave you in my first post which is very important.


This piece of code stops errors involving characters, but it doesn't change the input mode.

1
2
3
4
5
6
 
else if  (playersGuess > 25 || playersGuess < 1)
		        {cout << "Invalid Responce!";
			cin.clear();
		        cin.ignore(numeric_limits<streamsize>::max(),'\n');}
Sorry, I overlooked it. However I would add to the line else if (!cin && playersGuess > 25 || playersGuess < 1)
Topic archived. No new replies allowed.