Hello,
I need help to read entire string line with white spaces.
I tried many ways but they didn't work :|

If I remove cin>> company_name;the run jumps to std::cout << "Enter the symbol of the company: "; without completing reading company_name from the user!

AND,

If I keep cin>> company_name;the getline skips the first part from the string.

Thanks in advance for your hep :)

std::cout << "Enter the name of the company: ";
cin>> company_name;
//cin.clear();

 std::getline (cin,company_name, '\n');

   std::cout << "Enter the symbol of the company: ";
  std::cin >> company_symbol;

Edited 3 Years Ago by deceptikon: Changed to discussion thread

If you are mixing the >> operator and getline you need to use ignore() before calling getline. Give this a try

cin.ignore();  <-- added this
cout << "Enter the company name: ";
getline(cin, company_name);

Actually, I incidentally founded a solution, but I don't know how it works! :P
I had to add cin.get();
Any help to explain the work of cin.get() function
Thanks for your help

std::cout << "Enter the name of the company: ";
    cin.get();
    std::getline (std::cin,company_name);

       std::cout << "Enter the symbol of the company: ";
      std::cin >> company_symbol;

Edited 3 Years Ago by Sara Tech

The problem you are getting is that you are mixing your input types. Using the >> operator leaves the newline in the stream. When you call getline after that it reads in the newline and then stops. That is how getline works. It will keep reading untill it sees a newline.

This article has been dead for over six months. Start a new discussion instead.