I'm pretty sure I'm making this way more complicated than it needs to be. The program already works, but i'm trying to add a counter to the function so that it returns the number of correct answers at the end. As it is now it always returns 1 out of 3 were correct, no matter what.
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace std;
#include <cmath>
// prototype for void function askQuestion
void askQuestion(string theQuestion, string theAnswer1);
int totalCorrect(int correct);
int main()
{
// declaration of variables
string theAnswer1;
string theQuestion;
int correct = 0;
// variables for each question
string a = "What color is the sky? ";
string b = "Is the moon a planet? ";
string c = "What year was Y2K? ";
// question 1 and function call 1
theQuestion = a;
theAnswer1 = "blue";
askQuestion(theQuestion, theAnswer1);
// question 2 and function call 2
theQuestion = b;
theAnswer1 = "no";
askQuestion(theQuestion, theAnswer1);
// question 3 and function call 3
theQuestion = c;
theAnswer1 = "2000";
askQuestion(theQuestion, theAnswer1);
correct = totalCorrect(correct);
cout << "#of correct answers: " << correct << " out of 3 questions." << endl;
}
// function to ask a question and evaluate the answer
void askQuestion(string theQuestion, string theAnswer1)
{
int correct;
// variable for user input
string userAnswer;
cout << theQuestion;
getline(cin, userAnswer);
// if statement to compare user answer to question answer
if (userAnswer == theAnswer1)
cout << "Correct!" << endl;
if (userAnswer == theAnswer1)
totalCorrect(correct);
else
cout << "Incorrect!" << endl;
}
int totalCorrect(int correct)
{
correct++;
return correct;
}
I would recommend changing your askQuestion function to return a bool, and then increment the correct variable inside main based on whether or not each askQuestion function call is true. Then just output correct at the end.
This means you don't need the totalCorrect function anymore.
#include <iomanip>
#include <iostream>
#include <string>
usingnamespace std;
#include <cmath>
// prototype for bool function askQuestion
bool askQuestion(string theQuestion, string theAnswer1, int correct);
int main()
{
// declaration of variables
string theAnswer1;
string theQuestion;
int correct = 0;
// variables for each question
string a = "What color is the sky? ";
string b = "Is the moon a planet? ";
string c = "What year was Y2K? ";
// question 1 and function call 1
theQuestion = a;
theAnswer1 = "blue";
if (askQuestion(theQuestion, theAnswer1, correct) == true)
correct++;
// question 2 and function call 2
theQuestion = b;
theAnswer1 = "no";
if (askQuestion(theQuestion, theAnswer1, correct) == true)
correct++;
// question 3 and function call 3
theQuestion = c;
theAnswer1 = "2000";
if (askQuestion(theQuestion, theAnswer1, correct) == true)
correct++;
cout << "#of correct answers: " << correct << " out of 3 questions." << endl;
}
// function to ask a question and evaluate the answer
bool askQuestion(string theQuestion, string theAnswer1, int correct)
{
bool status;
// variable for user input
string userAnswer;
cout << theQuestion;
getline(cin, userAnswer);
// if statement to compare user answer to question answer
if (userAnswer == theAnswer1)
cout << "Correct!" << endl;
if (userAnswer == theAnswer1)
status = true;
else
{
cout << "Incorrect!" << endl;
status = false;
}
return status;
}
I switched it to a bool and everything works correctly now, thank you. Just wondering if this is the best way to accomplish this?
I don't know that there's one "best way" to accomplish it. There are alternatives for sure (such as reference parameters, although, as you've said, you can't use that yet).
I would take out the correct parameter from your askQuestion function since you aren't using it in there anymore. You can also put curly braces around lines 60 - 64, then get rid of line 62, since it checks the exact same thing. You can also leave out the "== true" on lines 30, 36, and 42, because askQuestion will already return true or false (and comparing true == true or false == true isn't necessary).
Overall though, it looks like you did a good job implementing the boolean return value to me.