Counting in a function

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.

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iomanip>
#include <iostream>
#include <string>
using namespace 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;
}
Have you read about how a function can take a parameter by reference?
It's for a class I'm in and we haven't gotten to references yet. We aren't supposed to use anything we haven't covered yet in the program.
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.
Last edited on
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iomanip>
#include <iostream>
#include <string>
using namespace 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.
Topic archived. No new replies allowed.