User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2006
Location: Canada
Posts: 4,472
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 269
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

User Input: Strings and Numbers [C++]

  #1  
Mar 7th, 2007
This thread is meant to facilitate comments about the tutorial entitled
User Input: Strings and Numbers [C++]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2002
Location: Lawn Guylen, NY
Posts: 10,889
Reputation: cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice cscgal is just really nice 
Rep Power: 32
Solved Threads: 109
Admin
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: User Input: Strings and Numbers [C++]

  #2  
Mar 18th, 2007
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?
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,472
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 269
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: User Input: Strings and Numbers [C++]

  #3  
Mar 21st, 2007
>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:
  1. int ch; // notice that this is int, not char, so it can hold larger values
  2. // (in case we get some crazy values from the input buffer)
  3. 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.
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: User Input: Strings and Numbers [C++]

  #4  
Mar 21st, 2007
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:
  1. int main()
  2. {
  3. int x;
  4. char ch;
  5. std::string myString;
  6. while (getline ( cin, myString ))
  7. {
  8. std::istringstream strin(myString);
  9. strin >> x;
  10. if (!strin)
  11. cout << "Bad input" << endl;
  12. else if ( strin >> ch )
  13. cout << "Bad input" << endl;
  14. else
  15. cout << "You entered: " << x << endl;
  16. }
  17. getchar ();
  18. return 0;
  19. }

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."
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,472
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 269
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: User Input: Strings and Numbers [C++]

  #5  
Mar 22nd, 2007
>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,
  1. 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...
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,811
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: User Input: Strings and Numbers [C++]

  #6  
Mar 22nd, 2007
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...
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."
Reply With Quote  
Join Date: Apr 2006
Location: Canada
Posts: 4,472
Reputation: John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light John A is a glorious beacon of light 
Rep Power: 16
Solved Threads: 269
Moderator
Staff Writer
Featured Blogger
John A's Avatar
John A John A is offline Offline
Vampirical Moderator

Re: User Input: Strings and Numbers [C++]

  #7  
Mar 24th, 2007
>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...
tuxation.com - Linux articles, tutorials, and discussions
Reply With Quote  
Join Date: Mar 2008
Posts: 3
Reputation: WXuan is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
WXuan's Avatar
WXuan WXuan is offline Offline
Newbie Poster

Re: User Input: Strings and Numbers [C++]

  #8  
Mar 17th, 2008
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;
}
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C and C++ Tutorials Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C and C++ Tutorials Forum

All times are GMT -4. The time now is 9:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC