![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
|
Random Number
Im need the code for C++ that generates arandom number. If you can give it me then please do
|
|
|
|
|
|
#2 |
|
Expert Programmer
|
__________________
Join us at #programmingforums @ irc.freenode.net! My software never has bugs. It just develops random features.
|
|
|
|
|
|
#3 |
|
Hobbyist Programmer
|
I never got anything of google for some reason
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
ive sorted that now but heres my code for some reason i think the while loop is stoping from working can somebody help me plz.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
int x;
int guess;
int a = 1;
int random_integer = rand();
while (a < 5);
cout<<"Enter a number:";
cin>>guess;
cin.ignore();
if (guess < random_integer) {
cout<<"Guess Higher";
};
if (guess > random_integer) {
cout<<"Guess Lower";
};
if (guess == random_integer) {
cout<<"Congratulations,You WIN!";
int a = 10;
};
cin.get();
}it is my first C++ program so thats why it doesnt work |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3
![]() |
I guess you want to do this:
int guess;
int random_integer = rand();
do{
cout<<"Enter a number:";
cin>>guess;
cin.ignore();
if (guess < random_integer)
{
cout<<"Guess Higher";
}
if (guess > random_integer)
{
cout<<"Guess Lower";
}
if (guess == random_integer)
{
cout<<"Congratulations,You WIN!";
}
} while ( guess!=random_integer );
cin.get(); |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
"It doesn't work" is pretty vague. Maybe a description of what is happening would help? Why do you have semi-colons after your closing braces after the IF statements?
__________________
HijackThis Team-SFDC |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jul 2005
Posts: 158
Rep Power: 0
![]() |
First you need to seed the random generater. Here's some code and I'll explain it.
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
int num;
int guess;
int main()
{
srand(time(0)); // seeds the random generator no changes necessary no matter what
num = rand()%(10-1+1); // a mathmatical formula for random integers take the random number and modulus it by max-1+min
while (num!=guess)
{
cout << "Guess my number it's 1-10" << endl;
cin >> guess;
}
cout << endl << "you guessed it";
getch();
return 0;
}
__________________
Geeks may not be cool now but in the long run they prosper. |
|
|
|
|
|
#8 | ||
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
Quote:
![]() |
||
|
|
|
|
|
#9 |
|
Hobbyist Programmer
|
You don't need to seed the rand function. If you dont you'll get the same string of random numbers everytime you run it, but its not a requirement to running the program.
__________________
#programmingforums relay - http://thegupstudio.com/cgi-bin/pforelay.cgi freelance scripts - http://ryanguthrie.com/index.html |
|
|
|
|
|
#10 |
|
Programming Guru
![]() |
If making a random number program produce random numbers is not a requirement to expect from trying to making a number guessing game then i guess neither is user-input..
__________________
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|