Quiz

I am trying to make a quiz using functions, but for some reason even if the answer is correct, i get an incorrect message. Does someone see where the mistake in my program is?

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
  #include <iostream>
#include <string>
using namespace std;

void question(string a, string b)
{   
 cin >> a;
  getline(cin,a);
    
  if(a == b)
    cout << "Correct!" << endl;
  if (a != b)
    cout<< "Incorrect! The correct answer is: " << b << endl;
  
}

int main()
{
  
  string a;
  cout << "What is the capital city of Belgium? ";
  string b = "Brussels";
  
      
  question(a,b);
  
  
  cout << "What is the capital of the US? ";
    b = "Washington";
 
  question(a,b);
  
  
  cout << "What is the capital city of Great Britain? ";
   b = "London";

    
  question(a,b);
  

        
}
Delete line 8. You already fetching the input on line 7.
Line 7-8: You're reading from cin twice. Line 7 reads the answer into a.
Line 8 overlays the answer (a) with an empty string.

Get rid of one or the other.
Last edited on
Topic archived. No new replies allowed.