char conversion

This is a discussion on char conversion within the C++ Programming forums, part of the General Programming Boards category; I am trying to write a function that allows a user to enter an option from a menu. The menu ...

  1. #1
    Master of Puppets rwmarsh's Avatar
    Join Date
    Feb 2006
    Location
    Texas
    Posts
    96

    char conversion

    I am trying to write a function that allows a user to enter an option from a menu. The menu options are numbered from 1 to 9.
    I quickly found out that if I just did a cin>> with an int variable that the program would crash it a non number character was entered ( ie a through z ). So now I am trying to make the function so that it requests a char var. instead of a int type, then looks at the value entered. If it is an integer value it will return that number. If it is anything else it will display a warning and retry. Only thing is that I am stuck on how to determine if the value entered is an integer or a character. Here is what I have so far....

    Code:
    int getselection()
    {
        char selection;
        int x=0;
        do
        {
            cin>> selection;
            if ( /* if selection is an integer */   )
                { x=1; }
            else
                { cout<<"incorrect selection!"; }
        }
        while ( x==0 );
        return atoi(selection);
    }
    or is there just a better way?

    Using DEV-C++ under Windows XP

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Read user input as a string and (attempt to) convert.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    /* if selection is an integer */
    Everything stored in memory on a computer has to be stored as a number. So characters are stored as numeric codes. You assignment is to look in the back of your C++ book(or google for it) for an ASCII table. That table will list all the characters and their corresponding numeric codes. You can then use those codes in your if statement.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    no need to convert the char to anything. just use it as it is.
    Code:
    int getselection()
    {
        char selection;
        do
        {
            cin>> selection;
            switch(selection)
            {
                case '0':
                    break; // stop the loop
                case '1':
                   do_menu_one();
                   break;
                case '2':
                   do_menu_two();
                   break;
               default:
                   cout<<"incorrect selection!"; 
                   break;
            }
        }    while ( selection != '0' );
    
        return selection - '0'; // convert selection to binary
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Ancient Dragon
    no need to convert the char to anything.
    It only makes life simpler. You handle the next question dealing with the leftover newline screwing stuff up downstream, or the oddity of a negative value.
    Last edited by Dave_Sinkula; 02-24-2006 at 11:15 PM. Reason: ~/make/makes
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can then use those codes in your if statement.
    Actually you don't even have to use the codes, and since the codes will make your program more cryptic, use the actual chars. Since chars are stored as numeric codes you can compare them with <= and >=, e.g.
    Code:
    if(ch >= '1'  && ch <= '9')
    That compares the ascii code of ch to the ascii codes for the characters '1' and '9'. As an exercise, write the equivalent if statement with the actual numeric codes. Then you'll understand how the if statment above works.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    >> I quickly found out that if I just did a cin>> with an int variable that the program would crash it a non number character was entered ( ie a through z ).
    Code:
    int selection = 0;
    while (!(cin >> selection))
    {
      cin.clear();
      cin.ignore(1000, '\n');
      cout << "incorrect selection!";
    }
    return selection;
    That allows you to read in an integer without crashing when the user types in a letter. This will also allow you to have more than 1-9 as your menu options (any integer will do). You can add to the while control if you want to do more checking (e.g. for negative numbers).
    Last edited by Daved; 02-25-2006 at 01:06 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  2. Fixing Errors
    By brietje698 in forum Networking/Device Communication
    Replies: 9
    Last Post: 12-10-2007, 11:17 PM
  3. Replies: 6
    Last Post: 06-30-2005, 09:03 AM
  4. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 09:38 PM

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