| View previous topic :: View next topic |
| Author |
Message |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Thu Jul 10, 2008 2:05 am Post subject: [C++] Need new method.. |
|
|
Well I'm just trying to make a simple "Guess the number" program.
| Code: | #include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;
void GoHigher()
{
do cout << "Guess Higher" <<endl;
while (random_integer > guess);
goto loop:
}
void GoLower()
{
do cout << "Guess Lower" <<endl;
while (random_integer < guess);
goto loop:
}
int main()
{
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
int random_integer = rand();
loop:
cout << "Take a guess" << endl;
int guess;
cin >> guess;
if (random_integer == guess) cout << "Congratz! You got the number!" << endl;
else if (random_integer > guess); goto GoHigher()
else (random_integer < guess); goto GoLower()
std::cin.sync();
std::cin.ignore();
return 0;
} |
I'm sure you can tell what I'm trying to do, but the way I wrote it is shit Obviously I need to implement a loop, but which would be best? Secondly I was looking at this http://www.daniweb.com/forums/thread1769.html and I was wondering, how do I set RAND_MAX to 100? I want my guess the number to be in between 1-100? And how do I use srand rather than rand?
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Thu Jul 10, 2008 4:28 am Post subject: Re: [C++] Need new method.. |
|
|
| manc wrote: | | Obviously I need to implement a loop, but which would be best? | Depends on what kind of loop do you want to make. For infinite looping, use for(;;), for looping the code while some variable is true, I'd use (do-)while-loop.
| manc wrote: | | how do I set RAND_MAX to 100? I want my guess the number to be in between 1-100? |
| Code: | | int value = (::rand()%100)+1; |
| manc wrote: | | And how do I use srand rather than rand? | Just call it with a "random" seed. The time works the best, since it changes often and won't be the same in future. | Code: | #include <ctime>
#include <cstdlib>
::srand ( time(NULL) );
int value = ::rand(); |
|
|
| Back to top |
|
 |
Zand Master Cheater
Reputation: 0
Joined: 21 Jul 2006 Posts: 424
|
Posted: Thu Jul 10, 2008 8:34 am Post subject: |
|
|
| I don't think you can use srand by itself.
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Thu Jul 10, 2008 11:05 am Post subject: |
|
|
FORGET THE POST AT THE TOP
Ok, so I dont understand whats wrong with this one..I'm really struggling here =\.
| Code: | #include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;
int main()
{
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
int random_integer = rand();
loop:
cout << "Take a guess" << endl;
cin >> guess;
do
{
cout << "Congratz! You got the number!" << endl;
} while (random_integer == guess);
do
{
cout << "Guess Higher" << endl;
goto loop;
} while (random_integer > guess);
do
{
cout << "Guess Lower" << endl;
goto loop;
} while (random_integer < guess);
std::cin.sync();
std::cin.ignore();
} |
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu Jul 10, 2008 12:22 pm Post subject: |
|
|
Whoa... firstly, try to avoid goto at all costs. It's not worth using it. Here's a quick example to achieve the same effect you are going for:
| Code: | #include <windows.h>
#include <iostream>
int main( int argc, TCHAR* argcv[] )
{
int iNumber = NULL;
int iGuess = NULL;
do{
srand( GetTickCount() );
iNumber = ( rand() % 100 ) + 1;
}while( iNumber == 0 );
std::cout << "Number Guessing Game" << std::endl;
std::cout << "I have generated a random number between 1 and 100. Try to guess it!" << std::endl;
std::cout << "If you wish to exit the game, simply enter 0." << std::endl;
for(;;)
{
std::cout << "Enter your guess: ";
std::cin >> iGuess;
if( iGuess == iNumber )
{
std::cout << "You guessed the number!" << std::endl;
break;
}
else if( iGuess > iNumber )
{
std::cout << "Guess lower.." << std::endl;
}
else if( iGuess < iNumber )
{
std::cout << "Guess higher.." << std::endl;
}
else if( iGuess == 0 )
{
break;
}
}
std::cin.sync();
std::cin.ignore();
return 0;
} |
_________________
- Retired. |
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Thu Jul 10, 2008 12:26 pm Post subject: |
|
|
| Code: | #include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;
int main()
{
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
random_integer = rand();
while (1==1){
cout << "Take a guess" << endl;
cin >> guess;
if (random_integer == guess)
{
cout << "Congratz! You got the number!" << endl;
}
if (random_integer > guess)
{
cout << "Guess Higher" << endl;
}
if (random_integer < guess)
{
cout << "Guess Lower" << endl;
}
}
std::cin.sync();
std::cin.ignore();
} |
Edit: Things to remeber
1. Avoid loop:
2. Use if statements
3. Do not declare the same variable twice.
|
|
| Back to top |
|
 |
