Jump to content
Sign in to follow this  
Arrogance

Guess my Number [C++]

Recommended Posts

[code]#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(int argc, char** argv)
{
srand(static_cast<unsigned int>(time(0)));

int iMyNumber;
int iYourNumber;

while(true)
{
cin.clear();
iMyNumber = rand() % 100 + 1;

cout << "Guess my Number '1 - 100'" << endl;
do
{
cout << "Enter your Number ";
cin >> iYourNumber;

if (iYourNumber > iMyNumber)
cout << "Too high!" << endl << endl;
else if (iYourNumber < iMyNumber)
cout << "Too low!" << endl << endl;

} while (iYourNumber != iMyNumber);
cout << "Correct!" << endl << endl;
cin.get();
}
return 0;
}[/code]

Share this post


Link to post
Share on other sites
[code]#include <iostream>
#include <ctime>
using namespace std;

int main()
{
restart:
int number;
int guess;
int tries;
char response;

srand(time(0));
number = rand() % 500 + 1;

cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
cout << "Remember - you have only twenty tries!" << endl;
cin >> guess;
if (guess > number)
{
cout << "Your guess is too big." << endl;
tries++;
}
else if (guess < number)
{
cout << "Your guess is too small." << endl;
tries++;
}
if (tries == 20)
{
cout << "Sorry! That was your twentieth try! Want to restart the game? Press 'Y' or 'y' if you do" << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
system("CLS");
return(0);
}
}

if (guess == number && tries <= 20);
{
cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
return(0);
}
}
}
[/code]

Share this post


Link to post
Share on other sites
creds plz

[quote name='ninjablood2'][code]#include <iostream>
#include <ctime>
using namespace std;

int main()
{
restart:
int number;
int guess;
int tries;
char response;

srand(time(0));
number = rand() % 500 + 1;

cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
cout << "Remember - you have only twenty tries!" << endl;
cin >> guess;
if (guess > number)
{
cout << "Your guess is too big." << endl;
tries++;
}
else if (guess < number)
{
cout << "Your guess is too small." << endl;
tries++;
}
if (tries == 20)
{
cout << "Sorry! That was your twentieth try! Want to restart the game? Press 'Y' or 'y' if you do" << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
system("CLS");
return(0);
}
}

if (guess == number && tries <= 20);
{
cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
return(0);
}
}
}
[/code][/QUOTE]
lmfao nice bad practice there (system, goto lmfao) you suck. please never post C++ code here again ty

Share this post


Link to post
Share on other sites
[quote name='ninjablood2'][code]#include <iostream>
#include <ctime>
using namespace std;

int main()
{
restart:
int number;
int guess;
int tries;
char response;

srand(time(0));
number = rand() % 500 + 1;

cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
cout << "Remember - you have only twenty tries!" << endl;
cin >> guess;
if (guess > number)
{
cout << "Your guess is too big." << endl;
tries++;
}
else if (guess < number)
{
cout << "Your guess is too small." << endl;
tries++;
}
if (tries == 20)
{
cout << "Sorry! That was your twentieth try! Want to restart the game? Press 'Y' or 'y' if you do" << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
system("CLS");
return(0);
}
}

if (guess == number && tries <= 20);
{
cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
return(0);
}
}
}
[/code][/QUOTE]

And your point is? DON'T use System...

Share this post


Link to post
Share on other sites
[quote name='EvolvedBlitz']I dont get this is it a game?[/QUOTE]
Yeah, it is.

Share this post


Link to post
Share on other sites
[quote name='ninjablood2'][code]
goto restart;
[/code][/QUOTE]

[img]http://imgs.xkcd.com/comics/goto.png[/img]

Share this post


Link to post
Share on other sites
[quote name='EvolvedBlitz']I dont get this is it a game?[/QUOTE]

Yup, I'll add points.

[code]
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main(int argc, char** argv)
{
srand(static_cast<unsigned int>(time(0)));

int iMyNumber;
int iYourNumber;
int iPoints = 5;

while(true)
{
cin.clear();
iMyNumber = rand() % 100 + 1;

cout << "You start with 5 points, each spin cost 1 point." << endl;
cout << "Guess my Number '1 - 100'" << endl;
cout << "Get as many points as you can!" << endl;
do
{
cout << "Enter your Number ";
cin >> iYourNumber;
iPoints += -1;
if (iYourNumber > iMyNumber)
cout << "Too high!" << endl << endl;
else if (iYourNumber < iMyNumber)
cout << "Too low!" << endl << endl;
} while (iYourNumber != iMyNumber);
cout << "Correct!" << endl << endl;
cout << "You've got " << iPoints << " points." << endl << endl;
cin.get();
}
return 0;
}[/code]

Share this post


Link to post
Share on other sites
[quote name='Arrogance']Yup, I'll add points.

[code]
int iMyNumber;
int iYourNumber;
int iPoints = 5;
[/code][/QUOTE]

Don't use Hungarian Notation or, more specifically, [URL="http://www.joelonsoftware.com/articles/Wrong.html"]Systems Hungarian[/URL].

Share this post


Link to post
Share on other sites
[quote name='ninjablood2'][code]#include <iostream>
#include <ctime>
using namespace std;

int main()
{
restart:
int number;
int guess;
int tries;
char response;

srand(time(0));
number = rand() % 500 + 1;

cout << "I have generated a number. Attempt to guess it! (1-500)" << endl;
cout << "Remember - you have only twenty tries!" << endl;
cin >> guess;
if (guess > number)
{
cout << "Your guess is too big." << endl;
tries++;
}
else if (guess < number)
{
cout << "Your guess is too small." << endl;
tries++;
}
if (tries == 20)
{
cout << "Sorry! That was your twentieth try! Want to restart the game? Press 'Y' or 'y' if you do" << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
system("CLS");
return(0);
}
}

if (guess == number && tries <= 20);
{
cout << "Congratulations! You guessed the number in " << tries << " tries!" << endl;
cout << "Want to restart the game? Press 'Y' or 'y' if you do." << endl;
cin >> response;
if (response != 'Y' || response != 'y')
{
goto restart;
}
else
{
return(0);
}
}
}
[/code][/QUOTE]

Looks good and it's good that you're venturing into different languages. However, you should avoid the usage of goto and labels as much as possible. You should also avoid using System because almost anything you pass to it is going to be platform dependent as well as execute slowly.

In this case, rather than use a goto, just wrap the whole game logic in a do-while loop and have a bool (or int) for shouldExit or something similar. You could write an infinite loop and simply break when needed, but it's best practice to allow control flow to handle the flow of logic and avoid using things like break, continue, return, etc. when you can.

for example:
[code]
bool shouldBreak = false;
do {
//game logic
} while(!shouldBreak);

Then when you check for a response you just need to check for no and set shouldBreak to true.


@OP - similar concept. Rather than loop forever, just loop until they want to quit. Otherwise, you're forcing them to terminate the program prematurely by closing it. You should allow the program to end naturally as much as possible. Other than that minor piece, good work. Edited by Trey

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×