Public Group

if you could answer this stupid question!

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

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;

if (diff == "b"){
}else if (diff == "i"){
}else{
}
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();

}
sorry for the sloppyness

Share on other sites
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 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 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 on other sites
awesome...thanx...i only new how to cal random from vb i thought it might be the same

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• Forum Statistics

• Total Topics
633769
• Total Posts
3013754
×