accept upper or lower case Y/n

D

David

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();





}
 
S

Sharad Kala

David said:
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
 
J

John Harrison

David said:
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
 
R

Robbie Hatley

David said:
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.)
 
H

Howard

James Lothian said:
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
 
T

Thomas Matthews

Sharad said:
David said:
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
 
T

Thomas Matthews

Robbie said:
David said:
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
 
J

John Harrison

Howard said:
James Lothian said:
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
 
J

Jeff Schwab

Howard said:
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
 
J

James Lothian

Howard said:
James Lothian said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top