Write a function (seat_type) that reads an input from the user that represents a seat class. The user should enter F: for a first class seat, C: for a business class seat, or Y: for an economy seat. The function should validate the user input to accept only F, C, or Y.

Recommended Answers

All 14 Replies

School problem. Not allowed according to site's TOS. If you make an honest effort to solve this yourself, we can help you, but DO NOT ask us to do your work for you! :-(

Come on! That's so easy. If you would have peeked in any of the c++/c books or online tutorials, you woud have found an revealing example.
Use google, try with c++ tutorial. I'm sure you'll find the answer there.

that's what I came up with:

char seat_type (char s)

{   
    int z;

    cout<< "Please enter F for first class seat, C for business class seat or Y for economy seat: ";
    cin>>z;

    while ( (z != "F") || (z != "C") || (z != "Y") )
    {
        cout<<"Incorrect letter, please re-enter: ";
        cin>>z;
    }

    return (z);
}

while ( (z != "F") || (z != "C") || (z != "Y") )

Can you give me a value of z for which this will not be true? Any value at all - give me one letter that will make the while loop stop.

Any letter other than F, C or Y?

Any letter other than F, C or Y?

None of those letters will make it stop.

Also, you're trying to work with a single letter. Use the char type, not int. You'll make your life so much easier.

Also, "F" is a string - a pointer to a char. You don't want that. You want 'F'.

Yea, these are the only letters that are going to make the loop work. Any letter inserted other than these 3 capitalised letters will stop the loop. right?

Any letter inserted other than these 3 capitalised letters will stop the loop.

No. Nothing will make that loop stop. It will loop forever.

Let's say you enter the letter F.

(z != "F") - This is FALSE.
(z != "C") - This is TRUE.
(z != "Y") - This is TRUE.

So
while ( (z != "F") || (z != "C") || (z != "Y") )
is
while ( FALSE || TRUE || TRUE )

Alright, thank you

while ( (z != 'F') && (z != 'C') && (z != 'Y') )

better?

Yes, its better. I see u have "char s" as a parameter. What the reason for the parameter?

since you are getting the value entered in the function itself , there's no use
of the parameter (char s)

also the data type of z should be char and not int since you are storing
characters in it .

this should do the work :

char seat_type ()
{   
    char z;
    cout<< "Please enter F for first class seat, C for business class seat or Y for economy seat: ";
    cin>>z;
    while ( (z != 'F') && (z != 'C') && (z != 'Y') )
    {
        cout<<"Incorrect letter, please re-enter: ";
        cin>>z;
    }
    return (z);
}

...and to make the life of the user of your program easier, consider him wanting to type lower case F, C and Y.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.