Quest...?

This is a discussion on Quest...? within the C++ Programming forums, part of the General Programming Boards category; I'm working on this program and this is part of the code: Code: cout << "Enter seat (11f): "; cin ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    16

    Quest...?

    I'm working on this program and this is part of the code:
    Code:
    cout << "Enter seat (11f): ";
    cin >> p.row >> p.column;
    cin.ignore(INT_MAX,'\n');
    
    	while(cin.fail())
    	{
    	  cin.clear();
    	  cin.ignore(100,'\n');
    	  cout << "Enter seat (11f): ";
    	  cin >> p.row >> p.column;
    	}
    My problem is if the user enters something like (number and more than one letter) 5adffe the user should get an error.

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    u can use a while look to verify its the correct input. Heres an example code inside a function from one of my older bot versions.

    Code:
    int User_Info::get_age()
    {	
        /* Cin in while condition verifies the input is of
           its variable type. In this case, it verifies its
           a number.
        */
    	while (cout << "Enter age: " && !(cin >> age))
    	{
    		cout << "\nInvalid input\n";
    		cin.clear();
    		cin.ignore();
    	}

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    16
    The thing is that the number has to be between 1 and 13 and the letter between a and f , and both integer and character should be input together(NO SPACE).And what i'm looking for is a method that if there's more than and integer and a character to output incorrect or something.I'll guess i'll try something with the peek() to see if there's another character afterwards and if so to output incorrect except for the '\n'.

    Thanx

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Have you considered why you're doing this? A program shouldn't go into a failure state unless the input given cannot be used in normal processing. If you have the proper input followed by extraneous data, the program can still run normally. So you should rather be working on ways to handle the extra data if it would effect future input to the program. The simplest solution would be to throw it away.
    Code:
    #include <iostream>
    #include <limits>
    #include <string>
    
    using namespace std;
    
    struct seat_number {
      int row;
      char seat;
    };
    
    bool get_seat ( istream& in, seat_number& sn )
    {
      const string seats ( "abcdef" );
    
      if ( !( in>> sn.row ) || !in.get ( sn.seat ) )
        return false;
      if ( sn.row < 0 || sn.row > 13 || seats.find ( sn.seat ) == string::npos )
        return false;
      return true;
    }
    
    int main()
    {
      seat_number sn;
    
      cout<<"Enter seat number: ";
      for ( ; ; ) {
        if ( get_seat ( cin, sn ) )
          cout<<"You chose "<< sn.row << sn.seat <<endl;
        else {
          cin.clear();
          cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
          cerr<<"Input error\n";
        }
      }
    }
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >My problem is if the user enters something like (number and more than one letter) 5adffe the user should get an error.

    To me a completely different approach could be used to answer the above question. That is, to determine if the input (for example 5adffe) is in error you could accept the input as a single string. If the string is longer than 3 char or shorter than 2 char then it is an error. Then look at each char of string. All char should be alphanum. If not there was an error. If the first char (or two char of the input string if the string is 3 char long) is/are not digits then it is an error. If it is okay, then convert the digits to an int by converting the original string into two strings using abcdef as the parsing spots and converting the first string into an int. If the int is less than 0 or greater than 13 then it is an error. If there is more than 1 letter in the string then it is an error. If the letter is not between a and f then it is an error.

    I believe that >> will stop input into a numeric variable such as int when the first non digit char is encountered. Therefore parsing user input with >> will probably parse the leading digits from the letters for you. A call to get() will then get a single char from the input buffer for you, if there is one. However, it won't tell you if there is more than one char in the input buffer, which is what's needed to determine that 5ab or 5adffe are errors.

    Maybe rather than using a call to get() you could use another call to >> to a string variable. Then if the string variable was longer than 1 it is an error and if the first char of the string isn't between a and f it is an error. That may be the easiest way to do this.

    Oh, well, just my thoughts on an algorhythm to use to answer the question of whether 5adffe is an error. Use at your discretion.
    Last edited by elad; 03-17-2004 at 09:09 AM.

  6. #6
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Jut my take on how to do it.

    Code:
    	char ch;
    	p.row =14;
    	p.column ='g';
    	while((ch!='\n')|| ((p.row >13) || (p.column >'f')))
    	{
    		cout << "Enter seat (11f): ";
    		cin >> p.row;
    		cin.clear();
    		cin>>p.column;
    		ch=cin.get();
    
    		if(ch!='\n')  //added code
    		cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    
    
    	}
    		cout<<p.row  <<p.column <<endl;
    oops had to edit code so whenn user enters long string of letters and then a valid 11f it wouldn't cycle through the loop
    Last edited by manofsteel972; 03-17-2004 at 05:00 PM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MoveToEx() & LineTo() functions
    By csonx_p in forum Windows Programming
    Replies: 17
    Last Post: 04-11-2008, 12:53 AM
  2. Peasant's Quest Walkthrough
    By sean in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-25-2004, 06:46 AM
  3. Space Quest 7
    By sean in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 07-27-2004, 08:26 PM
  4. Quest for the Quests
    By sean in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-15-2002, 11:18 PM
  5. Space, Police, and King's quest.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-02-2002, 12:33 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21