C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 8th, 2002, 10:45 PM
beefstu01 beefstu01 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 3 beefstu01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Problem using "or" in a while loop

Hello!

I'm a student taking C++, fun stuff. I hope to one day get good at this stuff, I sorta am already, but good at the elementary levels

Here's my problem; I'm writing a program to flip a coin x amount of times and print the results. Of course I want to go one step higher and ask the user if he would like to redo the "experiment," so there's the simpe "Do you want to to it again? (y/n)" prompt. My problem is this- I want to use both cases (n and N) to say "no," and the or thingymabobber (||) isn't working for me. Here's the chunk of code that isn't being nice to me (There's more to it, but its irrelevant because it works, well most of this code is irrelevant, but I just copied and pasted)->

Quote:
int holder, heads, tails;
char cont;
while (cont != 'n') || (cont != 'N') //<-- Problem THERE
{
holder = 0; //Holds the temporary value in the for loop
heads = 0; //Number of heads (zeros) returned
tails = 0; //Number of tails (ones) returned
for (int count = getcount(); count >= 0; count--)
{
holder = flipcoin();
if (holder == 0)
heads = heads + 1;
else
tails = tails + 1;
}
printresults (heads, tails);
cout << "Do you want to do it again? (y/n) ";
cin >> cont;


I've tried everything with that while (), but I bet it's a simple fix. So-- what did I screw up this time?

Thanks a lot.

EDIT-
Perhaps I should tell you what went wrong too. Silly me.

When I tried compiling it as is it gave me a parse error before the ||

When I had-

while (cont != 'n' || cont != 'N')

- The program compiled and ran, but did not recognize the check. Well, it asked for input, but if I entered 'n' or 'N', the program breezed right through it and repeated itself. Same thing with-

while (cont != 'n' || 'N')

Last edited by beefstu01 : November 8th, 2002 at 10:52 PM.

Reply With Quote
  #2  
Old November 9th, 2002, 02:35 AM
MJEggertson MJEggertson is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Seattle WA
Posts: 863 MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level)MJEggertson User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 22 sec
Reputation Power: 13
You almost got it. You just need to add nested parentheses, so that each condition is evaluated, then the OR of each evaluation is taken.
Code:
while ((cont != 'n') || (cont != 'N'))

Reply With Quote
  #3  
Old November 9th, 2002, 10:04 AM
beefstu01 beefstu01 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 3 beefstu01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hrm.. that's odd. When I do that, the program compiles but still doesn't recognize the 'n' or 'N'

Any idea what the problem could be, or is it just my compiler being dumb?

Reply With Quote
  #4  
Old November 9th, 2002, 12:01 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
I'm afraid your logic's at fault in this case.

Quote:
while ((cont != 'n') || (cont != 'N'))


As you know, a while-loop loops as long as the conditional expression is True. So when cont is either 'n' or 'N', you want the expression to evaluate to False, right?

Let's assume cont contains the value of 'n', and see what happens:

a) cont != 'n' evaluates to False, clearly enough.
b) cont != 'N' evaluates to True however, since 'n' != 'N'.
c) The logical OR of True and False evaluates to True, so the conditional expression holds. The while-loop continues looping.

That's applying the definitions, but if you just think about the whole expression for a bit (you know, let it sink in), you'll find that it indeed doesn't make sense.

What you -really- want is the expression
Code:
while ((cont != 'n') && (cont !='N'))


Of course you could also apply De Morgan's rule (at least, one of them), and come up with the equivalent
Code:
while (!(cont == 'n' || cont == 'N'))


Hopefully this will clear things up a little
__________________
"A poor programmer is he who blames his tools."
http://analyser.oli.tudelft.nl/

Reply With Quote
  #5  
Old November 9th, 2002, 12:05 PM
Analyser's Avatar
Analyser Analyser is offline
*bounce*
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2002
Location: Delft, The Netherlands
Posts: 513 Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level)Analyser User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Days 22 h 45 m 12 sec
Reputation Power: 41
Send a message via ICQ to Analyser
Oh, just for kicks, here's a web site with some info on boolean algebra. Enjoy

Reply With Quote
  #6  
Old November 9th, 2002, 12:52 PM
beefstu01 beefstu01 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Posts: 3 beefstu01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oh my God, how could I be so stupid!

Duh, thanks a whole bunch. Course II math and all that logic stuff flew out of my head. If you can imagine the sound of a head banging on a desk, well, thats the sound I'm making.

Thanks sooooo much. You rock.

Reply With Quote
  #7  
Old November 10th, 2002, 11:13 PM
TechNoFear TechNoFear is offline
Offensive Member
Dev Shed Novice (500 - 999 posts)
 
Join Date: Oct 2002
Location: in the perfect world
Posts: 618 TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level)TechNoFear User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 13 h 50 m
Reputation Power: 26
You are forgetting to initialise the while condition variable

char cont; //could start = 'n'

use

toupper(cont)

then just test for 'N'


can streamline somewhat
Code:
if(FlipCoin())
    heads++;
else
    tails++;
__________________
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

Frank Zappa

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Problem using "or" in a while loop

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap