Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 29th, 2005, 1:43 PM   #1
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Random Number

Im need the code for C++ that generates arandom number. If you can give it me then please do
HaCkeR is offline   Reply With Quote
Old Nov 29th, 2005, 1:47 PM   #2
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 4 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
http://www.cprogramming.com/tutorial/random.html

Googling wasn't too hard
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Nov 29th, 2005, 1:50 PM   #3
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
I never got anything of google for some reason
HaCkeR is offline   Reply With Quote
Old Nov 29th, 2005, 2:28 PM   #4
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
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
HaCkeR is offline   Reply With Quote
Old Nov 29th, 2005, 2:38 PM   #5
-=PARADOX=-
Programmer
 
-=PARADOX=-'s Avatar
 
Join Date: Oct 2005
Location: Portugal
Posts: 53
Rep Power: 3 -=PARADOX=- is on a distinguished road
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();
-=PARADOX=- is offline   Reply With Quote
Old Nov 29th, 2005, 2:42 PM   #6
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
"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
groovicus is offline   Reply With Quote
Old Nov 29th, 2005, 3:27 PM   #7
teencoder
Hobbyist Programmer
 
teencoder's Avatar
 
Join Date: Jul 2005
Posts: 158
Rep Power: 0 teencoder is an unknown quantity at this point
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;
}
enjoy
__________________
Geeks may not be cool now but in the long run they prosper.
teencoder is offline   Reply With Quote
Old Nov 29th, 2005, 6:20 PM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Quote:
Originally Posted by HaCkeR
int a = 10;
There's your problem. You're redeclaring a in the scope of the loop. You want a = 10; - no int.

Quote:
Originally Posted by HaCkeR
it is my first C++ program so thats why it doesnt work
Actually, the reason above is why it doesn't work.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 29th, 2005, 6:28 PM   #9
Mocker
Hobbyist Programmer
 
Mocker's Avatar
 
Join Date: Oct 2005
Location: Indiana
Posts: 218
Rep Power: 0 Mocker is an unknown quantity at this point
Send a message via AIM to Mocker
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
Mocker is offline   Reply With Quote
Old Nov 29th, 2005, 6:35 PM   #10
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
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..
__________________

tempest is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:50 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC