0

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.

8
Contributors
14
Replies
63
Views
4 Years
Discussion Span
Last Post by RonalBertogi
0

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! :-(

0

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.

0

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);
}
0

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.

0

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'.

Edited by Moschops

0

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?

0

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 )

Edited by Moschops

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

better?

Edited by yassink

0

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

0

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);
}
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.