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;

Recommended Answers

All 4 Replies

Could you please post the whole section of code including variable declarations etc.

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;

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.