-2

I want simply read two chars in C++ but it can't take the second char!!

#include <iostream>
#include <sstream>

using namespace std;

int main()
{

    cout << "reading char: ";
    char ch1;
    cin.get(ch1);
    cout << ch1 << endl;

    char ch2;
    cout << "reading char: ";
    cin.get(ch2);
    cout << ch2 << endl;


    return 0;
}

can anyone help me to solve this problem?

This is the output:

reading char: a

a

reading char:


Process returned 0 (0x0)   execution time : 2.158 s
Press any key to continue.

*************************

The program finishes here, and it does not read the second the char

NathanOliver
  • 150,499
  • 26
  • 240
  • 331
Shahab Einabadi
  • 178
  • 2
  • 12

4 Answers4

3

The problem here is that standard input (std::cin) is buffered. So the program gets no input until the <return> key is presses (or you fill the buffer). This gives you the impression that the code will always wait for user input.

This is not true. It will only wait for user input if the current buffer is empty. If there is data in the buffer it will read that rather than waiting for user input.

The first read:

cin.get(ch1);

Start of the program so the buffer is empty. So when you read from std::cin the program will pause waiting for the buffer to be flushed then the characters will be read. If you hit A<return> your buffer will flush with two characters in the buffer.

Buffer: 'A' '\n'

Your read will now retrieve thefirst character from the input: 'A' leaving the '\n' on the input stream:

Buffer: '\n'

Now the second read sees the buffer is not empty.

cin.get(ch2);

And does not need to wait it will read the next character from the stream. So it reads the '\n' character from the stream. and continues.

Solutions:

That depends what you want. Do you want only the first character from the line?

std::cin.ignore(); // drops the rest of the line.
                   // The next character that will be available will
                   // be the first character of the next line.

You just want to ignore white space? (this is space/tab/newline etc).

std::cin >> v1 >> v2;

Using the operator>> ignores leading white space, then reads the next value. So it will ignore new lines (they are white space).

Community
  • 1
  • 1
Martin York
  • 234,851
  • 74
  • 306
  • 532
2

Your issue here is get only grabs a single character from the stream. When you enter character for your first input by typing the character and pressing enter you actually put two characters into the stream. The one you entered and then enter key (newline character). That means your second call to get is going to get the newline character that is still in the buffer and not wait for you to enter anything. To fix this you should use the stream extraction operator (operator >>) to read the single character and it will skip over the white space that is left in the buffer. That turns your code into

int main()
{

    cout << "reading char: ";
    char ch1;
    cin >> ch1;
    cout << ch1 << endl;

    char ch2;
    cout << "reading char: ";
    cin >> ch2;
    cout << ch2 << endl;


    return 0;
}
NathanOliver
  • 150,499
  • 26
  • 240
  • 331
1

If you enter the first character and press enter, then the new-line character (ASCII 10) is read by the second get() call.

Cepheus
  • 551
  • 4
  • 14
1

The problem is that when you enter in a char and then hit enter, you are entering a newline character into the console. So the call to cin.get() returns the newline to ch2 and your program then prints out ch1 and a blank newline from ch2 and exits.

Instead of using cin.get() you should use cin >> ch1 and cin >> ch2

GBlodgett
  • 12,612
  • 4
  • 26
  • 42
  • 1
    @ShahabEinabadi: You can use `std::cin.get()` but you should know that it gets the next character (this can include the newline character). Once you know that you can ignore that value and read the next character. – Martin York Oct 22 '18 at 19:01