Thread: checking multiple inputs

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    25

    checking multiple inputs

    is there a way i can have a series of inputs that are storing numbers into an array, but make it so if they type something other than a number like finish or done it will end and goto a function, im pretty sure i can do this but can i make it so i dont have to write an if statement checking for something else every time??

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Lightbulb

    Code:
    #include<iostream>       //provides cin & cout
    #include<cctype>         //provides  isdigit()
    #include<conio.h>        //provides clrcsr()     
    
    using namespace std;
    
    
    int main()
    {     
    
         int number_array[10];
         int temp;
    
          for(int j = 0; j < 10; j++)
         {
    
              clrscr();
    
              cout << "\n\nEnter an integer type  number to be added to the array: ";
     
              cin >> temp;
    
              if(isdigit(temp))
    
                   number_array[j] = temp;
    
              else
              {
    
                   function_call();   //replace this with the function call of your choice
    
                   break;
              }
    
         }
    
    return 0;
    
    }

    In this example, the isdigit( ) function (part of the <cctype> library) will return true if the user input is a number.. if the user input is something other than a number, then isdigit( ) will return false.. and the IF evaluation will short circuit and resolve to ELSE.

    I haven't tried this code.. but this is something I would start out with.

    Hope this helps
    Last edited by The Brain; 08-28-2004 at 03:06 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Using a little stl can go a long way with things like this.
    Code:
    template<class Type>
    std::istream & get_until(std::vector<Type> &v, const std::string &term=";",std::istream &is=std::cin) {
        Type element;
        std::string token;
        std::vector<Type> tmp;
        do {
            while(is >> element) {tmp.push_back(element);}
            if(is.fail()) is.clear();
        } while(is >> token && token != term);
        if(is) {v.swap(tmp);}
        return is;
    }
    This will work for any type that supports >> It also won't change the vector if it doesn't see the terminating string. It might be too agressive in that it will read until end of file if there is no terminator, and you might want to stop after the first error. Naturally you will need to specialize for a vector of string or char.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >#include<conio.h> //provides clrcsr()
    Not necessarily. conio is a nonstandard library, and it's contents will vary with each implementation. Why do you need to clear the screen anyway? That seems pretty antisocial to me in most cases.

    >cin >> temp;
    >if(isdigit(temp))
    And this works for you by some miracle? Let's take it bit by bit (yes it's a pun, yes it was intended). cin's >> operator converts values based on the type of the variable you pass to it, in this case int. So cin will fail if anything but a digit is entered and the contents of temp will be indeterminate. Now, if this indeterminate value happens to not be within the range of unsigned char, passing it to isdigit results in undefined behavior.

    So, let's come up with a solution and actually test it this time:
    Code:
    #include <iostream>
    #include <cstdlib>
    
    void bad_input()
    {
      std::cerr<<"Invalid input entered, bailing..."<<std::endl;
      std::exit ( EXIT_FAILURE );
    }
    
    int main()
    {
      int vals[10];
    
      for ( int i = 0; i < 10; i++ ) {
        if ( !( std::cin>> vals[i] ) )
          bad_input();
      }
    }
    This works, but it relies on a failure state and isn't very flexible if you want to perform different operations based on what the invalid input was. Fortunately, we can fix the second part easily by using a more robust function:
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <string>
    
    void stop_input ( int vals[], int n )
    {
      std::string stop;
    
      std::cin.clear(); // Make cin usable again
    
      // Read the extraneous data as a string
      // because we don't know how to convert it
      if ( std::getline ( std::cin, stop ) ) {
        // Valid stop condition
        if ( stop == "stop" ) {
          std::cout<<"You entered: ";
          for ( int i = 0; i < n; i++ )
            std::cout<< vals[i] <<' ';
          std::cout<<std::endl;
          std::exit ( EXIT_SUCCESS );
        }
        else { // Invalid condition
          std::cerr<<"Error: Invalid input"<<std::endl;
          std::exit ( EXIT_FAILURE );
        }
      }
    }
    
    int main()
    {
      int vals[10];
    
      for ( int i = 0; i < 10; i++ ) {
        if ( !( std::cin>> vals[i] ) )
          stop_input ( vals, i );
      }
    }
    The first part, where you rely on an error condition to direct potentially valid input, is considered by some to be bad style, but cleaning the stream and validating the data is relatively simple, so make your own decision about it.
    My best code is written with the delete key.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    My code was crushed... at the speed of sarcasm.


    Today officially marks the end of my "off the cuff" programming.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. checking inputs meets multiple conditions
    By 60beetle60 in forum C Programming
    Replies: 5
    Last Post: 04-19-2008, 08:25 AM
  3. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM
  4. multiple inputs of multiple types
    By frontz in forum C Programming
    Replies: 8
    Last Post: 01-19-2004, 02:57 PM
  5. Getting multiple inputs using cin
    By edk in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2001, 02:34 PM