1,105,386 Community Members

User Input: Strings and Numbers [C++]

Member Avatar
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
 
10
 

User input/output is a key component in the programs you write, yet most online tutorials just provide a quick-and-dirty method of grabbing data. In other words, it works right up until the point the user does something unexpected.

In this tutorial you will learn how to avoid the common mistakes by doing it properly.

Take this typical method that tutorials use to teach user input:

int number;
cin >> number;
cout << "You typed the number " << number << ".\n";

Now let's say you want the user to enter a string that contains a space in it. Since using cin >> myString won't work (it will only grab the first word and then exit), you decide you need to find a function that will grab a whole line of input.

After some Googling, you decide that getline() is probably the best way to do it. You add it to your existing code, and this is how your entire program looks:

#include <iostream>
#include <string>
using namespace std;

int main() {
    
    int number;
    string name;
    
    cout << "Please enter a number." << endl;
    cin >> number;
    cout << "Enter your entire name (first and last)." << endl;
    getline(cin, name);
    
    cout << "Your full name is " << name 
         << ", and the number you entered is " << number << endl;
    return 0;
}

Now you run it and the program totally skips the getline() statement!

Please enter a number.
5
Enter your entire name (first and last).
Your full name is , and the number you entered is 5

That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead):

#include <iostream>
#include <string>
using namespace std;

int main() {
    
    string number;
    string name;
    
    cout << "Please enter a number." << endl;
    getline(cin, number);
    cout << "Enter your entire name (first and last)." << endl;
    getline(cin, name);
    
    cout << "Your full name is " << name
         << ", and the number you entered is " << number << endl;
    return 0;
}

The program's output being:

Please enter a number.
5
Enter your entire name (first and last).
 Joe Programmer
Your full name is Joe Programmer, and the number you entered is 5

The problem went away.

You will probably suspect something is up with cin and are right. What's actually happening in the first example is:

  1. You enter something in.
  2. You hit return.
  3. All of this is put into the input buffer (including the newline that results from hitting return).
  4. cin grabs whatever it needs (in this case the number), but leaves the newline behind!
  5. Since getline() only grabs 1 line, all it gets is the newline left behind by cin .

To fix this the best approach, although not that easy for newbies, is to avoid the use of cin until you know what you're doing, and let getline() handle your user input. It not only solves the newline problem, but it also solves a number of other problems induced by using cin .

This is relatively easy for people to do until they get to numbers (which usually can't be read into strings because they need to be manipulated). For this, you can use a stringstream to convert the string back into a number:

#include <iostream>
#include <string>
#include <sstream> // need this for stringstreams!
using namespace std;

int main() {
    
    int number;
    string line;
    stringstream stream;
    
    cout << "Please enter a number." << endl;
    getline(cin, line);
    
    stream << line;
    stream >> number;
    cout << "The number entered was " << number << ".\n";
    
    return 0;
}

This outputs:

Please enter a number.
4
The number entered was 4.

You can now see how the operation is nearly identical, except that you are taking an extra step by first putting the input a string, and then putting it into a separate stream before popping it back out in the number.

Finally, you actually can mix cin with getline() , if you know what you are doing. The easiest method is to use cin.ignore([I]<some large number>[/I], '\n') to clear the input buffer. You could simply hard code a large number, although it's usually better to use a built-in constant. The Standard Template Library provides such a constant, which is found in the <limits> header: numeric_limits<streamsize>::max() If you were to implement this in your first example, it would work like this:

#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main() {
    
    int number;
    string name;
    
    cout << "Please enter a number." << endl;
    cin >> number;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Enter your entire name (first and last)." << endl;
    getline(cin, name);
    
    cout << "Your full name is " << name
         << ", and the number you entered is " << number << endl;
    return 0;
}

And the output would be:

Please enter a number.
3
Enter your entire name (first and last).
Joe Programmer
Your full name is Joe Programmer, and the number you entered is 3

However, this should only be used when absolutely necessary, as this is just a band-aid for the function. It doesn't really fix some of the other problems that come with using it. So perhaps it is best to stick with getline() . Don't do the quick-and-dirty method that online tutorials teach you, because it will eventually come back and bite you in the back of the neck. Flushing the input buffer like mentioned previously is only a last resort for using cin ; it's not a good alternative to using getline() to handle your input. A little bit of trouble now using getline() will save you a lot of pain later!

