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 in C++ by Nicholas
• 4,720 points
55 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 by Damon
• 4,760 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 in C++ by Nicholas
• 4,720 points
10 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 in C++ by Nicholas
• 4,720 points
22 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 in C++ by Nicholas
• 4,720 points
13 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 in C++ by Nicholas
• 4,720 points
19 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 in Others by Rahul
• 9,000 points
38 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 in C++ by Damon
• 4,760 points
47 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 in C++ by Damon
• 4,760 points
38 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 in C++ by Damon
• 4,760 points
32 views
0 votes
1 answer

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

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

answered Jun 27 in C++ by Damon
• 4,760 points
17 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 in C++ by Damon
• 4,760 points
29 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP