Page 1 of 1

C++ Random Number

#1 computerfox  Icon User is offline

  • straight vegetarian kid

Reputation: 50
  • View blog
  • Posts: 3,772
  • Joined: 29-January 09

Posted 12 August 2009 - 02:27 PM

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:

#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:

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.
 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.
 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:


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:


#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!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1