Update: Narue has written a nice tutorial showing how to clear the input buffer and all the problems associated with doing so: How do I flush the input stream?

Member Avatar
Dani
The Queen of DaniWeb
20,569 posts since Feb 2002
Reputation Points: 1,356 [?]
Q&As Helped to Solve: 931 [?]
Skill Endorsements: 204 [?]
Administrator
Featured
Sponsor
 
1
 

Can you explain what cin.ignore actually does, and why we need to specify some randomly large number? Is there no command to just clear the input buffer of the newline character?

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
1
 

>Can you explain what cin.ignore actually does
cin.ignore basically ignores (another way of saying 'trashes') characters from the input buffer.

>why we need to specify some randomly large number?
That number is the maximum number of characters that cin.ignore trashes before it encounters a newline character. Theroretically, you would only need 1 to trash a newline, but you never know what else is sitting in the buffer, and this is a pretty safe way of emptying it.

>Is there no command to just clear the input buffer of the newline character?
You could simply use cin.get() to manually remove character-by-character from the input buffer, like this:

int ch;  // notice that this is int, not char, so it can hold larger values
         // (in case we get some crazy values from the input buffer)
while ((ch = cin.get()) != '\n' && ch != EOF);

This method isn't as reliable for clearing the input buffer though, so it's better to stick with cin.ignore().

Additionally, it's a good idea to call cin.clear() before clearing the input buffer, because what cin.clear does is erase any bad bits (errors) that have occured. If there are errors that haven't been cleared, you can't do read (or clear) the input buffer.

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

Coding the tutorial with the magic number given to ignore function would not be such a good idea.

Either declare the constant at the start of the program, in a header file or better yet use inbuilt constants like max value provided in the limits header file.

Another robust method for accepting a integer worth incorporating in your tutorial is:

int main()
{
    int x;
    char ch;
    std::string myString;
    while (getline ( cin, myString ))
    {
        std::istringstream strin(myString);
        strin >> x;
        if (!strin)
            cout << "Bad input" << endl;
        else if ( strin >> ch )
            cout << "Bad input" << endl;
        else
            cout << "You entered: " << x << endl;
    }
    getchar ();
    return 0;
}

Hope, it made sense, bye.

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 

>Coding the tutorial with the magic number given to ignore function would not be such a good idea.

Well, I know that it's far better to use something like this,

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

but I didn't want to scare newbies with a long chunk of code, when I was simply trying to demonstrate clearing the input buffer. But if you think it should be changed, I can get Davey to edit it...

>Another robust method for accepting a integer worth incorporating in your tutorial is:
I wanted to keep the tutorial relatively simple, so I didn't really cover input validation. I'm considering writing some code snippets that would accomodate this (and perhaps link to it at the end of the tutorial, much like Dave Sinkula did in his excellent C input tutorial).

~s.o.s~, thank you very much for your suggestions...

Member Avatar
~s.o.s~
Failure as a human
10,399 posts since Jun 2006
Reputation Points: 2,496 [?]
Q&As Helped to Solve: 992 [?]
Skill Endorsements: 72 [?]
Administrator
Featured
 
0
 

Considering that you have already declared using namespace std at the top of your code, the complicated statement becomes cin.ignore(numeric_limits<streamsize>::max(), '\n') ;

Not so complicated, is it ? ;) But even so please make the changes since we want publication quality tutorials and we don't need the magic numbers to mar that.

> I wanted to keep the tutorial relatively simple, so I didn't really cover input validation.
No problem as such, include the code which I pasted whenever you think its appropriate. Just wanted to let you know.

And last but not the least, really good tutorial. I am waiting for the next one in the series... ;)

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 

>Not so complicated, is it ?
Well, I was kind of trying to exaggerate the effect of using that statement by adding the std:: prefix to make my point... ;)

>But even so please make the changes since we want publication quality tutorials and we don't need the magic numbers to mar that.
Alrighty then, I'll make some modifications, and then send them to Davey for approval.

>And last but not the least, really good tutorial.
Thank you!

>I am waiting for the next one in the series...
When I have some more time I'll get around to it...

Member Avatar
WXuan
Newbie Poster
4 posts since Mar 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Read the content, I realized that the convinence of the function getline().
I have been using the following method to process input containing number and string.

#include<iostream>
#include<string>
using namespace std;
int main()
{
int number;
string str,remainder;
cout<<"Please enter a number:"<<endl;
cin>>number;
getline(cin,remainder);
getline(cin,str);
cout<<"The string you entered is "<<str<<", and the number you entered is "<<number<<endl;
return 0;
}

