Jump to content
  • Advertisement
Sign in to follow this  
grim_reaper7

if you could answer this stupid question!

This topic is 5048 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i am creating a simple guessing game, starting off in bits cause i am not understanding how to debug in c++ yet, not knowing how to set breaks so i can see values..any way, here is the code i have come up w/ so far...y when i run it, can i press enter several times before the next line prints in prompt?
#include <iostream>
#include <string>
using namespace std;

void fig_answer(string diff){
  int answer;
  if (diff == "b"){
	answer = 10;
  }else if (diff == "i"){
	answer = 50;
  }else{
	answer = 100;
  }
cout<<"Here's you answer "<<answer<<endl;
return;
}

int main(){
string name;
string diff;
cout<<"To begin our little guessing quest start off by telling me your name"<<endl;
//cin>>name;
  getline(cin, name);
  while (name == ""){
	  cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!"<<endl;
      getline(cin, name);
  }
  cin.get();
cout<<"OK "<<name<< " you think you got what it takes eh?"<<endl;
cout<<"Start off by choosing level of Difficulty, type b for beginner, i for intermediate, or a for advanced"<<endl;
cin>>diff;
cin.ignore();
  getline(cin, diff);
  while (diff == ""){
	  cout<<"YOU NEED TO TELL ME YOUR DIFFICULTY PREFERENCE BEOTCH!\n";
      getline(cin, diff);
  }
  cin.get();
  cin.ignore();
fig_answer(diff);

}
sorry for the sloppyness

Share this post


Link to post
Share on other sites
Advertisement
oh yeah...
and when i continue, what kind of method could i call to make

int number_to_be_guessed = random(); <-i knw that's not right it's just an example

i want that to be a new randomly picked number each time...does that make sense? so every time you run the program the number to be guessed is diffent..

Share this post


Link to post
Share on other sites
rand() is the function. It generates a random number from 0 to RAND_MAX. I think it's defined either in cstdlib or cstdio.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>
using namespace std;
void fig_answer(string diff){
int answer;
if (diff == "b"){
answer = 10;
}
else if (diff == "i"){
answer = 50;
}
else{
answer = 100;
}
cout<<"Here's you answer "<<answer<<endl;
return;
}


int main(){
string name;
string diff;
cout<<"To begin our little guessing quest start off by telling me your name"<<endl;
//cin>>name;
getline(cin, name); //<---here
while (name == ""){
cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!"<<endl;
getline(cin, name); //<---here
}
cin.get(); //<-- here
cout<<"OK "<<name<< " you think you got what it takes eh?"<<endl;
cout<<"Start off by choosing level of Difficulty, type b for beginner, i for intermediate, or a for advanced"<<endl;
cin>>diff; //<--here
cin.ignore();
getline(cin, diff); //<--here
while (diff == ""){
cout<<"YOU NEED TO TELL ME YOUR DIFFICULTY PREFERENCE BEOTCH!\n";
getline(cin, diff); //<---here
}
cin.get(); //<---here
cin.ignore();
fig_answer(diff);
}







I marked your usage of cin and getline. I would recomment either using one or the other. I'm not entirely sure of your intent, but just in case, I have code that works the way I think you want it.


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

void fig_answer(string diff){
int answer;
if (diff == "b"){
answer = 10;
}
else if (diff == "i"){
answer = 50;
}
else{
answer = 100;
}
cout<<"Here's you answer "<<answer<<endl;
return;
}

int main(){
string name;
string diff;
cout<<"To begin our little guessing quest start off by telling me your name"<<endl;
cin>>name;

while (name == ""){
cout<<"YOU NEED TO TELL ME YOUR NAME FIRST BEOTCH!"<<endl;
cin >> name;
}
cout<<"OK "<<name<< " you think you got what it takes eh?"<<endl;
cout<<"Start off by choosing level of Difficulty, type b for beginner, i for intermediate, or a for advanced"<<endl;
cin>>diff;
while (diff == ""){
cout<<"YOU NEED TO TELL ME YOUR DIFFICULTY PREFERENCE BEOTCH!\n";
cin >> diff;
}
fig_answer(diff);
system ( "PAUSE"); //I used this, because I was running the .exe from the IDE.
}







As for the random function, as stated above, there is the rand() function. I'm pretty sure you include cstdlib to use it.

basically, you call srand() to seed it, and rand() to generate a number. You can set a limit to the number by using a modulus in conjunction with the rand() function. so you would call:

number = rand() % 20;

and it would give you a number between 0 and 19.

hope I helped.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!