This while loop keeps repeating even when a correct character is entered....
cout<<endl<<"Wh at day would you like to schedule the appointment?"<< endl;
cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H'
for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
while (day != "M" || day != "m" || day != "T" || day != "t" ||
day != "W" || day != "w" || day != "H" || day != "h" ||
day != "F" || day != "f")
{
cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday,
'H' for Thursday \n"<<
"and 'F' for Friday: ";
cin>>day;
}
cout<<endl<<"Yo u entered "<<day<<end l;
No matter what character is input, it repeats. If I check for just "M" and
enter that it works. As soon as I add the first || it fails. What is wrong?
Thanks! 6 1492
SB wrote: This while loop keeps repeating even when a correct character is entered....
cout<<endl<<"Wh at day would you like to schedule the appointment?"<< endl; cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; while (day != "M" || day != "m" || day != "T" || day != "t" || day != "W" || day != "w" || day != "H" || day != "h" || day != "F" || day != "f") { cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; }
cout<<endl<<"Yo u entered "<<day<<end l;
No matter what character is input, it repeats. If I check for just "M" and enter that it works. As soon as I add the first || it fails. What is wrong?
You're using the wrong expression and I assume std::string day;
e.g. if day == "M" then it's obvious that day != "m" and hence the while
expression allways evaluates to true.
Try this.
while ( day != "M" && day != "m" ... etc
Now, you'll also have problems with different languages which use
different letter abbreviations.
SB wrote in news:hB0hc.7567 $2e6.1239@laker ead01 in comp.lang.c++: This while loop keeps repeating even when a correct character is entered....
cout<<endl<<"Wh at day would you like to schedule the appointment?"<< endl; cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; while (day != "M" || day != "m" || day != "T" || day != "t" || day != "W" || day != "w" || day != "H" || day != "h" || day != "F" || day != "f") { cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; }
cout<<endl<<"Yo u entered "<<day<<end l;
No matter what character is input, it repeats. If I check for just "M" and enter that it works. As soon as I add the first || it fails. What is wrong?
Change all the || to && (logical or to logical and)
HTH.
Rob.
-- http://www.victim-prime.dsl.pipex.com/
SB wrote: This while loop keeps repeating even when a correct character is entered....
cout<<endl<<"Wh at day would you like to schedule the appointment?"<< endl; cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; while (day != "M" || day != "m" || day != "T" || day != "t" || day != "W" || day != "w" || day != "H" || day != "h" || day != "F" || day != "f") { cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; }
cout<<endl<<"Yo u entered "<<day<<end l;
No matter what character is input, it repeats. If I check for just "M" and enter that it works. As soon as I add the first || it fails. What is wrong?
Thanks!
You could simplify this:
const std::string day_letters("MT WHFmtwhf");
char day;
cin >> day;
// check for any stream errors first!
while (day_letters.fi nd(day) == std::string::np os)
{
cout << '\'' << day << "\' is not a valid letter.\n";
cout << "Enter 'M' for Monday, 'T' for Tuesday,"
" 'W' for Wednesday, 'H' for Thursday \n"
"and 'F' for Friday: ";
cout.flush(); // make sure the text is displayed.
cin >> day;
}
Or one could use the std::toupper or std::tolower to
convert to one letter-case and reduce the comparison
string:
const std::string day_letters("mt whf");
char day;
cin >> day;
// check for any stream errors first!
day = std::tolower(da y);
//...
cin >> day;
day = std::tolower(da y);
--
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.l earn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html
Other sites: http://www.josuttis.com -- C++ STL Library book
"Thomas Matthews" <Th************ *************** *@sbcglobal.net > wrote... SB wrote:
This while loop keeps repeating even when a correct character is
entered.... cout<<endl<<"Wh at day would you like to schedule the
appointment?"<< endl; cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for Wednesday,
'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; while (day != "M" || day != "m" || day != "T" || day != "t" || day != "W" || day != "w" || day != "H" || day != "h" || day != "F" || day != "f")
I think SB should examine this condition. Imagine I have a number. Let's
name it 'N'. Then I have a condition (N != 1 || N != 2). What's its value?
If N is neither 2 nor 1, first we test it against 1. It does not equal 1,
so the first one is true. We stop checking and execute the controlled
statement. Now, if N is 1, we begin again. N != 1? False. Keep going:
N != 2. Sure. 1 != 2, true. Execute the controlled statement. Now, N is
2.
Is N not equal 1? Sure. 2 != 1, true, execute the controlled statement.
WTF? How come we always execute the controlled statement? Simple. How can
the logical expression be completely false? Only if BOTH parts of it are
false. When is the first one false? When N equals 1. When is the second
one false? When N equals 2. N cannot SIMULTANEOUSLY be 1 and 2 to make
both parts false. So, at least one of them is always true.
Can we do anything about it? Of course. When do you want to keep asking
the user for the correct input? If the day is neither of the accepted
values. How do you achieve the "neither" condition? You use 'AND', not
'OR'.
if (day != "M" && day != "m" ...
^^^^
Now, another problem is how you declared 'day'. If it's the same as Thomas
suggested:
char day;
then you will NEVER get the right answer if you try comparing it with a
string
literal (something in double quotes). Although in that case the compiler
will
complain about comparing a char to a pointer. You probalby declared it
'string',
which is OK, as long as the user always enters ONE character. If they enter
MO
or monday, the comparison will again fail. You should think of comparing
only
the first character of 'day'.
Anyway, enough for now?
{ cout<<endl<<"En ter 'M' for Monday, 'T' for Tuesday, 'W' for
Wednesday, 'H' for Thursday \n"<< "and 'F' for Friday: "; cin>>day; }
cout<<endl<<"Yo u entered "<<day<<end l;
No matter what character is input, it repeats. If I check for just "M"
and enter that it works. As soon as I add the first || it fails. What is
wrong? Thanks!
You could simplify this: const std::string day_letters("MT WHFmtwhf"); char day;
cin >> day; // check for any stream errors first! while (day_letters.fi nd(day) == std::string::np os) { cout << '\'' << day << "\' is not a valid letter.\n"; cout << "Enter 'M' for Monday, 'T' for Tuesday," " 'W' for Wednesday, 'H' for Thursday \n" "and 'F' for Friday: "; cout.flush(); // make sure the text is displayed. cin >> day; }
Or one could use the std::toupper or std::tolower to convert to one letter-case and reduce the comparison string: const std::string day_letters("mt whf"); char day;
cin >> day; // check for any stream errors first! day = std::tolower(da y); //... cin >> day; day = std::tolower(da y);
-- 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.l earn.c-c++ faq: http://www.raos.demon.uk/acllc-c++/faq.html Other sites: http://www.josuttis.com -- C++ STL Library book
Like the other folks said, your "OR"s should be "ANDS"
But on another note, isn't this what "switch" expresions are for?:
std::tolower(da y);
switch (day){
case "m":{}
case "t":{}
case "w":{}
case "h":{}
case "f":
{
DoStuff();
break;
}
default:
{
TryAgain();
}
}
"Dan Moos" <da******@veriz on.net> wrote in message news:<xW******* ***********@nwr ddc03.gnilink.n et>... Like the other folks said, your "OR"s should be "ANDS"
But on another note, isn't this what "switch" expresions are for?:
std::tolower(da y);
Undefined behaviour - std::tolower takes an unsigned int.
Note that this does not alter the value of 'day' either.
switch (day){
case "m":{}
'day' is either a char or a std::string (the OP didn't say), but
in neither case is it going to match the string literal "m" case "t":{}
case "w":{}
case "h":{}
case "f": { DoStuff(); break; } default: { TryAgain(); }
A lot of superfluous braces here
} This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: 127.0.0.1 |
last post by:
With all the problems with having register_globals = on, I propose the
following idea:
We define register_globals_manual = on as a new configuration default.
What this does is enable 3 new explicit variable declaration mechanisms
with the same syntax as the existing static and global mechanisms.
They would be httpget, httppost and session, so for example:
|
by: Jim Hubbard |
last post by:
Yet another hotfix alert (http://www.kbalertz.com/Feedback_823535.aspx) that
states "To resolve this problem immediately, contact Microsoft Product
Support Services to obtain the hotfix."
Whatever happened to automatic updates? Doesn't Microsoft know that
developers are busy people.....too busy to be stopped by this type of crap
every time Microsoft is alerted to something they missed in the software by
another developer (note:...
|
by: seberino |
last post by:
I've heard 2 people complain that word 'global' is confusing.
Perhaps 'modulescope' or 'module' would be better?
Am I the first peope to have thought of this and suggested it?
Is this a candidate for Python 3000 yet?
Chris
|
by: Raposa Velha |
last post by:
Hello to all!
Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.
Cheers,
RV
jmclopesAThotmail.com replace the AT with the thing you know ;-)
After discovering that access 2000 support form properties (I'm a
|
by: Daniel Billingsley |
last post by:
I think it's really nice to be able to do code like
someVariable = someMethod(new SomeClass("some text"));
However, as method signatures are number and types of parameters, you're
limited to having one constructor that accepts a single string, for example.
Call me crazy, but wouldn't it be useful to be able to have a syntax where
you could set properties in a statement like the one above, without being
limited to just what you could...
| |
by: Josh Ferguson |
last post by:
I don't believe a syntax driven equivalent exists for this, but I just
thought it would be neat if you could use foreach to do something like this:
foreach (Object x in collection1, collection2)
and have it sequentially enumerate through both of them.
|
by: wxs |
last post by:
Many times we have a bunch of enums we have from either different enums or
the same enum that will have various numeric values assigned. Rarely will
there be collisions in numbering between the enums. These enums you might
imagine would be like OrderPrice=27, OrderQuantity=50, OrderSide=62. There
may be a lot of these. So normally what we would have to do is store these
in hash table so hashtable=value could be set with the value....
|
by: Nurit N |
last post by:
This is the third newsgroup that I'm posting my problem.
I'm sorry for the multiple posts but the matter becoming urgent.
I hope this is the right place for it...
I have created a very simple batch file (echo hello world) and was trying to
retrieve the standard output but every time I run the code it returns
ExitCode as 1.
|
by: Michael7 |
last post by:
Hi everyone,
I'm new to CSS of course, and have been trying to learn it. However, when I try to pull off something as simple as positioning of text . . . nothing works in my index page. So in frustration I made a test page. Just a but of words to test only the code I'm looking for.
Still nothing!
I apologize if this question is a nuisance, but could anyone perhaps at least let me know what I'm doing wrong with this very simple thing?...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |