Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > accept upper or lower case Y/n

Reply
Thread Tools

accept upper or lower case Y/n

 
 
David
Guest
Posts: n/a
 
      05-04-2004
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:izza (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();





}



 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      05-04-2004

"David" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-04-2004

"David" <(E-Mail Removed)> 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


 
Reply With Quote
 
Robbie Hatley
Guest
Posts: n/a
 
      05-04-2004
"David" <(E-Mail Removed)> 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 =---
 
Reply With Quote
 
James Lothian
Guest
Posts: n/a
 
      05-04-2004
Robbie Hatley wrote:
[snip]
> Hints: "==" tests for equality, and you can use either "||" or "or" as
> the "or" connective.


Really??


James
 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      05-04-2004

"James Lothian" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) k...
> 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


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      05-04-2004
Sharad Kala wrote:
> "David" <(E-Mail Removed)> 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

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      05-04-2004
Robbie Hatley wrote:

> "David" <(E-Mail Removed)> 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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-04-2004

"Howard" <(E-Mail Removed)> wrote in message
news:c78epq$(E-Mail Removed)...
>
> "James Lothian" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) k...
> > 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


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      05-04-2004
Howard wrote:
> "James Lothian" <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Emacs, VHDL Mode - Upper Case Enum Values & Upper Case Constants (not working)? BlackHelicopter VHDL 0 01-31-2013 04:12 AM
Regular Expression to Replace UPPER Case Text with lower case text penny Perl Misc 28 03-10-2008 01:14 AM
lower case to upper case Janice C Programming 17 12-14-2004 02:35 PM
Regular expression weirdness with upper and lower case semovrs@concord.edu Perl 4 12-07-2004 01:35 PM
Upper and Lower case letters Ch. Rajinder Nijjhar Jatt Computer Support 5 11-02-2003 05:16 PM



Advertisments