LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-07-2003, 11:35 PM   #1
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Rep: Reputation: 30
cin.ignore


Recently, I find a new problem in my previous posts, I start a new thead.
Code:
#include<iostream>
using namespace std;
const int size = 11;
void min ();
void try_again ();

int main () {
   cout<<"First, please let me say: best regards to you."
       <<"The program is run to guess a clase that contains two words, "
       <<"that contains eleven letter. Only the first one is capital. "
       <<"You could type a space between the two words, and finally, enter. "<<endl;
   min ();
}

void min () {
   char input[size];
   char result[] = "Best regards";
   char c;
   cout<<"Okay, now, give it a go->:";
   for (int i=0; i<size; i++) //initialize the array
      cin.get(input[i]);
   for (int i=0; i<size; i++) //compare with the result
      if (input[i]==result[i])
         cout<<"Letter "<<i+1<<" is good"<<endl;
      else
         cout<<"Sorry for letter "<<i+1<<endl;
   cin.ignore ();  //the cin is used to filter extra letters
   cout<<"Do you want ot try again, yet? y for yes, n for no."<<endl;
   try_again ();
}

void try_again () {
   char s;
   cin>>s;             
   cin.ignore ();  //the cin is used to filter extra letters
   if (s=='y')
      min ();
   else if (s=='n')
      exit (0);
   else
   {
      cout<<"Please enter y, or n:";
      try_again ();
   }
}
[root@localhost regards_b]# ./min_h
First, please let me say: best regards to you.The program is run to guess a clase that contains two words, that contains eleven letter. Only the first one is capital. You could type a space between the two words, and finally, enter.
Okay, now, give it a go->:Best regardsZ
Letter 1is good.
Letter 2is good.
Letter 3is good.
Letter 4is good.
Letter 5is good.
Letter 6is good.
Letter 7is good.
Letter 8is good.
Letter 9is good.
Letter 10is good.
Letter 11is good.
Do you want ot try again, yet? y for yes, n for no.
Please enter y, or n:ZZZ
Please enter y, or nPlease enter y, or n::n
[root@localhost regards_b]#
I want to use the first cin.ignore to throw out the letters that are after the eleventh one, but it doesn't. The letters that after the eleventh one have been accepted in try_again function.
I want to use the second cin.ignore to throw out the extra letters of the seonc cin, it doesn't too.
How to achieve my purpose?
Thank you.

Last edited by Xiangbuilder; 10-07-2003 at 11:42 PM.
 
Old 10-08-2003, 06:52 AM   #2
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
The prototype for ignore is this:

istream& ignore ( streamsize n = 1, int delim = EOF );

Its purpose is to extract (and discard) n characters, up to and including delim. Extraction stops at whatever limit is reached first.

So ... you need to add some arguments or you are just going to ignore a single character.

Just plucking some possible values at random, you could try std::numeric_limits<std::streamsize>::max() and '\n'.
 
Old 10-08-2003, 08:02 AM   #3
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you.
How to ignore all user's inputs between the twelfth one and the first enter?
I guess the difficult is the user's inputs is random.

Last edited by Xiangbuilder; 10-08-2003 at 08:05 AM.
 
Old 10-08-2003, 08:38 AM   #4
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
How to ignore all user's inputs between the twelfth one and the first enter?

If I understand correctly, then this is what you are already doing (if you used my earlier suggestion!):

When you call cin.get( ... ) or cin >> s - what you are doing here is asking for a character to be read from a buffer that is connected to the standard input (probably the terminal in which you are running the program).

The user has to put something here and has to signal to the program that something has been put there - in this case you want to use the alphanumeric keys followed by 'return' but it could be a file, output from another program, or anything that can be directed to stdin.

The calls to get( ... ) or cin >> s; will grab from this entered text a single character each. You make 11 calls to get( ... ), thus moving 11 characters from the buffer to the argument supplied to get( .. ).

Everything else in that buffer is, as far as you are concerned, junk and junk you want to get rid of. This is what the call to cin.ignore( ... ) does.

cin.ignore( numeric_limits<streamsize>::max(), '\n' ) will discard every character sitting in the buffer, up to and including the character at which it encounters your 'return' key press (seen as '\n' character).


Hope this helps but if this doesn't clarify things, can you post an example of how things are unsatisfactory?
 
Old 10-08-2003, 07:57 PM   #5
Xiangbuilder
Member
 
Registered: Apr 2003
Location: Shandong province China
Distribution: fedora core 1.0
Posts: 206

Original Poster
Rep: Reputation: 30
Thank you for your patience.
I modify
Code:
cin.ignore ();
to
Code:
cin.ignore( numeric_limits<streamsize>::max(), '\n' )
It works well.
Thank you.
Very helpful.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to cin >> string? kornerr Programming 2 11-01-2005 03:58 AM
C++ | cin.getline question... Mega Man X Programming 7 01-08-2004 10:34 AM
question concerning cin.ignore rootyard Programming 1 08-27-2003 02:01 PM
g++ and cin.getline() codeviking Programming 8 07-03-2003 07:42 AM
cin.getline adam_boz Programming 6 10-02-2002 08:57 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:09 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration