Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Nov 30th, 2005, 1:20 AM   #11
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
Talking

the post from mocker is stupid as tempest pointed out. if you do not use srand with time or something pseudo-random you'll get the same shit every time. yeah it'll run...woo-fucking-hoo.

here is some neatly written code that modifies the original:

#include <cstdlib>
#include <iostream>
#include <time.h>

using namespace std;

int main()
{
    srand (time(NULL));
    int random_integer = rand()%10;
    int x;
    int guess;    
    
    while (1)
    {
          cout<<"Enter a number:";
          cin>>guess;
    
          if (guess < random_integer) 
          {
             cout<<"Guess Higher"<<endl;
             continue;
          }
          
          if (guess > random_integer) 
          {
             cout<<"Guess Lower"<<endl;
             continue;
          }
    
          if (guess == random_integer) 
          {
             break;
          }
     }
         
     cout<<"Congratulations,You WIN!"<<endl;

     system("pause");
 }
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Nov 30th, 2005, 3:28 AM   #12
kurifu
Expert Programmer
 
kurifu's Avatar
 
Join Date: Jul 2004
Location: Halifax, Nova Scotia (Canada)
Posts: 784
Rep Power: 5 kurifu is on a distinguished road
Send a message via ICQ to kurifu Send a message via MSN to kurifu
How random does the number actually have to be? lol.
__________________
Clifford Matthew Roche &lt;geek@cliffordroche.com&gt;
Web Hosting: http://www.crd-hosting.com
Consulting: http://www.crdev-consulting.com
kurifu is offline   Reply With Quote
Old Nov 30th, 2005, 3:33 AM   #13
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,033
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by 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
This is your problem. The semicolon is terminating the while() construct. In other words, there is an empty loop body. Let me guess- by 'not working', you mean it gets into an endless loop, and you've got to kill the program, right?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Nov 30th, 2005, 3:59 AM   #14
SittingDuck
Programmer
 
SittingDuck's Avatar
 
Join Date: Nov 2005
Location: Moseley, Birmingham, England, Earth
Posts: 51
Rep Power: 3 SittingDuck is on a distinguished road
Quote:
Originally Posted by kurifu
How random does the number actually have to be? lol.
So that the mean of an infinite number of random numbers equals the mean of the maximum and minimum values that rand() can return.
SittingDuck is offline   Reply With Quote
Old Nov 30th, 2005, 2:37 PM   #15
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
actualy no its not an inf loop it just does nothing at all.
ow an Ooble the
int a = 10
is fine because u need to int a and i wanted to set a = 10 and it is fine to do it all together.
HaCkeR is offline   Reply With Quote
Old Nov 30th, 2005, 2:53 PM   #16
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
It's fine, but it isn't neat.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Nov 30th, 2005, 3:16 PM   #17
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Someone needs to watch their semicolons -- both in terms of missing and extra.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 30th, 2005, 3:55 PM   #18
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
Hacker, you're creating a whole new variable called a inside the loop every iteration that replaces the one outside the loop for its duration. Remove the "int".

BN, why not use ctime instead of time.h? This is C++ ya know.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 30th, 2005, 5:25 PM   #19
jayme
Professional Programmer
 
jayme's Avatar
 
Join Date: Nov 2005
Location: Canada
Posts: 495
Rep Power: 0 jayme is an unknown quantity at this point
Send a message via MSN to jayme
seeing as theres already a thread about random numbers made, ill ask here.. How do i assign srand() to a number inbetween something higher than 0? I have a header for producing random numbers in between the specified values, but then other poeple cant compile the program.. and thats not fair
__________________

Quote:
Originally Posted by Mohamed Jihad
Durka durka!
Due to incorrect calculations during the middle ages, our calendar actually begins a few years after Jesus' birth. Thus the real 6/6/6 happened a few years back. The world already ended and you missed it.

Download Code::Blocks now!
jayme is offline   Reply With Quote
Old Nov 30th, 2005, 6:01 PM   #20
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
srand just seeds the generator. Adjusting the range of the random numbers is another operation. One can scale it by various means. You need to know its normal range for your system and platform, of course.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei 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 4:25 AM.

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