Post: [C++] Guessing Game (BETA)
02-17-2011, 09:00 PM #1
Girby2K11
☮ ☯ ☢ ✔ ➝
(adsbygoogle = window.adsbygoogle || []).push({}); Guessing Game!



laugh out load about the 'the beta' part.

but anyway im making this guessing game a very simple one. but i need some help i want the program to close if the user inputs a character, when a user inputs a character it keeps looping nonstop. but otherwise the game works perfect.

also can people tell me what i should improve to make the game not as confusing as the writing is a bit compact together.

heres the code try editing it making it better and ill put it in my game. and put credits at the end "LOL"; NOTE: char error[1] kind of failed i'm really lazy and don't want to remove it. No

    #include <iostream>

// simple guess the number game.

using namespace std;


void Answer()
{
cout << "WOW. You actually Got it right? Computer Malfuntion!\n\n \a\a\a\a";
}

int main()
{
cout << "Guess the number BETA. \n\nDifficulty = to the max (Prototype At the moment.)\n\nNote: the number will keep adding up everytime you fail!\nAlso there is a extreme bug when user inputs a character. BEWARE\n" << endl;
char error[1];
int secretNumber;
double userGuess;


secretNumber = 34;


loop:

cout << "\n\nGuess the number:\n\n";

cin >> userGuess;

if (userGuess > secretNumber)
{
cout << "***************\n!!!TOO BIG!!! Try again!\n***************";
++secretNumber;
goto loop;
}


if(userGuess < secretNumber)
{
cout << "****************\n!!!TOO SMALL!!! Try Again\n****************";
++secretNumber;
goto loop;
}


else
{
Answer();
}


return 0;
}


i know that is could be layed out better but this is how i program and find it easy like this :p
02-18-2011, 10:21 AM #2
Originally posted by Girby2K11 View Post
Guessing Game!



laugh out load about the 'the beta' part.

but anyway im making this guessing game a very simple one. but i need some help i want the program to close if the user inputs a character, when a user inputs a character it keeps looping nonstop. but otherwise the game works perfect.

also can people tell me what i should improve to make the game not as confusing as the writing is a bit compact together.

heres the code try editing it making it better and ill put it in my game. and put credits at the end "LOL"; NOTE: char error[1] kind of failed i'm really lazy and don't want to remove it. No


i know that is could be layed out better but this is how i program and find it easy like this :p


    #include <iostream>
#include <windows.h>
#include <math.h>
#include <string.h>
#include <conio.h>

// simple guess the number game.

using namespace std;

void Answer()
{
cout << "WOW. You actually Got it right? Computer Malfuntion!\n\n \a\a\a\a";
Sleep(1000);
return;
}

//ignore this part, it just determines wether the input is a number or not!
bool stringToShort(string valThisStr,short& copyToMeIfYouCan)
{
copyToMeIfYouCan = 0;
for(char index=0;index<valThisStr.length();index++)
{
if(index!=0)
copyToMeIfYouCan*=10;

if(isdigit( (((int)valThisStr[index])-4Cool Man (aka Tustin) ))
{
copyToMeIfYouCan = 0;
return false;
}
copyToMeIfYouCan += (short)valThisStr[index]-48;
}
return true;
}
//^^^^^^ Ignore this part ^^^^^^^^

int main()
{
cout << "Guess the number BETA. \n\nDifficulty = to the max (Prototype At the moment.)\n\nNote: the number will keep adding up everytime you fail!\nAlso there is a extreme bug when user inputs a character. BEWARE\n" << endl;

short twoDigitNumber =0;
short userGuess =0;
string input ="";
srand ((unsigned)time(NULL)); // inistialize random value generator

twoDigitNumber = rand()%41; // random number from 0 to 40

cout<<endl;
system("PAUSE");

for(;Winky Winky // loop:
{
system("CLS");

cout << "\n\nGuess the number: ";
getline(cin,input);

//we want to convert short to char to compare, so we use (char) before variable
if(stringToShort(input,userGuess))//if the input is a number, play the game
{
if (userGuess>twoDigitNumber)
{
cout << "***************\n!!!TOO BIG!!! Try again!\n***************";
twoDigitNumber++;
Sleep(1000);
continue; // never use goto, cotinue starts the loop again
}


if (userGuess<twoDigitNumber)
{
cout << "****************\n!!!TOO SMALL!!! Try Again\n****************";
twoDigitNumber++;
Sleep(1000);
continue; //same as goto loop:
}


else
{
Answer();
return 0;
}
}
else
{
cout<<"\n\nYou did not enter a number!";
}
Sleep(1000);
}
}


You should not get any errors now.

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo