accept upper or lower case Y/n

Discussion in 'C++' started by David, May 4, 2004.

  1. David

    David Guest

    What can I do to accept either uppercase or lower case " y or n" in the
    program below.?
    any help will be appreciated

    #include <iostream>
    using namespace std;

    //class definigtion
    class Pizza
    {
    public:
    Pizza (const char top [] = "cheese", const int size = 12, const double
    Price = 8.99);
    void setValue();
    void displayValues();

    private:
    char topping [20];
    int diameter;
    double price;
    };

    //constructor
    Pizza::pizza (const char top[], const int size, const double Price)
    {
    strcpy (topping, top);
    diameter = size;
    price = Price;
    }
    //set pizza function
    void Pizza::setValue()
    {
    cout << "Please enter your choice of topping: "<<'\n';
    cout<<'\n';
    cin>>topping;
    cout<< '\n';
    cout<< "Please enter your choice of size: "<<'\n';
    cout<< '\n';
    cin>>diameter;
    cout<<'\n';

    if (topping != "cheese")
    price = 9.99;

    if (diameter > 12)
    price = price + 1.5;


    }
    //call pizza function
    void Pizza::displayValues ()
    {
    cout<<"Your choice of pizza is a " <<topping<< " " <<diameter<< "in. pizza
    for " "$" <<price<<'\n';
    }
    //start the main engine
    void main()
    {
    char option[2];

    Pizza p;
    cout<< '\n';
    cout<< '\n';
    cout<< " MAHARAJA'S PIZZA" <<'\n\n';
    cout<< "Do you want a cheese 12 in. pizza? y/n: ";
    cin>> option;

    if (option[0] == 'n')
    p.setValue();
    p.displayValues();





    }
    David, May 4, 2004
    #1
    1. Advertising

  2. David

    Sharad Kala Guest

    "David" <> wrote in message news:uJFlc.362252$Ig.133228@pd7tw2no...
    > What can I do to accept either uppercase or lower case " y or n" in the
    > program below.?


    {snip]
    > //start the main engine
    > void main()


    Return type of main is int. It's undefined behavior if you change it to anything
    else.

    > {
    > char option[2];
    >
    > Pizza p;
    > cout<< '\n';
    > cout<< '\n';
    > cout<< " MAHARAJA'S PIZZA" <<'\n\n';
    > cout<< "Do you want a cheese 12 in. pizza? y/n: ";
    > cin>> option;

    Why do you need a char array for option?

    > if (option[0] == 'n')

    Simple, check for both 'n' or 'N' i.e. if(option=='n' || option == 'N')

    -Sharad
    Sharad Kala, May 4, 2004
    #2
    1. Advertising

  3. "David" <> wrote in message
    news:uJFlc.362252$Ig.133228@pd7tw2no...
    > What can I do to accept either uppercase or lower case " y or n" in the
    > program below.?


    Sharad's already answered that. I'll just point out another problem in your
    code

    > any help will be appreciated
    >
    >
    > if (topping != "cheese")
    > price = 9.99;


    This does not do what you think it does. If you use char arrays for strings
    then you must use strcmp to compare strings (just like you must use strcpy
    to copy strings).

    if (strcmp(topping, "cheese") != 0)
    price = 9.99;

    A better way is to use std::string for strings. Then you can use == for
    equality and = for copying, just like everything else.

    #include <string>
    using namespace std;

    class Pizza
    {
    public:
    Pizza (const string& top = "cheese", int size = 12, double Price = 8.99);
    void setValue();
    void displayValues();

    private:
    string topping;
    int diameter;
    double price;
    };

    It's good to get into good habits early.

    john
    John Harrison, May 4, 2004
    #3
  4. "David" <> wrote:
    > What can I do to accept either uppercase or lower case " y or n" in the
    > program below.?
    > (snip)
    > cout<< " MAHARAJA'S PIZZA" <<'\n\n';
    > cout<< "Do you want a cheese 12 in. pizza? y/n: ";
    > cin>> option;
    >
    > if (option[0] == 'n')
    > p.setValue();
    > p.displayValues();


    Your code doesn't test for 'y' or 'Y' or 'N'; just for 'n'.

    So if you want to test for the others... why not just test for the others?
    In the same way that you test for 'n'?

    Hints: "==" tests for equality, and you can use either "||" or "or" as
    the "or" connective.

    // fruit-test.cpp
    #include <string>
    #include <iostream>

    bool IsYummyFruit(const std::string& sprat)
    {
    return sprat == "orange" or sprat == "apple" or sprat == "banana";
    }

    bool IsYuckyFruit(const std::string& sprat)
    {
    return sprat == "guava" || sprat == "persimmon" || sprat == "breadfruit";
    }

    int main(int argc, char* argv[])
    {
    using std::string;
    using std::cout;
    using std::endl;
    string Object (argv[1]);
    if (IsYummyFruit(Object))
    {
    cout << "Yummy!" << endl;
    }
    else if (IsYuckyFruit(Object))
    {
    cout << "Eww, yuck!" << endl;
    }
    else
    {
    cout << "YOU CAN\'T EAT THAT!!!!!" << endl;
    }
    return 42;
    }

    RH

    ps: I think I'll eat a kiwi. (The fruit, not the flightless bird.)




    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
    Robbie Hatley, May 4, 2004
    #4
  5. Robbie Hatley wrote:
    [snip]
    > Hints: "==" tests for equality, and you can use either "||" or "or" as
    > the "or" connective.


    Really??


    James
    James Lothian, May 4, 2004
    #5
  6. David

    Howard Guest

    "James Lothian" <> wrote in message
    news:...
    > Robbie Hatley wrote:
    > [snip]
    > > Hints: "==" tests for equality, and you can use either "||" or "or" as
    > > the "or" connective.

    >
    > Really??
    >
    >
    > James


    Sarcasm, or surprise? :)

    Personally, I'm surprised. Is it really part of the standard, or just a
    common implementation? I can't remember ever seeing a C++ program where
    "and" and "or" were used instead of && and ||. Why is that? It's not like
    it's so much harder to type. I would think that using "and" and "or" would
    be a lot easier to read, too. Maybe it's just a way of making C++ look more
    difficult, so we can justify our larger salaries? :) In any case...has
    anyone out there ever seen or used "and" and "or" instead of && and ||?
    Just curious...

    -Howard
    Howard, May 4, 2004
    #6
  7. Sharad Kala wrote:
    > "David" <> wrote in message news:uJFlc.362252$Ig.133228@pd7tw2no...
    >
    >>What can I do to accept either uppercase or lower case " y or n" in the
    >>program below.?

    [snip]

    >if (option[0] == 'n')
    >
    > Simple, check for both 'n' or 'N' i.e. if(option=='n' || option == 'N')
    >
    > -Sharad


    Better still, use only one check:
    if (std::toupper(option[0]) == 'N')

    or

    if (std::tolower(option[0]) == 'n')

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, May 4, 2004
    #7
  8. Robbie Hatley wrote:

    > "David" <> wrote:
    >
    >>What can I do to accept either uppercase or lower case " y or n" in the
    >>program below.?
    >>(snip)
    >>cout<< " MAHARAJA'S PIZZA" <<'\n\n';
    >>cout<< "Do you want a cheese 12 in. pizza? y/n: ";
    >>cin>> option;
    >>
    >>if (option[0] == 'n')
    >> p.setValue();
    >> p.displayValues();

    >
    >
    > Your code doesn't test for 'y' or 'Y' or 'N'; just for 'n'.
    >
    > So if you want to test for the others... why not just test for the others?
    > In the same way that you test for 'n'?
    >
    > Hints: "==" tests for equality, and you can use either "||" or "or" as
    > the "or" connective.

    [snip]

    If the "toupper" or "tolower" function is used, then only one
    comparison has to be made:

    if (std::toupper(option[0]) == 'N')

    /* or */

    if (std::tolower(option[0]) == 'n')


    For many options, one might want to check the incoming
    character against a string of valid options:
    const string valid_options("yYnN");
    //...
    if (valid_options.find(option[0]) != string::npos)

    Since a character is being input, one must validate that
    only the options desired have been input.

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
    Thomas Matthews, May 4, 2004
    #8
  9. "Howard" <> wrote in message
    news:c78epq$...
    >
    > "James Lothian" <> wrote in message
    > news:...
    > > Robbie Hatley wrote:
    > > [snip]
    > > > Hints: "==" tests for equality, and you can use either "||" or "or"

    as
    > > > the "or" connective.

    > >
    > > Really??
    > >
    > >
    > > James

    >
    > Sarcasm, or surprise? :)
    >
    > Personally, I'm surprised. Is it really part of the standard, or just a
    > common implementation? I can't remember ever seeing a C++ program where
    > "and" and "or" were used instead of && and ||. Why is that? It's not

    like
    > it's so much harder to type. I would think that using "and" and "or"

    would
    > be a lot easier to read, too. Maybe it's just a way of making C++ look

    more
    > difficult, so we can justify our larger salaries? :) In any case...has
    > anyone out there ever seen or used "and" and "or" instead of && and ||?
    > Just curious...
    >


    and, or, not, and several others are known as alternative tokens, see
    section 2.5 of the standard.

    I think the intent is for countries whose keyboards don't have obscure
    symbols like | and &.

    I've never seen them used in anger either, but I have seen several programs
    break when upgrading to a compiler that introduced these new tokens.

    john
    John Harrison, May 4, 2004
    #9
  10. David

    Jeff Schwab Guest

    Howard wrote:
    > "James Lothian" <> wrote:
    >
    >>Robbie Hatley wrote:
    >>
    >>>you can use either "||" or "or" as the "or" connective.

    >>
    >>Really??

    >
    > Sarcasm, or surprise? :)
    >
    > Personally, I'm surprised. Is it really part of the standard, or just a
    > common implementation? I can't remember ever seeing a C++ program where
    > "and" and "or" were used instead of && and ||. Why is that? It's not like
    > it's so much harder to type. I would think that using "and" and "or" would
    > be a lot easier to read, too. Maybe it's just a way of making C++ look more
    > difficult, so we can justify our larger salaries? :) In any case...has
    > anyone out there ever seen or used "and" and "or" instead of && and ||?
    > Just curious...


    Please see the thread

    Re: AND, NOT, OR keywords -- morality vs legality

    currently being waged in comp.lang.c++.moderated
    Jeff Schwab, May 4, 2004
    #10
  11. Howard wrote:
    >
    > "James Lothian" <> wrote in message
    > news:...
    > > Robbie Hatley wrote:
    > > [snip]
    > > > Hints: "==" tests for equality, and you can use either "||" or "or" as
    > > > the "or" connective.

    > >
    > > Really??
    > >
    > >
    > > James

    >
    > Sarcasm, or surprise? :)
    >

    Actually a bit of both. I'm amazed to discover that 'and' and 'or' are
    keywords in standard C++. What will they think of next?

    Off to bed to read the standard some more...

    James
    James Lothian, May 7, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    707
    J├╝rgen Exner
    Dec 7, 2004
  2. Janice

    lower case to upper case

    Janice, Dec 10, 2004, in forum: C Programming
    Replies:
    17
    Views:
    1,175
    Richard Bos
    Dec 14, 2004
  3. Pierre

    Manipulation of strings: upper/lower case

    Pierre, Jan 15, 2005, in forum: C Programming
    Replies:
    32
    Views:
    911
    Lawrence Kirby
    Jan 20, 2005
  4. penny
    Replies:
    28
    Views:
    2,403
    Charlton Wilbur
    Mar 10, 2008
  5. BlackHelicopter
    Replies:
    0
    Views:
    513
    BlackHelicopter
    Jan 31, 2013
Loading...

Share This Page