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:
/* Created by PopEye || Brian S. */
/* Usually used for demonstration */
#include <iostream>
#include <stdlib.h>
#include <time.h>
usingnamespace std;
void ShowIntro (void);
int main (void)
{
constshort MAX = 3;
short sNum,yANS, counter = 0, aENT[MAX], n = 0;
srand((unsignedshort)time(NULL));
sNum = rand () % 10 + 1;
ShowIntro();
do
{
cout << "Pick a number between 1 and 10: ";
cin >> yANS;
counter++;
if (yANS < 1)
yANS = 1;
elseif (yANS > 10)
yANS = 10;
if (sNum > yANS)
cout << "You are guessing low\n" << endl;
elseif (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";
}
elseif (counter == 2 && yANS == sNum)
{
cout << "Good job! Right on your second try too!\n";
cout << "Showing attemps now...\n" << endl;
cout << "*********************\n";
}
elseif (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.
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;
elseif (yANS > 10)
yANS = 10;
if (sNum > yANS)
cout << "You are guessing low\n" << endl;
elseif (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
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;
[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.
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) { ... } elseif (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?