•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C and C++ Tutorials section within the Software Development category of DaniWeb, a massive community of 402,006 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,395 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 23326 | Replies: 7
![]() |
This thread is meant to facilitate comments about the tutorial entitled
User Input: Strings and Numbers [C++]
User Input: Strings and Numbers [C++]
•
•
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,889
Reputation:
Rep Power: 32
Solved Threads: 109
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?
Dani the Computer Science Gal
Do you run a computer-related website? Feature it in our niche link directory!
Do you run a computer-related website? Feature it in our niche link directory!
>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:
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.
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:
cplusplus Syntax (Toggle Plain Text)
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);
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.
tuxation.com - Linux articles, tutorials, and discussions
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:
Hope, it made sense, bye.
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:
cplusplus Syntax (Toggle Plain Text)
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.
Last edited by ~s.o.s~ : Mar 21st, 2007 at 10:31 pm.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
>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,
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...
Well, I know that it's far better to use something like this,
cplusplus Syntax (Toggle Plain Text)
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
>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...
tuxation.com - Linux articles, tutorials, and discussions
Considering that you have already declared
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...
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...
Last edited by ~s.o.s~ : Mar 22nd, 2007 at 10:29 pm.
"I don't accept change. I don't deserve to live."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
>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...
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...
tuxation.com - Linux articles, tutorials, and discussions
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.
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;
}
![]() |
•
•
•
•
•
•
•
•
DaniWeb C and C++ Tutorials Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- User Input: Strings and Numbers [C] (C and C++ Tutorials)
- error checking of user input (C++)
- user input into a string (C++)
- Need Help With Error Checking User Input (C)
Other Threads in the C and C++ Tutorials Forum
- Next Thread: User Input: Strings and Numbers [C]



Linear Mode