| View previous topic :: View next topic |
| Author |
Message |
MegaForum Grandmaster Cheater
Reputation: 0
Joined: 20 Aug 2007 Posts: 558
|
Posted: Fri Jul 04, 2008 2:16 pm Post subject: Questions |
|
|
Hi guys, just got a few questions. (Again?? I know sorry lol )
Many you remember my number guess game. After a few suggestions and changes theres 1 problem and I have 1 question .
Problem: The number that is randomy selected is ALWAYS 42. I'm not sure if the formula is messed up or if I changed something on accident.
Question: What kind of statement can I use to dissallow letters. Basically all I'm asking is an example of something that would make it so if someone typed in a letter,word, or a symbol that It would come up with a message of my choice so that only numbers are allowed. Thanks for the help once again. .
Current Source:
| Code: |
#include "iostream.h"
#include "stdlib.h"
#include "ctime.h"
using namespace std;
int main()
{
int number;
int tries;
int mynumber=rand()%100+1;
cout << "You get 3 tries to match my number! 1-100"<< endl;
for(int tries=1; tries <=5; tries++)
{
cout << "Enter number" << tries << endl;
cin >> number;
if(number == mynumber)
{
cout << "You got my number correct!" << endl;
break;
}
else if(number != mynumber)
{
cout << "Your are incorrect. " << endl;
}
if(number > mynumber)
{
cout << "Your number is higher than the correct number, try again." << endl;
}
if(number < mynumber)
{
cout << "Your number is lower than the correct number, try again." << endl;
}
}
system("PAUSE");
return 0;
}
|
Note: For people who are curious why my headers all are .h or have " " symbols is because in visual C++ 2008, you have name your headers w/e.h and its kept seperate from the main source so you tell it to use that file by using " " but it's really the normal thing.
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Jul 04, 2008 2:30 pm Post subject: |
|
|
Here's an example program that does the same thing, but 1-10.
| Code: |
/* rand example: guess the number */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
int iSecret, iGuess;
/* initialize random seed: */
srand ( time(NULL) );
/* generate secret number: */
iSecret = rand() % 10 + 1;
do {
printf ("Guess the number (1 to 10): ");
scanf ("%d",&iGuess);
if (iSecret<iGuess) puts ("The secret number is lower");
else if (iSecret>iGuess) puts ("The secret number is higher");
} while (iSecret!=iGuess);
puts ("Congratulations!");
return 0;
}
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25832 Location: The netherlands
|
Posted: Fri Jul 04, 2008 3:11 pm Post subject: |
|
|
the important difference: srand
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
|