d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Hey Yall > Stuck On A Do-while Loop.
12Next
Add Reply New Topic New Poll
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Mar 9 2014 11:03pm
Here is my code:

Code
// NinjaSushi2
// 09-Mar-2014

// This program displays a hot beverage menu and prompts the user to
// make a selection. A switch statement determines which item the user
// has chosen. A do-while loop repeats until the user selects item E
// from the menu.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int number;
float cost;
char beverage;

bool validBeverage;
cout << fixed << showpoint << setprecision(2);

do {
cout << endl << endl;
cout << "Hot Beverage Menu" << endl << endl;
cout << "A: Coffee $1.00" << endl;
cout << "B: Tea $ 0.75" << endl;
cout << "C: Hot Chocolate $1.25" << endl;
cout << "D: Cappuccino $2.50" << endl << endl << endl;
cout << "Enter the beverage A,B,C, or D you desire" << endl;
cout << "Enter E to exit the program" << endl << endl;
cin >> beverage;

switch(beverage)
{
case 'a':
case 'A':
case 'b':
case 'B':
case 'c':
case 'C':
case 'd':
case 'D':

validBeverage = true;
break;
default: validBeverage = false;
}

if (validBeverage == true)
{
cout << "How many cups would you like?" << endl;
cin >> number;
}

switch(beverage)
{
case 'a':
case 'A':
cost = number * 1.0;
cout << "The total cost is $ " << cost << endl;
break;
case 'b':
case 'B':
cost = number * 0.75;
cout << "The total cost is $ " << cost << endl;
break;
case 'c':
case 'C':
cost = number * 1.25;
cout << "The total cost is $ " << cost << endl;
break;
case 'd':
case 'D':
cost = number * 2.50;
cout << "The total cost is $ " << cost << endl;
break;
case 'e':
case 'E': cout << "\nPlease come again" << endl;
break;
default:cout << "\nNot a valid selection" << endl;
cout << "Try again please" << endl;
}

} while (beverage != 'e' || 'E');

//else

// Fill in the code to finish the do-while statement with the
// condition that beverage does not equal E or e.
// Fill in the appropriate return statement

return 0;
}


I am guessing I am doing the while part of the loop incorrectly. How can I write the loop to test for the char E so that when E is used, the loop when the end?

Edit: I am trying to add in a sentinel value for my loop.
Edit 2: So how do I get it to test for both e and E? OR I am guessing is not the correct syntax to be using?

This post was edited by NinjaSushi2 on Mar 9 2014 11:10pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 9 2014 11:05pm
} while (beverage != 'e' || 'E');

didnt read most of it, but that looks wrong.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 9 2014 11:07pm
while loops use conditional statements aka what if statements are.

Code
beverage != 'e' || 'E'


In no way shape or form is this a valid condition statement for pretty much any language.

Code
beverage != 'e' || beverage != 'E'


This would be a proper conditional statement.


This post was edited by AbDuCt on Mar 9 2014 11:12pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Mar 9 2014 11:11pm
Quote (AbDuCt @ 10 Mar 2014 00:07)
while loops use conditional statements aka what if statements are.

Code
beverage != 'e' || 'E'


In no way shape or form is this a valid condition statement for pretty much any language.

Code
beverage != 'e' || beverage != 'E'


This would be a proper conditional statement.


Yeah I just figured that as I was playing with the code. I wasn't thinking to repeat the variable beverage.

Thanks yall! :D :wub:
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 9 2014 11:12pm
Quote (NinjaSushi2 @ Mar 10 2014 01:11am)
Yeah I just figured that as I was playing with the code. I  wasn't thinking to repeat the variable beverage.

Thanks yall! :D  :wub:


This is the worse coding I've seen her since I've came back.

Use a tolower() method to convert all your input to lowercase and then simple to a ranged conditional to set your flag to true.

Code
if(beverage > 'a' && beverage < 'd') { w/e = true } else { w/e = false }


