Thread: Help with writing a function

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    31

    Help with writing a function

    I am trying to write a function that will take input from the user and compare it to a list of valid entries.

    Code:
    #include <iostream.h>
    
    char GetChoice(const String, &LegalChoices);
    
    /*Forces the user to enter a character in LegalChoices and returns it
      Post: Character in ValidChoices returned							*/
    
    char *ValidChoices = "ABCabc";
    
    char GetChoice(char *ValidChoices)
    {
     char Choice;
     int cx;
     int valid = FALSE;
    
     while (!valid)
     {
      cout<<"Enter your choice (A, B, or C):";
      cin>>Choice;
    
      cx = 0;
      while (ValidChoices[cx] != '\0')
      {
       if (Choice == ValidChoices[cx])
       {
        valid = TRUE;
        break;
       }
       cx ++;
      }
      if (!valid) "Invalid Choice";
     }
    
     return Choice;
    }
    This is the code I am trying to use in the function. I am getting 3 errors:

    Error in this line
    Code:
    char GetChoice(const String, &LegalChoices);
    This line
    Code:
     int valid = FALSE;
    and this line
    Code:
    valid = TRUE;
    I don't see what I am missing maybe because I have been staring at this for a few hours. Can someone point what I am doing wrong? Or if there is a better way for me to do this. i am using this in many sections of my code and I am just trying to make this easier then writing it multiple times.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What errors are you getting?

    EDIT: I just noticed, change this:
    Code:
    char GetChoice(const String &LegalChoices);
    You had a comma in there.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    There's plenty wrong with this, mostly in your prototype and definition. Then there's the fact that FALSE and TRUE haven't been defined. Try something like this:
    Code:
    #include <iostream>
    
    char getChoice ( const char *valid )
    {
      char c = 0;
      bool done = false;
    
      while ( !done && std::cin.get ( c ) ) {
        for ( const char *p = valid; *p != '\0'; p++ ) {
          if ( c == *p ) {
            done = true;
            break;
          }
        }
      }
    
      return c;
    }
    
    int main()
    {
      std::cout<< getChoice ( "ABCabc" ) <<std::endl;
    }
    Or if you can stand using the STL:
    Code:
    #include <iostream>
    #include <algorithm>
    
    char getChoice ( const char *valid, int size )
    {
      char c;
      const char *end = valid + size;
    
      while ( std::cin.get ( c ) ) {
        if ( std::find ( valid, end, c ) != end )
          return c;
      }
    
      return 0; // Or another suitable failure value
    }
    
    int main()
    {
      std::cout<< getChoice ( "ABCabc", 6 ) <<std::endl;
    }
    Last edited by Prelude; 11-14-2003 at 05:20 PM.
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    31
    Thanks for the help. Those options are a much better way I think. Mine was kind of the extra long way.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    Code:
    #include <iostream>
    #include <string>
    #include <limits>
    
    using namespace std;
    
    char getChoice()
    {
        string validChoices("abcABC");
        char userInput;
        while(true){
            cout << "Enter a character: " << flush;
            cin >> userInput;
            cin.ignore( numeric_limits<streamsize>::max(), '\n' );
            if(validChoices.find(userInput) != string::npos){
                return userInput;
            } else {
                cout << "Invalid choice. ";
            }
        }
    }
    
    int main()
    {
        char userSelection = getChoice();
        cout << "You entered: " << userSelection << endl;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM