-5
bool getAnswer(int a);

int main ()
{
string questions[5] = {"CPU stands for 'Central Processing Unit'", "RAM stands for 'Reserved Access Main'", "HDD stands for 'Hard Drive Doubler'", "SSD stands for 'Solid State Drive'", "CPP stands for 'C Programming Plus'"};

    for (int i = 0; i < 5; i++)
    {
        cout << "Question " << ++i << " \n";
        i--;
        cout << questions[i] << "\n";
        cout << getAnswer(i) << endl;
    }
}

bool getAnswer(int a)
{
    bool answer[5] = {true, false, false, true, false};
    bool user[5];
    string input;

    cout << "Answer " << ++a << " \n";
    a--;

    cout << "Enter a true or false answer: " << "\n";
    cin >> input;

    while (input != "T" && input != "t" && input != "F" && input != "f" && input != "True" && input != "true" && input != "False" && input != "false")
    {
        cout << "Invalid entry, try again!\nEnter a true or false answer: " << "\n";
        cin >> input;
    }

    if (input == "T" || input == "t" || input == "True" || input == "true")
    {
        user[a] = true;
    }
    else if (input == "F" || input == "f" || input == "False" || input == "false")
    {
        user[a] = false;
    }

    if (answer[a] == user[a])
    {
         cout << "Correct!\n";
    }

     else if (answer[a] != user[a])
     {
         cout << "Incorrect!\n";
     }
}

In the output between the correct/incorrect and next question, I keep getting a "0" in-between. How do i remove them.

Ex: Question 1
CPU stands for 'Central Processing Unit'
Answer 1
Enter a true or false answer:
f
Incorrect!
0
Question 2
RAM stands for 'Reserved Access Main'
Answer 2
Enter a true or false answer:
t
Incorrect!
0

Vmansuria
  • 1
  • 2
  • 2
    `getAnswer` is at fault - it's returning 0 since you didn't specify a return value (if no return values are given, the compiler will return 0). For more on why, see this: https://stackoverflow.com/questions/1610030/why-does-flowing-off-the-end-of-a-non-void-function-without-returning-a-value-no – jrd1 Oct 06 '18 at 21:13
  • Hint: What happens after a call to `getAnswer` returns? – 1201ProgramAlarm Oct 06 '18 at 21:14
  • 1
    Change `cout << getAnswer(i) << endl;` to `getAnswer(i);` and change `bool getAnswer(int a)` to `void getAnswer(int a)` in both places. – drescherjm Oct 06 '18 at 21:15
  • That worked thank you!! – Vmansuria Oct 06 '18 at 21:16
  • Also your increment of `i` to print then decrement right after is unusual. Just add 1 when you print. – drescherjm Oct 06 '18 at 21:16

3 Answers3

1

your problem lies with the return value of the getAnswers

cout << getAnswer(i) << endl;

you are already outputting if the answer is correct or not within the getanswer function. and again you are using cout to output the return value of the getAnswer

you can either :

  cout << "Question " << ++i << " \n";
  i--;
  questions[i]
  getAnswer(i);
  cout << endl;

or you can simply stop doing the output in getAnswer and return a string to containing the message.

std::string getAnswer(int a)
{
    bool answer[5] = {true, false, false, true, false};
    bool user[5];
    string input;
    ...
    ...
    ...

    if (answer[a] == user[a])
    {
        input = "Correct!\n";
    }

    else if (answer[a] != user[a])
    {
        input = "Incorrect!\n";
    }
    return input;
}

if you do it like that you don't need to change anything in your main. make sure to change the decleration of the getAnswers from

bool getAnswer(int a); 

to

std::string getAnswer(int a);
Yucel_K
  • 688
  • 9
  • 17
1

Just remove cout << "correct" and cout << "incorrect" and change the return type from bool to std::string.

From:

bool getAnswer(int a) {
    ...
    if (answer[a] == user[a])
        cout << "Correct!\n";

    else if (answer[a] != user[a])
        cout << "Incorrect!\n";
}

To:

std::string getAnswer(int a) {
    ...
    if (answer[a] == user[a])
        return "Correct!\n";

    else if (answer[a] != user[a])
        return "Incorrect!\n";
}

What happens in you program is that your function prints on the screen the answer (as that is what your function does inside the last two ifs). After that, your program tries to print what your function returns through:

cout << getAnswer(i) << endl;

Now, you declared the return value of getAnswer()to be of type bool but, actually, you did not specified a return statement. Consequently, the value returned by your function can be either 1 or 0 (undefined behaviour), in your case it is 0, the value you see.

Tip Use std::cout instead of cout. See Why is “using namespace std” considered bad practice?

Neb
  • 2,270
  • 1
  • 12
  • 22
  • or alternatively just call `getAnswer()` without nesting it in a `cout` line. – Code-Apprentice Oct 06 '18 at 21:18
  • I would not do so, for readability purposes. If I call a function `getAnswer()`, I expect it to `get` the answer, not to `print` it. Moreover, it is more useful to return the answer instead of printing it so you can manipulate it later. – Neb Oct 06 '18 at 21:20
  • Yes, the return makes more sense...if you also rename the function to something like `checkAnswer()`. However, it's return value should be changed to `std::string` in this case, or it should return a `bool` value that the caller can check in an `if` instead. There are several ways to skin this cat to get the correct behavior. – Code-Apprentice Oct 06 '18 at 21:22
  • Yes, you're right, I'll update my answer, thanks! – Neb Oct 06 '18 at 21:24
1

You declared getAnswer() to return a bool value, but never return anything. One way to fix your output is to honor this return type and change

if (answer[a] == user[a])
{
     cout << "Correct!\n";
}

 else if (answer[a] != user[a])
 {
     cout << "Incorrect!\n";
 }

to

return answer[a] == user[a];

Then instead of

cout << getAnswer(i) << endl;

do

if (getAnswer(i)) {
    cout << "Correct!\n" << endl;
} else {
    cout << "Incorrect\n" << endl;
}
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268