473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Do while question

Can you have one or the other type conditions for a do while loop?
See the non-working code below...
do
{
cout << "Are you a Member or Non-member? Enter 'M' or 'N':
";
cin >charMstat;
charMstat = toupper(charMstat);
} while(charMstat != 'M' || charMstat != 'N');
Thanks

Feb 3 '07 #1
16 1565
On Feb 2, 9:41 pm, "Cybex" <jsmel...@gmail.comwrote:
Can you have one or the other type conditions for a do while loop?
See the non-working code below...

do
{
cout << "Are you a Member or Non-member? Enter 'M' or 'N':
";
cin >charMstat;
charMstat = toupper(charMstat);
} while(charMstat != 'M' || charMstat != 'N');

Thanks
Yes.

Feb 3 '07 #2
Thank you for the quick answer.

What am I doing wrong with my code that it is not working?

Feb 3 '07 #3
I V
On Fri, 02 Feb 2007 21:41:32 -0800, Cybex wrote:
Can you have one or the other type conditions for a do while loop?
See the non-working code below...
[snip]
} while(charMstat != 'M' || charMstat != 'N');
This "while" condition will always be true. if charMstat is "M" and
the first part of the condition is false, then charMstat isn't "N", and so
the second part of the condition will be true, and vice versa. This
condition would be false only if charMstat was equal to "M" _and_ it was
equal to "N", which obviously can never happen.

You need to think through your logic again. Under what circumstances do
you want your loop to quit?

Feb 3 '07 #4
I need to be able to exit the loop if either condition is met.

Feb 3 '07 #5
On Feb 2, 10:09 pm, "Cybex" <jsmel...@gmail.comwrote:
Thank you for the quick answer.

What am I doing wrong with my code that it is not working?
You should be using '&&':

[code]
} while(charMstat != 'M' && charMstat != 'N');
[end of code]

Feb 3 '07 #6
Thank you. That was it...
I thought "||" was "or" and "&&" was "and"???

Feb 3 '07 #7
On Feb 2, 10:15 pm, "Cybex" <jsmel...@gmail.comwrote:
I need to be able to exit the loop if either condition is met.
That's not really true. One of the conditions will be met each time
(since they are != comparisions). It might be clearer to use a do-
until loop and equality comparisions:

[code]
} until(charMstat == 'M' || charMstat == 'N');
[end code]

Or you could use my earlier suggestion of just changing '||' to '&&'

The best thing for you to do when faced with these problems is to draw
out a truth table.

Feb 3 '07 #8
On Feb 2, 10:30 pm, "Cybex" <jsmel...@gmail.comwrote:
Thank you. That was it...
I thought "||" was "or" and "&&" was "and"???
It is! But you are using not-equal comparisons. Sit down and work
through the logic. You should be able to figure out the problem once
you work it out case by case.

Feb 3 '07 #9
Cybex wrote:
Thank you. That was it...
I thought "||" was "or" and "&&" was "and"???
That's correct. Those terms have specific meanings in Boolean algebra
that don't necessarily correspond to colloquial usage.

--
Mike Smith
Feb 3 '07 #10
I see what you mean now...

Thanks!

Feb 3 '07 #11
Z.*****@gmail.com wrote:
It might be clearer to use a do-
until loop and equality comparisions:

[code]
} until(charMstat == 'M' || charMstat == 'N');
[end code]

C++ doesn't have an "until" keyword.
Feb 3 '07 #12
On Feb 3, 1:09 am, Alan Johnson <a...@yahoo.comwrote:
Z.Me...@gmail.com wrote:
It might be clearer to use a do-
until loop and equality comparisions:
[code]
} until(charMstat == 'M' || charMstat == 'N');
[end code]

C++ doesn't have an "until" keyword.
<deeply embarrased>Well I'll be darned!!! I guess that's why it isn't
very prevalent in my and C++ code.</deeply embarrased>

Feb 3 '07 #13
Pan
Z.*****@gmail.com wrote:
<deeply embarrased>Well I'll be darned!!! I guess that's why it isn't
very prevalent in my and C++ code.</deeply embarrased>
<don't try it at home>

You can still

#define until(cond) while(!(cond))

</don't try it at home>

The bad point is that I actually read it on a student's exercise...

--
Marco
Feb 3 '07 #14
On Feb 3, 8:41 am, "Cybex" <jsmel...@gmail.comwrote:
Can you have one or the other type conditions for a do while loop?
See the non-working code below...

do
{
cout << "Are you a Member or Non-member? Enter 'M' or 'N':
";
cin >charMstat;
charMstat = toupper(charMstat);
} while(charMstat != 'M' || charMstat != 'N');

Thanks
Why is it not working?I do not see any problem with it.you had better
post the error message or the problem you have encountered .

Feb 3 '07 #15
On 3 Feb 2007 02:57:08 -0800, "terminator" <fa***********@gmail.comwrote:
do
{
cout << "Are you a Member or Non-member? Enter 'M' or 'N':
";
cin >charMstat;
charMstat = toupper(charMstat);
} while(charMstat != 'M' || charMstat != 'N');
^^ should be &&

-dr
Feb 3 '07 #16
Dave Rahardja:
On 3 Feb 2007 02:57:08 -0800, "terminator" <fa***********@gmail.comwrote:
> do
{
cout << "Are you a Member or Non-member? Enter 'M' or 'N':
";
cin >charMstat;
charMstat = toupper(charMstat);
} while(charMstat != 'M' || charMstat != 'N');
^^ should be &&

-dr

You can turn a && into a || (and vice versa) by doing the following

1) Invert all operands.
2) Invert the result.

For instance, a && b becomes !(!a || !b)

a || b || !c becomes !(!a && !b && c)
--
~/JB\~
Feb 14 '07 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mohammed Mazid | last post by:
Can anyone please help me on how to move to the next and previous question? Here is a snippet of my code: Private Sub cmdNext_Click() End Sub Private Sub cmdPrevious_Click() showrecord
3
by: Stevey | last post by:
I have the following XML file... <?xml version="1.0"?> <animals> <animal> <name>Tiger</name> <questions> <question index="0">true</question> <question index="1">true</question> </questions>
7
by: nospam | last post by:
Ok, 3rd or is it the 4th time I have asked this question on Partial Types, so, since it seems to me that Partial Types is still in the design or development stages at Microsoft, I am going to ask...
3
by: Ekqvist Marko | last post by:
Hi, I have one Access database table including questions and answers. Now I need to give answer id automatically to questionID column. But I don't know how it is best (fastest) to do? table...
10
by: glenn | last post by:
I am use to programming in php and the way session and post vars are past from fields on one page through to the post page automatically where I can get to their values easily to write to a...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
56
by: spibou | last post by:
In the statement "a *= expression" is expression assumed to be parenthesized ? For example if I write "a *= b+c" is this the same as "a = a * (b+c)" or "a = a * b+c" ?
2
by: Allan Ebdrup | last post by:
Hi, I'm trying to render a Matrix question in my ASP.Net 2.0 page, A matrix question is a question where you have several options that can all be rated according to several possible ratings (from...
3
by: Zhang Weiwu | last post by:
Hello! I wrote this: ..required-question p:after { content: "*"; } Corresponding HTML: <div class="required-question"><p>Question Text</p><input /></div> <div...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.