Who ever taught you to do an select statement like that to set a variable to true or false is an idiot.
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Mar 9 2014 11:14pm
I am only on chapter 5 of any C++ I've learned haha. I haven't learned tolower() methods. Though the while statement doesn't end the loop. If I just do:

Code
} while (beverage != 'e');

or
Code
} while (beverage != 'E');
It will not execute correctly. The OR condition isn't registering.

This post was edited by NinjaSushi2 on Mar 9 2014 11:19pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 9 2014 11:19pm
Quote (NinjaSushi2 @ Mar 10 2014 12:14am)
I am only on chapter 5 of any C++ I've learned haha. I haven't learned tolower() methods. Though the while statement doesn't end the loop. If I just do:

Code
} while (beverage != 'e');

or
Code
} while (beverage != 'E');
It will not execute. The OR doesn't work though for some reason.


&& not ||

think about what || is doing.

This post was edited by carteblanche on Mar 9 2014 11:20pm
Member
Posts: 35,456
Joined: Jan 25 2009
Gold: 1,173.00
Mar 9 2014 11:22pm
Oh god derp. I was thinking if I should use AND. I forgot that != reverses the truths, correct?

So the && boolean works but I am confused on why. The statement is saying

Code
while (beverage is NOT equal to e && NOT equal E);
return 0;


I would think it would require the use of both e and E but it compiles correctly and executes correctly. Can someone explain why?

This post was edited by NinjaSushi2 on Mar 9 2014 11:30pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Mar 9 2014 11:37pm
Quote (NinjaSushi2 @ Mar 10 2014 12:22am)
Oh god derp. I was thinking if I should use AND. I forgot that != reverses the truths, correct?

So the && boolean works but I am confused on why. The statement is saying

Code
while (beverage is NOT equal to e && NOT equal E);
return 0;


I would think it would require the use of both e and E but it compiles correctly and executes correctly. Can someone explain why?


or => if either condition is true, result is true
and => if both conditions are true, result is true.

think about #8 very carefully. why is "yes" a valid answer? given her question, when will hubby ever answer 'no'? how would it change if wifey asked "and" instead of "or"?


/edit: then after you answer that, look at your problem again. when you use OR, under what conditions is your statement true? how about false? repeat with AND.

This post was edited by carteblanche on Mar 9 2014 11:48pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 9 2014 11:47pm
Quote (NinjaSushi2 @ Mar 10 2014 01:22am)
Oh god derp. I was thinking if I should use AND. I forgot that != reverses the truths, correct?

So the && boolean works but I am confused on why. The statement is saying

Code
while (beverage is NOT equal to e && NOT equal E) return 0;


I would think it would require the use of both e and E but it compiles correctly and executes correctly. Can someone explain why?




Create a truth table out of it.

Let beverage equal A and let 'e' equal B and let 'E' equal C

Your variables would be like so

Code
A B C
0 0 0
0 0 1
0 1 0
1 0 0
0 1 1
1 0 1
1 1 1


Now solve the sub equations (NOT EQUALS)



Code
A B C A!=B A!=C
0 0 0 0 0
0 0 1 0 1
0 1 0 1 0
1 0 0 1 1
0 1 1 1 1
1 0 1 1 0
1 1 1 0 0


Now simply do your != && != table

Code
A B C A!=B A!=C (A!=B)&&(A!=C)
0 0 0 0 0 0
0 0 1 0 1 0
0 1 0 1 0 0
1 0 0 1 1 1
0 1 1 1 1 1
1 0 1 1 0 0
1 1 1 0 0 0


Now thinking back on how a while loop works (it loops as long as it is true) so as long as the states of A B C relates to 0 1 1 or 1 0 0 it will not break from the loop.

*Hopefully I got that table right, it's getting late for me*

This post was edited by AbDuCt on Mar 9 2014 11:53pm
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll