Tips on Shortening?

Hello. I'm new to C++ and read several books on C++ for beginners. After indulging in some knowledge I wrote my own 'Guess the Number' game. Check it out:

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
84
85
86
87
88
89
90
91
92
93
94
/* Created by PopEye || Brian S. */
/* Usually used for demonstration */

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;

void ShowIntro (void);

int main (void)
{
	const short MAX = 3;
	short sNum,yANS, counter = 0, aENT[MAX], n = 0;
	
	srand((unsigned short)time(NULL));
	sNum = rand () % 10 + 1;

	ShowIntro();
	
	do
	{
		cout << "Pick a number between 1 and 10: ";
		cin >> yANS;

			counter++;

		if (yANS < 1)
			yANS = 1;
		else if (yANS > 10)
			yANS = 10;
		if (sNum > yANS)
			cout << "You are guessing low\n" << endl;
		else if (sNum < yANS)
			cout << "You are guessing high\n" << endl;
		
		short m = 0;
		
		while (m != 1)
		{

			aENT[n] = yANS;
			n++;
			m++;
		}

	}
	while ((yANS != sNum) && (counter != 3));

	if (counter == 1 && yANS == sNum)
	{
		cout << "Nice Job! Right on your first try too!\n";
		cout << "Showing attemps now...\n" << endl;
		cout << "*********************\n";
	}
	else if (counter == 2 && yANS == sNum)
	{
		cout << "Good job! Right on your second try too!\n";
		cout << "Showing attemps now...\n" << endl;
		cout << "*********************\n";
	}
	else if (counter == 3 && yANS == sNum)
	{
		cout << "Lucky! You got the number correct on your last try!\n";
		cout << "Showing attemps now...\n" << endl;
		cout << "*********************\n";
	}
	else
	{
		cout << "Sorry! You failed to figure out the number...\n";
		cout << "The number was " << sNum << '\n';
		cout << "Showing attemps now...\n" << endl;
		cout << "*********************\n";
	}

	for (short i = 0; i < counter; i++)
	{
		cout << "Attempt #" << i + 1 << " value is " << aENT[i] << '\n';
		cout << "Attempt #" << i + 1 << " address is " << aENT + i << '\n';
	}

	return 0;
}

void ShowIntro (void)
{
	cout << "			Guess the Number! - v1.0\n";
	cout << '\n';
	cout << "**********\n";
	cout << "This game is rather... simple. You have 3 chances to guess the number.\n";
	cout << "If your guess is higher then the secret number, you will be notified if your \n";
	cout << "guess if high or low. That's all there is to it.\n";
	cout << "**********\n" << endl;
}

In finishing this game I felt accomplished. My question is that if there are any tips on shortening the length of the code. Thank you. I would really much appriciate it.

Edit 1: I forgot to mention that this game is on a Console Application. Also, the aENT + 1 was just to practice constant pointers like arrays. You can ignore that.
Last edited on
What's the point of the while on line 3139? It will always run exactly once.
Last edited on
Line 31 is:
yANS = 10;
If you meant line 48, I use the while because it's part of the do...while loop.

Edit 1:
Oh I see what you mean.
1
2
3
4
5
6
7
while (m != 1)
{

	aENT[n] = yANS;
	n++;
	m++;
}

Now that I think about it, you are correct. There is no need for the while to be there; also taking away the m variable. Thanks.
Last edited on
Sorry. I meant 39.
Yeah. I edited my 1st post that corresponds to your 1st answer. Thanks man.

Edit 1:
Any other tips?
Last edited on
I hate (but this is personal) do while when the maximum number of loops is already known.
You might change the first do/while loop in the following way.
1st: initialize yANS to 0 (otherways if you are really unlucky, you might not enter the following cycle)
then write (in place of your do...while) the following cycle like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (counter = 0; counter <3 && yAns!=sNum ; counter++)
{
        cout << "Pick a number between 1 and 10: ";
	cin >> yANS;
//start copy paste of your code
                 if (yANS < 1)
			yANS = 1;
		else if (yANS > 10)
			yANS = 10;
		if (sNum > yANS)
			cout << "You are guessing low\n" << endl;
		else if (sNum < yANS)
			cout << "You are guessing high\n" << endl;
//end copy paste of your code
        aENT[counter] = yANS;//<-you don't need n variable at all, counter does the job for you!
}


Moreover, you don't need the if (counter == 1 && yANS == sNum) elseif elseif else structure...
(if elseif elseif make it more difficult) Your counter already tells you if that guy won or not: if counter is 1,2,3 he won.
You might wanna use
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
switch (counter)
{
case 1:
cout << "Nice Job! Right on your first try too!\n";
break;//<-if you forget this C++ will go through all the subsequent cases
case 2:
cout << "Good job! Right on your second try too!\n";
break;
case 3:
cout << "Lucky! You got the number correct on your last try!\n";
//edit: I forgot to put the following line!
break;
default: //keyworld: it means in all the other cases (which for you is only 4, actually)
cout << "Sorry! You failed to figure out the number...\n";
cout << "The number was " << sNum << '\n';
}
//this part is common, so you write it only once
cout << "Showing attemps now...\n";
//put endl on next line so that you are sure your text is going to cout. with \n it might not !
cout << "*********************"<<endl;

The other part is fine.
Last edited on
Thanks samusamu! I need to study your code for a while to actually get it in my system.
[edit]@samusamu[/edit]
Loops are useful even if you know the number of iterations. The point is to reduce complexity, not repeat it N times.

If you are concerned about loop unwinding optimizations, then you should get yourself an assembler and play with that -- the C and C++ compilers are smart enough to do that kind of stuff for you.
Last edited on
You are welcome!
If you think something is not clear, keep posting: I'll be glad to answer (but it might take time, I'm not always in this forum)
The compiler also will have the same result using ifs and elses or switch, but in a case like this switch is much more clear and easy to understand for sure.
Are you sure? I don't know anything about how compilers work their magic, but in my head it would do this:

if-else: generates two "goto" labels, then jumps according to the result of the check.
switch: generates a "goto" label for each case, then jumps according to the value of your switch variable.

-> switch has 1 jump.
-> nested if-else has 1 jump for each if-else pair.

In cases of if (x == 5) { ... } else if (x == 6) { ... } I'd expect a compiler to realize it's a "noobie switch", but is that also the case with inequalities or compound conditions?
I use Crisco! Oh, not THAT kind of shortening ... sorry! ;)
Topic archived. No new replies allowed.