Noz3001 I'm a spammer
Reputation: 26
Joined: 29 May 2006 Posts: 6220 Location: /dev/null
|
Posted: Thu Jul 10, 2008 12:36 pm Post subject: |
|
|
| Yeah, goto shouldn't be used like that
|
|
| Back to top |
|
 |
Overload Master Cheater
Reputation: 0
Joined: 08 Feb 2008 Posts: 293
|
Posted: Thu Jul 10, 2008 3:21 pm Post subject: |
|
|
| manc wrote: | FORGET THE POST AT THE TOP
Ok, so I dont understand whats wrong with this one..I'm really struggling here =\.
| Code: | #include <windows.h>
#include <iostream>
#include <cstdlib>
using namespace std; //Basic "Guess the number" program
int guess;
int random_integer;
int main()
{
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
int random_integer = rand();
loop:
cout << "Take a guess" << endl;
cin >> guess;
do
{
cout << "Congratz! You got the number!" << endl;
} while (random_integer == guess);
do
{
cout << "Guess Higher" << endl;
goto loop;
} while (random_integer > guess);
do
{
cout << "Guess Lower" << endl;
goto loop;
} while (random_integer < guess);
std::cin.sync();
std::cin.ignore();
} |
|
whoa, you can't have multiple "do's" in a program, unless they are embedded in one another.
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Thu Jul 10, 2008 3:25 pm Post subject: |
|
|
Yeah lol my bad. Final result:
| Code: |
#include "windows.h"
#include <iostream>
#include "stdio.h"
using namespace std;
//-----------------------------------------------
int Guess, Generated_num, intcheck;
char w_h[] = " Guess Higher! ";
char w_l[] = " Guess Lower! ";
char correct[] = " You guessed right! Start a new game? [1:Yes/2:No] ";
//-----------------------------------------------
int check(int inGuess, int RandomNum)
{
if(inGuess != RandomNum)
{
if(inGuess > RandomNum)
return 1;
if(inGuess < RandomNum)
return 2;
}
return 3;
}
//-----------------------------------------------
int main()
{
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
::srand(time(NULL));
newgamejump:
Generated_num = (::rand()%100)+1;
incorrectjump:
cout << " Guess : " ;
cin >> Guess;
intcheck = check(Guess, Generated_num);
switch(intcheck)
{
case 1:
cout << w_l << endl;
goto incorrectjump;
case 2:
cout << w_h << endl;
goto incorrectjump;
case 3:
cout << correct << endl;
cin >> intcheck;
if(intcheck == 1)
goto newgamejump;
}
return 0;
}
|
With alot of help from slippppppp
_________________
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
Posted: Thu Jul 10, 2008 9:15 pm Post subject: |
|
|
| manc wrote: | Yeah lol my bad. Final result:
| Code: |
#include "windows.h"
#include <iostream>
#include "stdio.h"
using namespace std;
//-----------------------------------------------
int Guess, Generated_num, intcheck;
char w_h[] = " Guess Higher! ";
char w_l[] = " Guess Lower! ";
char correct[] = " You guessed right! Start a new game? [1:Yes/2:No] ";
//-----------------------------------------------
int check(int inGuess, int RandomNum)
{
if(inGuess != RandomNum)
{
if(inGuess > RandomNum)
return 1;
if(inGuess < RandomNum)
return 2;
}
return 3;
}
//-----------------------------------------------
int main()
{
cout << "Welcome to guess the number." << endl;
cout << "The secret number lies within 1-100" << endl;
::srand(time(NULL));
newgamejump:
Generated_num = (::rand()%100)+1;
incorrectjump:
cout << " Guess : " ;
cin >> Guess;
intcheck = check(Guess, Generated_num);
switch(intcheck)
{
case 1:
cout << w_l << endl;
goto incorrectjump;
case 2:
cout << w_h << endl;
goto incorrectjump;
case 3:
cout << correct << endl;
cin >> intcheck;
if(intcheck == 1)
goto newgamejump;
}
return 0;
}
|
With alot of help from slippppppp |
You are still doing bad things. Don't use goto, it's not good to use it ever, there are ways to code things to never need it. Take a look at the example I posted above, it will achieve what you are trying to do, using a small if/then case and loop until the user either wants to exit or guesses the number.
_________________
- Retired. |
|
| Back to top |
|
 |
