Thread: How can I?

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

    How can I?

    Why I cannot use this methode to flush out the invalid input.
    I wanted to have the input part inside the while loop, unlike the one the FAQ show on the board(Inside the while condition).

    Code:
    #include <iostream>
    #include <cctype>
    #include <fstream>
    #include <limits>
    using namespace std;
    struct inventory_items
    {
    	int code,quantity,reorder_level;
    	char name[100], units[100];
    	double price;
    };
    
    int main()
    {
    	int counter=0;
    	inventory_items s;
    
    	while (counter==0)
    	{
      
    	  cout<<"Enter the item code.\n";
    	  cin>>s.code;
    	  cin.get();
    	  if(!isdigit(s.code))
    	  {
    		cout << "\nInvalid input" <<endl;
    		cin.clear();
    		cin.ignore(std::numeric_limits < int >::max(), '\n');
    	  }
    	  else
    	  {
    		  cout<<"You enter : "<<s.code<<endl;
    		  counter=1;
    	  }
    	}
      return 0;
    }

  2. #2
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You're using an int into s.code using >>. This will only work if there is an actual number being inputted (sp?). Otherwise it'll go into an infinite loop searching for a number. You should use a temporary char, and check that.
    Code:
    char temp;
    while(counter == 0)
    {
       cin.get(temp);
       if(!isdigit(temp))
          cout<<"Invalid input"<<endl;
       else
       {
          s.code = atoi(temp);
          cout<<"Correct. You entered "<<s.code<<endl;
       }
    }
    This will only get the first digit of the input. If you want more than that you'll have to make your own function to check an array of chars.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Or, an easier way to read a whole number:

    Code:
    #include <iostream>
    #include <cctype>
    #include <fstream>
    #include <limits>
    using namespace std;
    struct inventory_items
    {
    	int code,quantity,reorder_level;
    	char name[100], units[100];
    	double price;
    };
    
    int main()
    {
    	int counter=0;
    	inventory_items s;
    
    	while (counter==0)
    	{
      
    	  cout<<"Enter the item code.\n";
    	  cin>>s.code;
    	  if(!cin)
    	  {
    		cout << "\nInvalid input" <<endl;
    		cin.clear();
    		cin.ignore(std::numeric_limits < int >::max(), '\n');
    	  }
    	  else
    	  {
    		  cout<<"You entered : "<<s.code<<endl;
    		  counter=1;
    	  }
    	}
      return 0;
    }
    One note, if someone enters "10254rgh", it is seen as a valid input of "10254". Also, you're always going to leave something in the input stream, so you may want to do the clear/ignore no matter what:

    Code:
    int main()
    {
        int counter=0;
        inventory_items s;
    
        while (counter==0)
        {
      
          cout<<"Enter the item code.\n";
          cin>>s.code;
          if(!cin)
          {
            cout << "\nInvalid input" <<endl;
          }
          else
          {
              cout<<"You entered : "<<s.code<<endl;
              counter=1;
          }
          cin.clear();
          cin.ignore(std::numeric_limits < int >::max(), '\n');
        }
      return 0;
    }

Popular pages Recent additions subscribe to a feed