First, as usual we need imports:
#include <iostream> //input/output #include <string> //strings #include <cstdlib> //random using namespace std; //no need for std::
Okay, so not much to import, well, more than the JAVA version, but it's still only 3 imports.
Next step, we will have to make the methods. One to make a random integer and another to to get your answer.
The random number generator is pretty simple:
int randomNum(){
int NUM=(rand()%5); //the modulus enables it to stop at a certain number, giving you a result in a range
return NUM; //returns that number
}
The other method:
string check(string line,int NUM){
int randomCheck=NUM;
string answer,tmp;
//since there is no such thing as "forever" we have to take out any thought of that word
for(int i=0;i<line.length();i++){
if(line[i]=='f'){
for(int j=0;j<7;j++){
tmp+=line[i];
i++;
}
}
}
if(tmp=="forever"){
answer="Hell no...\n";
}
else
if(randomCheck==0){
answer="NO...\n";
}
else
if(randomCheck==1){
answer="Yes...\n";
}
else
if(randomCheck==2){
answer="Maybe...\n";
}
else
if(randomCheck==3){
answer="Only time can tell...\n";
}
else
if(randomCheck==4){
answer="Not enough information...\n";
}
return answer;
}
We have everything, cept for the driver function. How would that look? Well, it would look something like this:
int main () {
string line;
int NUM=randomNum(),again;
//This section isn't necessary, it just makes the program look nicer and a bit more professional.
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<" WELCOME TO THE NUMBER 8 BALL GAME \n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"Enter question: "; //prompt for question
getline(cin,line); //input of question
cout<<check(line,NUM)<<endl; //output of the answer to your question
cout<<"1-Again 2-NO: "; //prompt to continue
cin>>again; //input of answer
//loop
while(again==1){
cout<<"Enter question: ";
cin.get();
getline(cin,line);
NUM=randomNum();
cout<<check(line,NUM)<<endl;
cout<<"1-Again 2-NO: ";
cin>>again;
}
cout<<"\nThank you for playing. Please come agian soon...\n\n"; //exit note
return 0; //returns 0 since main is int
}
We have all the parts, but how would the code look altogether?
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int randomNum();
string check(string line,int NUM);
int main () {
string line;
int NUM=randomNum(),again;
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<" WELCOME TO THE NUMBER 8 BALL GAME \n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n";
cout<<"Enter question: ";
getline(cin,line);
cout<<check(line,NUM)<<endl;
cout<<"1-Again 2-NO: ";
cin>>again;
while(again==1){
cout<<"\nEnter question: ";
cin.get();
getline(cin,line);
NUM=randomNum();
cout<<check(line,NUM)<<endl;
cout<<"1-Again 2-NO: ";
cin>>again;
}
cout<<"\nThank you for playing. Please come agian soon...\n\n";
return 0;
}
int randomNum(){
int NUM=(rand()%5);
return NUM;
}
string check(string line,int NUM){
int randomCheck=NUM;
string answer,tmp;
//since there is no such thing as "forever" we have to take out any thought of that word
for(int i=0;i<line.length();i++){
if(line[i]=='f'){
for(int j=0;j<7;j++){
tmp+=line[i];
i++;
}
}
}
if(tmp=="forever"){
answer="Hell no...\n";
}
else
if(randomCheck==0){
answer="NO...\n";
}
else
if(randomCheck==1){
answer="Yes...\n";
}
else
if(randomCheck==2){
answer="Maybe...\n";
}
else
if(randomCheck==3){
answer="Only time can tell...\n";
}
else
if(randomCheck==4){
answer="Not enough information...\n";
}
return answer;
}
What about the results?

******WARNING******** PLEASE DON'T TAKE ANSWERS TOO SERIOUSLY. IT'S ONLY A GAME.
I guess that's all I can put down. Thank you for stopping by and I hope all of this was helpful. If you are looking for the JAVA version, please visit http://www.dreaminco...topic119859.htm.
This post has been edited by computerfox: 31 August 2009 - 09:05 PM




MultiQuote







|