Difference between cin and cin.get

62 Views Asked by At

I am wondering why my code works. There are two std::cin , but the second one doesn´t open a new line in the terminal for my input (isn´t std::cin supposed to do that?). I thought that std::cin.get is used for getting information out of a string.

Here is my code:

#include <iostream>

int main() {
    char ch;
    std::cin >> ch;
    switch(ch) {
        case '.':
        case '0': case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            std::cin.putback(ch);
            double value;
            std::cin >> value;
            std::cout << value << "\n";
        default:
            return 0;
    }
    return 0;
}

I want to extract the first value out of my input (e.g. "12.4 + 5") and it works (value = 12.4), but why? Where is the second prompt?

0

There are 0 best solutions below