Take only first character-ignore all others.

Hi, I am making a "quiz" and trying to get the program to accept only the first character in the input. This is so I can manually input if the answer is right or wrong. ex. they answer right-I put character that means right at the front. (In this case a vertical bar.) Program accepts only that character and says "Yes!".

My problem is that it is taking the first character but after using it the other characters are being put into the next statement. ex. I put "|idk" . It will take "|" and say yes but then it puts "i" into the next cin statement and "d" into the next.
How can I change the code to make the program not do that.

Here is my code:
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
#include <iostream>
using namespace std;
char answer(char);

int main()
{

    cout << endl;
    cout << "Irony-\n";
    cout << "Irony is the contrast between what seems to be and what really is.\n";
    cout << endl;
    
    char a, b, c; 
    
    do {
    cout << "Give an example of irony: \n";
    cin >> a;
    answer(a);
    } while (a == '|');
    
    do {
    cout << "Is this irony? \n";
    cin >> b;
    answer(b);
    } while (b == '|');
    
    do {
    cout << "Give an example of irony: \n";
    cin >> c;
    answer(c);
    } while (c == '|');
    cout << "end";
    cin.get();
    cin.get();
    return 0;
}

char answer(char m)
{
     if (m == '|')
     {cout << "Yes!\n";
     return '|';}
     
 }

Sorry if this was hard to understand. I am very new.
Thanks!
Well you have it set up to only read a char at a time. So, cin>> will take the first character inputted and put it into the variable and the rest will be left in the buffer for the next cin>> statements to grab.

Calling cin.sync(); after each call to cin>> will fix your problem.
Thank-you very much! Works perfectly!
Also, why is answer() returning a character? You don't ever use it.
It also doesn't return anything if m != '|' ...
Thank-you for the advice. I removed the return statements and made answer a void function. What do you mean shacktar? If I change the operator to that, NOT putting "|" will say yes. That is the opposite.
I mean if m is not '|' then the code in answer would go past the if statement. There's no return statement there which could lead to strange behaviour. But since it's a void function now, don't worry about it. Just be sure that all code paths return a value in non-void functions.
Topic archived. No new replies allowed.