Member Avatar
grisha83
Junior Poster in Training
70 posts since Sep 2008
Reputation Points: 26 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

It would be nice if you talked about the possible manipulations that we could do with strings.
Thank you

Member Avatar
abhijitm
Newbie Poster
6 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi ,

Not sure if you guys are not considering "fflush" on purpose , but you can even use to fflush the console buffer before getting the line to fflush the "Enter" which is present in the buffer after entering the number.
I am modifying the original piece of code and presenting it below

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
    
    int number;
    string name;
    
    cout << "Please enter a number." << endl;
    cin >> number;
    cout << "Enter your entire name (first and last)." << endl;
    fflush(stdin);
    getline(cin, name);
    
    cout << "Your full name is " << name 
         << ", and the number you entered is " << number << endl;
         
    getch() ; // to hold the output screen
    return 0;
}

The output is
Please enter a number.
4
Enter your entire name (first and last).
Abhijit Mehta
Your full name is Abhijit Mehta, and the number you entered is 4

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 

>Not sure if you guys are not considering "fflush" on purpose
Actually, yes... http://www.gidnetwork.com/b-57.html

Member Avatar
V.V.Raman
Newbie Poster
5 posts since Jan 2009
Reputation Points: -7 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

My question is regarding he use of numeric_limits<streamsize>::max().
I found that this compiled in the command line using the C++ command in Bloodshed Dev-C++. However, when I tried to compile this code in a console project in Visual C++ 6.0, I was successful only after including headers for <stream> and <limits>. Why this difference?
Raman

Member Avatar
John A
Vampirical Lurker
5,285 posts since Apr 2006
Reputation Points: 1,896 [?]
Q&As Helped to Solve: 408 [?]
Skill Endorsements: 12 [?]
Team Colleague
 
0
 

>Why this difference?
Well, first of all, Visual C++ 6.0 is a fairly old compiler and doesn't really conform to proper C++ standards. You should really get a newer compiler (Try the Express edition of Visual Studio 2008).

Second, some compilers' headers include other headers, some don't. In this case, because I was using the STL objects string, stringstream, cout, numeric_limits<T>, it means I should have also included the following headers in the code:

#include <string>
#include <limits>

Those headers are included by default from iostream in gcc. But you are correct, I should update the tutorial.

Member Avatar
V.V.Raman
Newbie Poster
5 posts since Jan 2009
Reputation Points: -7 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

>Why this difference?
Well, first of all, Visual C++ 6.0 is a fairly old compiler and doesn't really conform to proper C++ standards. You should really get a newer compiler (Try the Express edition of Visual Studio 2008).

Second, some compilers' headers include other headers, some don't. In this case, because I was using the STL objects string, stringstream, cout, numeric_limits<T>, it means I should have also included the following headers in the code:

#include <string>
#include <limits>

Those headers are included by default from iostream in gcc. But you are correct, I should update the tutorial.

Thank you for the explanation, John.
Raman

Member Avatar
bemboysms
Newbie Poster
2 posts since Jul 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

are the codes used in the c++ programming same with the dev-c++ programming, coz im using the latter, and will you give me the list of header files and their use and algorithms, i am a fully first time programmer, thanks

Member Avatar
hiyas
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

God Bless

Member Avatar
wyujack
Newbie Poster
1 post since Oct 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

User input/output is a key component in the programs you write, yet most online tutorials just provide a quick-and-dirty method of grabbing data. In other words, it works right up until the point the user does something unexpected.

In this tutorial you will learn how to avoid the common mistakes by doing it properly.

Take this typical method that tutorials use to teach user input:

int number;
cin >> number;
cout << "You typed the number " << number << ".\n";

Now let's say you want the user to enter a string that contains a space in it. Since using cin >> myString won't work (it will only grab the first word and then exit), you decide you need to find a function that will grab a whole line of input.

After some Googling, you decide that getline() is probably the best way to do it. You add it to your existing code, and this is how your entire program looks:

#include <iostream>
#include <string>
using namespace std;

int main() {
    
    int number;
    string name;
    
    cout << "Please enter a number." << endl;
    cin >> number;
    cout << "Enter your entire name (first and last)." << endl;
    getline(cin, name);
    
    cout << "Your full name is " << name 
         << ", and the number you entered is " << number << endl;
    return 0;
}

