Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,427 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,881 people online right now. Registration is fast and FREE... Join Now!




C++ char variable validation

 
Reply to this topicStart new topic

C++ char variable validation

Denise423
26 Jan, 2008 - 12:30 PM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 2


My Contributions
This program works fine with one exception, if the user enters more than one character, I would like the invalid user entry message to show. It does this if anything other than a letter is entered but will not if the user enters two or more letters - the first letter is picked up and the proper alpha is displayed. This isn't real important, I just want to know how I can make this a little cleaner.


CODE

//Write a C++ program that inputs a letter and outputs the corresponding
//International Civil Aviation Organization alphabet word using functional
//decomposition.

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout<<"\t\tInternational Civil Aviation Organization Alphabet\n";
        
    //declare variables
    char letter;    //store user input
    char choice;    //store user choice to continue using program or exit the program.

    do        //start do loop for user multiple entries
    {
        //instructions for user input
        cout<<"Please enter a letter, (A thru Z), that you would like to convert"<< endl;
        cout<<"to an International Civil Aviation Organization alphabet word"<< endl;
        cout<<"and press enter\t"<<endl;
        cout<<"\n\n\t";

        cin>> letter;        //user input

        cout<<"\n\n";

            if (letter == 'A' || letter == 'a')
                cout<<"\nThe alphabet word is:  Alpha"<< endl;
            else
                if (letter == 'B' || letter == 'b')
            cout<<"\nThe alphabet word is:  Beta"<< endl;
            else
                if (letter == 'C' || letter == 'c')
                    cout<<"\nThe alphabet word is:  Charlie"<< endl;
            else
                if (letter == 'D' || letter == 'd')
                    cout<<"\nThe alphabet word is:  Delta"<< endl;
            else
                if (letter == 'E' || letter == 'e')
                    cout<<"\nThe alphabet word is:  Echo"<< endl;
            else
                if (letter == 'F' || letter == 'f')
                    cout<<"\nThe alphabet word is:  Foxtrot"<< endl;
            else
                if (letter == 'G' || letter == 'g')
                    cout<<"\nThe alphabet word is:  Golf"<< endl;
            else
                if (letter == 'H' || letter == 'h')
                    cout<<"\nThe alphabet word is:  Hotel"<< endl;
            else
                if (letter == 'I' || letter == 'i')
                    cout<<"\nThe alphabet word is:  India"<< endl;
            else
                if (letter == 'J' || letter == 'j')
                    cout<<"\nThe alphabet word is:  Juliet"<< endl;
            else
                if (letter == 'K' || letter == 'k')
                    cout<<"\nThe alphabet word is:  Kilo"<< endl;
            else
                if (letter == 'L' || letter == 'l')
                    cout<<"\nThe alphabet word is:  Lima"<< endl;
            else
                if (letter == 'M' || letter == 'm')
                    cout<<"\nThe alphabet word is:  Mike"<< endl;
            else
                if (letter == 'N' || letter == 'n')
                    cout<<"\nThe alphabet word is:  November"<< endl;
            else
                if (letter == 'O' || letter == 'o')
                    cout<<"\nThe alphabet word is:  Oscar"<< endl;
            else
                if (letter == 'P' || letter == 'p')
                    cout<<"\nThe alphabet word is:  Papa"<< endl;
            else
                if (letter == 'Q' || letter == 'q')
                    cout<<"\nThe alphabet word is:  Quebec"<< endl;
            else
                if (letter == 'R' || letter == 'r')
                cout<<"\nThe alphabet word is:  Romeo"<< endl;
            else
                if (letter == 'S' || letter == 's')
                    cout<<"\nThe alphabet word is:  Sierra"<< endl;
            else
                if (letter == 'T' || letter == 't')
                    cout<<"\nThe alphabet word is:  Tango"<< endl;
            else
                if (letter == 'U' || letter == 'u')
                    cout<<"\nThe alphabet word is:  Uniform"<< endl;
            else
                if (letter == 'V' || letter == 'v')
                    cout<<"\nThe alphabet word is:  Victor"<< endl;
            else
                if (letter == 'W' || letter == 'w')
                    cout<<"\nThe alphabet word is:  Whiskey"<< endl;
            else
                if (letter == 'X' || letter == 'x')
                    cout<<"\nThe alphabet word is:  X-Ray"<< endl;
            else
                if (letter == 'Y' || letter == 'y')
                    cout<<"\nThe alphabet word is:  Yankee"<< endl;
            else
                if (letter == 'Z' || letter == 'z')
                    cout<<"\nThe alphabet word is:  Zulu"<< endl;
            else
                cout<<"Invalid entry. Please enter one letter, A thru Z"<< endl;
            //endif;

            cout<<"\n\nWould you like to enter another letter?   (Y/N)\t";
            cin>> choice;
            cout<<"\n\n";
    }while (choice == 'Y' || choice == 'y');
    
    cout<<"\n\nThank you for trying my program\n\n";
    
    return 0;
}

User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: C++ Char Variable Validation
26 Jan, 2008 - 02:46 PM
Post #2

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

You can read it in as a string, then check to make sure that the length of the string equals 1. If not, assign letter to some illegal character (I picked exclamation point). That'll print the error message. If the length is one, assign letter to be the first character of the string. Thus, change this input code:

CODE

        cin>> letter;        //user input


to this:

CODE

        string userInput;
        cin>> userInput;        //user input
        if (userInput.length () == 1)
            letter = userInput[0];    // user entered one character.  Assign to letter.
        else
            letter = '!';    // denotes bad input



User is offlineProfile CardPM
+Quote Post

Denise423
RE: C++ Char Variable Validation
26 Jan, 2008 - 03:38 PM
Post #3

New D.I.C Head
*

Joined: 13 Apr, 2007
Posts: 2


My Contributions
VernonDozier,

Thank you! It works perfectly. I knew I could control length with a string but I didn't think about using a string and then reassigning it back to the char variable.

Thanks for the assist, much appreciated!

Denise
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:48AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month