Jump to content

C++ problem with Q&A program

NeatSquidYT

Hi,

So in my program, you're supposed to be able to input a question, then an answer to that question. The user then is able to try and get it right. For some reason,my program always says that it's wrong. Thanks in advance

 

#include <iostream>#include <string> using namespace std; void check(string answer, string usranswer) {    if(answer != usranswer){        cout << "That is an incorrect answer." << endl;    }    else {        cout << "That is correct." << endl;    }} void qanda(string question, string answer) {    string usranswer;    cout << question << endl;    cin >> usranswer;    check(usranswer, answer);} void generatequestion(string question, string answer) {    cout << "Please enter the question you wish to be answered: " << endl;    getline(cin, question);    cout << "What is the answer to the question? " << endl;    getline(cin, answer);    qanda(question, answer);}int main() {    string question = "";    string answer = "";    generatequestion(question, answer);    return 0;} 
Link to comment
Share on other sites

Link to post
Share on other sites

 

For some reason,my program always says that it's wrong.

 

PC7vtBh.png

Hmm? I've tried both correct and incorrect and it seems to be working here.

From salty to bath salty in 2.9 seconds

 

Link to comment
Share on other sites

Link to post
Share on other sites

cin breaks up input by spaces, so if you're answer has a space in it you will only store the first word inside usranswer. You can use getline instead to get a full line of input, like you do the first time you ask for the answer.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Why are you initialising the question and answer strings that way?

Comb it with a brick

Link to comment
Share on other sites

Link to post
Share on other sites

PC7vtBh.png

Hmm? I've tried both correct and incorrect and it seems to be working here.

Wait, it's working now. Nevermind.

Link to comment
Share on other sites

Link to post
Share on other sites

Why are you initialising the question and answer strings that way?

how else would I do it? Be more helpful please.

Link to comment
Share on other sites

Link to post
Share on other sites

cin breaks up input by spaces, so if you're answer has a space in it you will only store the first word inside usranswer. You can use getline instead to get a full line of input, like you do the first time you ask for the answer.

ah yes, I knew I forgot to replace cin with getline somewhere. Thanks.

Link to comment
Share on other sites

Link to post
Share on other sites

Wait, it's working now. Nevermind.

No it's not, this test case is just not causing the issue

 

Read fizzlesticks' answer:

cin breaks up input by spaces, so if you're answer has a space in it you will only store the first word inside usranswer. You can use getline instead to get a full line of input, like you do the first time you ask for the answer.

 

 

also what Mark said:

Why are you initialising the question and answer strings that way?

 

you pass variable to your generatequestion function by variable so after this function ends returns, those variables are still empty strings. you just can declare them this way inside this function and function won't need any arguments after that. (Or if you want to change valuse bny function, pass them by reference or pointer)

Link to comment
Share on other sites

Link to post
Share on other sites

how else would I do it? Be more helpful please.

I dunno, it just seems a little redundant creating it in main() assigning "" to it and then passing it as an argument to generatequestion().

Why not just initialize them in generatequestion() so you don't need arguments to that and you don't need to assign what is essentially null to it.

Comb it with a brick

Link to comment
Share on other sites

Link to post
Share on other sites

 

you pass variable to your generatequestion function by variable so after this function ends returns, those variables are still empty strings. you just can declare them this way inside this function and function won't need any arguments after that. (Or if you want to change valuse bny function, pass them by reference or pointer)

Example?

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×