Now you run it and the program totally skips the getline() statement!

Please enter a number.
5
Enter your entire name (first and last).
Your full name is , and the number you entered is 5

That's rather odd. Let's try using only getline for our input (which means the number will be put into a string instead):

#include <iostream>
#include <string>
using namespace std;

int main() {
    
    string number;
    string name;
    
    cout << "Please enter a number." << endl;
    getline(cin, number);
    cout << "Enter your entire name (first and last)." << endl;
    getline(cin, name);
    
    cout << "Your full name is " << name
         << ", and the number you entered is " << number << endl;
    return 0;
}

The program's output being:

Please enter a number.
5
Enter your entire name (first and last).
 Joe Programmer
Your full name is Joe Programmer, and the number you entered is 5

The problem went away.

You will probably suspect something is up with cin and are right. What's actually happening in the first example is:

  1. You enter something in.
  2. You hit return.
  3. All of this is put into the input buffer (including the newline that results from hitting return).
  4. cin grabs whatever it needs (in this case the number), but leaves the newline behind!
  5. Since getline() only grabs 1 line, all it gets is the newline left behind by cin .

To fix this the best approach, although not that easy for newbies, is to avoid the use of cin until you know what you're doing, and let getline() handle your user input. It not only solves the newline problem, but it also solves a number of other problems induced by using cin .

This is relatively easy for people to do until they get to numbers (which usually can't be read into strings because they need to be manipulated). For this, you can use a stringstream to convert the string back into a number:

#include <iostream>
#include <string>
#include <sstream> // need this for stringstreams!
using namespace std;

int main() {
    
    int number;
    string line;
    stringstream stream;
    
    cout << "Please enter a number." << endl;
    getline(cin, line);
    
    stream << line;
    stream >> number;
    cout << "The number entered was " << number << ".\n";
    
    return 0;
}

This outputs:

Please enter a number.
4
The number entered was 4.

You can now see how the operation is nearly identical, except that you are taking an extra step by first putting the input a string, and then putting it into a separate stream before popping it back out in the number.

Finally, you actually can mix cin with getline() , if you know what you are doing. The easiest method is to use cin.ignore([I]<some large number>[/I], '\n') to clear the input buffer. You could simply hard code a large number, although it's usually better to use a built-in constant. The Standard Template Library provides such a constant, which is found in the <limits> header: numeric_limits<streamsize>::max() If you were to implement this in your first example, it would work like this:

#include <iostream>
#include <string>
#include <limits>
using namespace std;

int main() {
    
    int number;
    string name;
    
    cout << "Please enter a number." << endl;
    cin >> number;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    cout << "Enter your entire name (first and last)." << endl;
    getline(cin, name);
    
    cout << "Your full name is " << name
         << ", and the number you entered is " << number << endl;
    return 0;
}

And the output would be:

Please enter a number.
3
Enter your entire name (first and last).
Joe Programmer
Your full name is Joe Programmer, and the number you entered is 3

However, this should only be used when absolutely necessary, as this is just a band-aid for the function. It doesn't really fix some of the other problems that come with using it. So perhaps it is best to stick with getline() . Don't do the quick-and-dirty method that online tutorials teach you, because it will eventually come back and bite you in the back of the neck. Flushing the input buffer like mentioned previously is only a last resort for using cin ; it's not a good alternative to using getline() to handle your input. A little bit of trouble now using getline() will save you a lot of pain later!

Update: Narue has written a nice tutorial showing how to clear the input buffer and all the problems associated with doing so: How do I flush the input stream?

It's very usefull to me .thank you !

Member Avatar
chaithanya07
Newbie Poster
1 post since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-2
 

why do we use scope resolution operator?? explain about it please.....

Member Avatar
p-man
Newbie Poster
1 post since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 
#include <iostream>
#include <string>



int main()
{
int number;
string str,remainder;
cout<<"Please enter a number:"<<endl;
cin>>number;
cin>>remainder>>str;

cout<<"The string you entered is "<<str<<",and the number you entered is "<<number<<endl;
int v;
cin>>v;
return 0;
}
Member Avatar
ilyas0121
Newbie Poster
1 post since Jun 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

here is a nice tutorial on this with some more details:
http://augustcouncil.com/~tgibson/tutorial/iotips.html

You
Post:
Start New Discussion
Tags Related to this Article