0

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 by deceptikon: Changed to discussion thread

3
Contributors
4
Replies
22
Views
3 Years
Discussion Span
Last Post by NathanOliver
1

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);
0

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 by Sara Tech

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.