0
//the code below from a tutorial is demonstrating the usefulness 
//of the function getline() when entering strings with whitespaces.


#include <string> 
#include <iostream>

int main()
{
   std::cout << "Enter your full name: ";
    std::string name{};
    std::getline(std::cin >> std::ws, name); // read a full line of text into name

    std::cout << "Enter your age: ";
    std::string age{};
    std::getline(std::cin >> std::ws, age); // read a full line of text into age

    std::cout << "Your name is " << name << " and your age is " << age << '\n';

    return 0;
}


I run and compile with Visual Studio (with run in terminal checked in settings) and the input/output behaves as I would expect.

But with Eclipse CDT: After running there is nothing in console. If I type, "John Doe" FIRST then hit enter only then I am prompted to enter name. But following what I input is 'formatted' into variable "age" not variable "name" in std::cout << "Your name is " << name << "and your age is" << age << '\n';

taking some liberties here it goes as represented below.

(nothing in console), my input: "John Doe".
"enter your name", my input: "20".
"enter your age Your name is John Doe and your age is 20."

I try std::cerr and just get a "enter your name" but the program does nothing after I hit enter.

My lack of understanding in this trivial regard is really messing with me.

It appears the buffer (my uneducated guess) and the console output are not synchronized properly but why is this?

howlger
  • 31,050
  • 11
  • 59
  • 99
Michael
  • 3
  • 2
  • Visual Studio Code and Eclipse are IDE's, not C++ compilers. Whatever quirks they have with console output, simply stated, it is what it is -- maybe those IDE's have bugs, who knows. If you want to truly see how your program behaves, open a (real) console and run your program from there. – PaulMcKenzie Jun 25 '22 at 23:15
  • 1
    If you want to be sure your output gets shown to the user, `flush` it. Otherwise stuff could be held in buffers until a more efficient time to empty the buffer arrives. – user4581301 Jun 25 '22 at 23:19

0 Answers0