School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,012 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,039 people online right now. Registration is fast and FREE... Join Now!




C++ Random Number

 
Reply to this topicStart new topic

> C++ Random Number

computerfox
*****



post 12 Aug, 2009 - 01:27 PM
Post #1


Let's take a look at making a guessing game. It's simple and I'm sure there are a couple other ones, but here's my version:

first off you need to do your imports:

CODE

#include <iostream>//main package
#include <string>//for words
#include <cstdlib>//for random generator
using namespace std;//no need to add std:: before most things


since i love making methods (because they're a lot more mobile because you can use the same method over and over again in different parts of the program without copy and pasting the whole code), we will use two main methods:

CODE

int randomNum();//creates the random number within a range
string check(string name,int number,int NUM);//actual method that checks the numbers


since we just declared the methods, let's take a look at what each of them would have to do:

the smaller method is the random number because all you're doing is creating the number. this might be simple, but it's also the key. if you don't make a proper random number, the program wouldn't be doing what it's suppose to.
CODE

int randomNum(){
    
     int NUM=random()%10;// "%" enables the range and the range in this case is up to 10
    
    return NUM;//return the random number of course
}



that was simple enough, no?

now comes the other half of the project.
CODE

string check(string name,int number,int NUM){//uses the values in the variables outside of this method
    string line;
        int counter;
    
    if(name=="fox"||name=="FOX"){//this part is just a hack
        NUM=number;//hack making your number and the random number the same
        cout<<"Number: "<<NUM<<"\nYour number: "<<number;
    }
    
    else
        cout<<"Number: "<<NUM<<"\nYour number: "<<number;
        if(NUM==number)
        line="WIN!!!!\n";
      
       else
      
      
      //otherwise gives you 2 more chances before telling you, you suck
      while(counter<2||NUM==number){
    
       if(number>NUM){
       cout<<"Lower: ";
       cin>>number;
       }
    
       else

       if(number<NUM){
       cout<<"Higher: ";
       cin>>number;
     }
  }

   line="FAIL\n";
    
    return line;//returns line
}


and the driver function would look like this:

CODE


int main () {
    
    string name;
    int number,NUM=randomNum();
    
    cout<<"Name: ";
    cin>>name;
    
    cout<<"Number: ";
    cin>>number;
    
    cout<< check(name,number,NUM)<<endl<<endl;//executes check()
    
    return 0;
}


And the whole code would look like this:

CODE


#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int randomNum();
string check(string name,int number,int NUM);
int main () {
    
    string name;
    int number,NUM=randomNum();
    
    cout<<"Name: ";
    cin>>name;
    
    cout<<"Number: ";
    cin>>number;
    
    cout<< check(name,number,NUM)<<endl<<endl;
    
    return 0;
}

int randomNum(){
    
     int NUM=rand()%10;
    
    return NUM;
}

string check(string name,int number,int NUM){//uses the values in the variables outside of this method
    string line;
    int counter=0;
    
    if(name=="fox"||name=="FOX"){//this part is just a hack
        NUM=number;//hack making your number and the random number the same
    }
    
    
        //cout<<"Number: "<<NUM<<"\nYour number: "<<number;
    if(NUM==number)
        line="WIN!!!!\n";
    
        
        
        //otherwise gives you 2 more chances before telling you, you suck
        counter+=1;
        while(counter<4){
            
            if(NUM==number){
                line="WIN!!!!\n";
                break;
            }
            
            if(counter==3){
                line="FAIL!!!!!";
                break;
            }
            if(number>NUM){
                
                counter++;
                cout<<"Lower: ";
                cin>>number;
            }
            
            
                
                if(number<NUM){
                    counter++;
                    cout<<"Higher: ";
                    cin>>number;
                }
            
                
        }
    
        
    
    
    return line;//returns line
}




i hope this was helpful. thank you for stopping by. have a great day!
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Fast ReplyReply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:20AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month