Flyte Peanuts!!!!
Reputation: 6
Joined: 19 Apr 2006 Posts: 1887 Location: Canada
|
Posted: Thu Jul 10, 2008 10:16 pm Post subject: |
|
|
There is a use for goto, to abort nested loops:
| Code: | for(;;) {
for(;;) {
goto out;
}
}
out: |
Anyways, here is what you are looking for in proper C++, with error checking.
| Code: | #include <windows.h>
#include <iostream>
void main( void )
{
int random, i, guess;
//-------
::srand( ::GetTickCount() );
random = ::rand()%100 + 1;
std::cout << "Guess the number between 1-100. Begin..." << std::endl;
for( i = 1;; i++, std::cin.sync() ) {
std::cout << "Guess " << i << ": ";
std::cin >> guess;
if(std::cin.fail()) {
std::cin.clear();
std::cout << "Invalid input." << std::endl;
} else if( guess > random ) std::cout << "Too high!" << std::endl;
else if( guess < random ) std::cout << "Too low!" << std::endl;
else break;
}
std::cout << "You win!" << std::endl;
std::cin.sync();
std::cin.ignore();
} |
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Fri Jul 11, 2008 1:54 am Post subject: |
|
|
| Wiccaan wrote: | Whoa... firstly, try to avoid goto at all costs. It's not worth using it. Here's a quick example to achieve the same effect you are going for:
| Code: | #include <windows.h>
#include <iostream>
int main( int argc, TCHAR* argcv[] )
{
int iNumber = NULL; //Why is it assigned NULL?
int iGuess = NULL; //Why is it assigned NULL?
do{
srand( GetTickCount() ); // What is/does GetTickCount do?
iNumber = ( rand() % 100 ) + 1;
}while( iNumber == 0 );
std::cout << "Number Guessing Game" << std::endl;
std::cout << "I have generated a random number between 1 and 100. Try to guess it!" << std::endl;
std::cout << "If you wish to exit the game, simply enter 0." << std::endl;
for(;;)
{
std::cout << "Enter your guess: ";
std::cin >> iGuess;
if( iGuess == iNumber )
{
std::cout << "You guessed the number!" << std::endl;
break;
}
else if( iGuess > iNumber )
{
std::cout << "Guess lower.." << std::endl;
} //what makes this jump back to "Enter Your Guess"?
else if( iGuess < iNumber )
{
std::cout << "Guess higher.." << std::endl;
} //what makes this jump back to "Enter Your Guess"?
else if( iGuess == 0 ) // er..???
{
break;
}
}
std::cin.sync();
std::cin.ignore();
return 0;
} |
|
I dont understand the parts I commented on. Why are they assigned NULL? And what is GetTickCount? Lastly, in the else if statements, after it says Guess Higher/Lower, what makes it jump back to "Enter your guess" ?
| Flyte wrote: |
Anyways, here is what you are looking for in proper C++, with error checking.
| Code: | #include <windows.h>
#include <iostream>
void main( void )
{
int random, i, guess;
//-------
::srand( ::GetTickCount() ); //What is GetTickCount?
random = ::rand()%100 + 1;
std::cout << "Guess the number between 1-100. Begin..." << std::endl;
for( i = 1;; i++, std::cin.sync() ) { //What..
std::cout << "Guess " << i << ": "; //If i hasnt been assigned a value yet..then what?
std::cin >> guess;
if(std::cin.fail()) { // especially lost on this part
std::cin.clear(); // and this part
std::cout << "Invalid input." << std::endl;
} else if( guess > random ) std::cout << "Too high!" << std::endl;// once again what makes it jump back?
else if( guess < random ) std::cout << "Too low!" << std::endl; //
else break;// same question
}
std::cout << "You win!" << std::endl;
std::cin.sync();
std::cin.ignore();
} |
|
I know I'm a nuisance, thanks for the help though.
_________________
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Fri Jul 11, 2008 6:38 am Post subject: |
|
|
| Flyte wrote: | | Anyways, here is what you are looking for in proper C++, with error checking. | Fails: | Code: | Guess the number between 1-100. Begin...
Guess 1: Invalid input. | CTRL-C fails :<
And I'd prefer | Code: | | ::srand( ::time(NULL) ); |
|
|
| Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8588 Location: 127.0.0.1
|
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Fri Jul 11, 2008 11:39 am Post subject: |
|
|
Oh ok, what about assigning NULL to iNumber and iGuess?
And then Its an endless loop because of (;;) ?
_________________
|
|
| Back to top |
|
 |
|