Help with void function

So for this homework assignment I have to ask 3 questions and output whether their correct or not. The part i need help with is writing this code using a void function and not having all 3 questions in a single function. In other words I have to write one generic function and call it 3 times in main. So if anyone could help that would be fantastic! Thank you!

Here's a link to the assignment for reference: http://www.rdb3.com/cpp/exercises/8.5.pdf

Here's my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
using namespace std;

void getAnswer()
{
    while(true)
    {
        string answer;
        cout << "1) What color is the sky? ";
        getline(cin, answer);
        
        if (answer == "Blue")
            cout << "That is correct!\n" << endl;
        else
            cout << "That is incorrect!\n" << endl;
        
        cout << "2) When was baseball invented? ";
        getline(cin, answer);
        
        if (answer == "1838")
            cout << "That is correct!\n" << endl;
        else
            cout << "That is incorrect!\n"<< endl;
        
        cout << "3) Who holds the homewrun record? ";
        getline(cin, answer);
        
        if (answer == "Barry Bonds")
            cout << "That is correct!\n";
        else
            cout << "That is incorrect!\n";
        break;
        
    }
}

int main()
{
    getAnswer();
}
Last edited on
Here are the bare bones for your project:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

void query(std::string question, std::string answer) {
	//print the question
	//accept user input
	//if the user input matches the answer, print "Correct"
	//otherwise, print "Incorrect"
}

int main() {

	query("Question1", "Answer1");
	query("Question2", "Answer2");
	query("Question3", "Answer3");

	return 0;
}


The instructions you've provided also state the following:
In case of multiple correct answers, use multiple
parameters and or-logic. For example, “who founded Microsoft”, accept “Bill Gates” or “Gates”.


It sounds to me like it's not required to have questions that have multiple answers, in other words, it's up to you. For simplicity's sake, I would just choose questions that are straight forward and can't be interpreted in more than one way.

Additionally, the instructions don't mention anything about handling upper- or lower-case answers, though it might be neat for you to implement that as well.
Last edited on
Topic archived. No new replies allowed.