Using getline in C

0 votes

I'm having trouble getting a message that the user types using the getline technique; I'm using something like:

string messageVar;
cout << "Type your message: ";
getline(cin, messageVar);

What's wrong with it if it doesn't stop to get the output value?

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
895 views

1 answer to this question.

0 votes

If you use getline() after cin >> anything, you must first flush the newline character from the buffer. 

You can achieve this by using the cin.ignore()

It would be something like this:

string messageVar;
cout << "Type your message: ";
cin.ignore(); 
getline(cin, messageVar);

Because the >> operator leaves a newline n character in the input buffer, this happens. 

When working with unformatted input, such as getline(), which reads until a newline character is discovered, this can be a problem. 

Because of that n that was left hanging there in your previous operation, it will immediately stop reading.

answered Jun 1, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

take string input using char* in C and C++

I'm having trouble accepting a string (actually a character array) as input.  Consider the following declaration: char* s; I have to input a string ...READ MORE

Jul 1, 2022 in C++ by Nicholas
• 7,760 points
408 views
+1 vote
0 answers

Parse (split) a string in C++ using string delimiter (standard C++)

In C++, I'm processing a string like follows: using namespace std; string parsed,input="text to be parsed"; stringstream ...READ MORE

Jul 4, 2022 in C++ by Nicholas
• 7,760 points
774 views
0 votes
0 answers

Using "super" in C++

The following phrase is part of my coding approach: class Derived : public Base { ...READ MORE

Jul 13, 2022 in C++ by Nicholas
• 7,760 points
736 views
0 votes
0 answers

How do I declare a 2d array in C++ using new?

How do I declare a two-dimensional array using new? For example, for a "typical" array, I would:      int* ary = new int[Size] but int** ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
488 views
0 votes
1 answer

Easiest way to convert int to string in C++

The C++11 version has introduced the std::stoi ...READ MORE

answered Feb 10, 2022 in Others by Rahul
• 9,680 points
527 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,385 views
0 votes
1 answer

How can I convert a std::string to int?

There are some new convert methods in C++ that convert std::string to a numeric type. As an alternative to str.c str() atoi(str.c str()) atoi(str.c str() you can make use of std::stoi std::stoi ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
688 views
0 votes
1 answer

How to reverse an std::string?

A reverse function is integrated into C++ and can be used to reverse a string.  This function accepts two parameters: The start iterator for the string The string iterator has come to an end. The following line of code demonstrates how to use this function: #include <iostream> //The library below must be included ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
538 views
0 votes
1 answer

Using :: (scope resolution operator) in C++

You're mostly correct regarding cout and cin. ...READ MORE

answered Jun 27, 2022 in C++ by Damon
• 4,960 points
556 views
0 votes
1 answer

Declare abstract class in c++

An abstract class is one that is intended to be used as a base class .  At least one pure virtual function exists in an abstract class.  A pure virtual function is declared in the class declaration by using a pure specifier (= 0) in the declaration of a virtual member function. Here is an example of an abstract class: class AB { public: virtual void f() ...READ MORE

answered May 31, 2022 in C++ by Damon
• 4,960 points
562 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP