Re-call Variable from "If-Else" Statement

So, I'm trying to make this menu application w/o using objects for practice, but I have an issue but I don't know how to fix it. I want to change a variable (the bready and meaty variables) based on what the customer picks and that combine the two at the very end to give a final price. Here's the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
  #include <iostream>
#include <iomanip> //need this for precision

using namespace std;

int main()
{
    double bready = 1;
    double meaty = 1;
    string bread;
    string meat;
    string topping;

    cout << "This is Subway V1!\nWhat kind of bread would you like?\n\n";
    cout << "Italian\nWheat\nFlatbread\n\n";

    cin >> bread;

    if (bread == "italian", "Italian")
    {
        bready + .5;
    }
    else if (bread == "wheat", "Wheat")
    {
        bready + .75;
    }
    else if (bread == "flatbread", "Flatbread")
    {
        bready + 1;
    }
    else
    {
        cout << "Please type in a bread that is listed.";
    }

    cout << bready << endl;

    cout << "Thank you, now choose a type of meat please\n\n";
    cout << "Ham\nTurkey\nSalami\n\n";

    cin >> meat;

    if (meat == "ham", "Ham")
    {
        meaty + .5;
    }
    else if (meat == "turkey", "Turkey")
    {
        meaty + 1;
    }
    else if (meat == "salami", "Salami")
    {
        meaty + 1.25;
    }
    else
    {
        cout << "Please type in a meat that is listed.";
    }

cout << meaty + bready << endl;

return 0;
}
Last edited on
Tweak the if condition to use the logical or operator instead of a comma. And use an assignment statement so you're actually updating the bready or meaty variable. What you have now returns a value, but it doesn't save anywhere.

For example,

1
2
3
4
    if (bread == "italian" || bread == "Italian")
    {
        bready += .5;
    }


You'd need some sort of while loop to let them re-enter their choice if they don't type it correctly the first time. Right now you move ahead to meaty even if they enter the wrong bread.
Thanks a ton wildblue!! Why should I use an operator instead of a comma? And so does this mean that using the "+=" always changes the value of the variable?

Oh, and yeah I need to work on that. I'm self teaching myself right now so I'm going to work some bugs out. I know I can loop the choice if I create an object, but if I wanted to do it w/o using an object and class, how would I go about doing that?

Sorry for all the questions. Thanks again
You want to execute the code in the if statement if the bread is equal to one or the other exact option. The || operator does that. The comma operator is different.

And yes, += updates the variable on the left side of the equation. a+=b is the same as a = a+b.

I pulled these out of the operators page from this forum.
http://www.cplusplus.com/doc/tutorial/operators/
The comma operator (,) is used to separate two or more expressions that are included where only one expression is expected. When the set of expressions has to be evaluated for a value, only the right-most expression is considered.
The operator || corresponds to the Boolean logical operation OR, which yields true if either of its operands is true, thus being false only when both operands are false.


You can create a loop even without objects. Something like...

while (valid bread type has not been entered)
--prompt for bread type again
Thanks for the updates and info, I understand it now. This is what I have so far for the while loop.

1
2
3
4
5
while (bread != "italian" || "Italian" || "wheat" || "Wheat" || "flatbread" || "Flatbread")
    {
        cout << "Please type in a bread that is listed.";
        cin >> bread;
    }


It doesn't return to the original bread at the beginning though.
closed account (j3Rz8vqX)
1
2
3
4
5
    while (bread != "italian" && bread != "Italian" && bread != "wheat" && bread != "Wheat" && bread != "flatbread" && bread != "Flatbread")
    {
        cout << "Please type in a bread that is listed.";
        cin >> bread;
    }

You may want to consider tolower'ing the string; will reduce the number of comparisons - not necessary, just an opinion.
Another suggestion might be to add in a menu where you equate a number with a bread type and only make the user type in the number instead of trying to type in a full bread name.

BREAD MENU
1. Italian
2. Wheat
3. Flatbread
Enter your choice (1-3):
@Dput I'll do that just so I can create a better habit if it helps with my coding in the long run.

@WildBlue I was planning on doing that, but I was practicing my strings which is why I chose to do it this way, but in V2 I will use numbers. Just less of a hassle

My while loops still won't restart the function. Any help with that? Thanks
Last edited on
Can you post what you've changed your code to? Did you change it to the example Dput gave?
Just did now, and it worked. Im stumped as too why the number of comparisons and using that certain operator allowed for a restart...
closed account (j3Rz8vqX)
Is there a function? (I did not see any posted above)
Also, be careful with cin and string data types.

Example of loop/validation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;
bool validate(string bread)
{
    const string data[] = {"italian","wheat","flatbread"};
    for(int i=0;i<bread.size();++i)
        bread[i]=tolower(bread[i]);
    for(int i=0;i<3;++i)
        if(bread==data[i])
            return false;
    return true;
}
int main()
{
    string bread;
    while (cout<<"Italian, Wheat, or Flatbread.\nChoice: "&&getline(cin,bread)&&validate(bread))
        cout << "Please type in a bread that is listed.\n\n";
    return 0;
}
Italian, Wheat, or Flatbread.
Choice: Bob
Please type in a bread that is listed.

Italian, Wheat, or Flatbread.
Choice: WheAT

Process returned 0 (0x0)   execution time : 20.139 s
Press any key to continue.

The above would repeat until a valid string was entered.

If you're using cin, you may want to consider using:
1
2
3
4
cin >> bread;
cin.clear();//Clears error flags in cin; happens if you put invalid data in cin - strings won't trigger errors
cin.ignore(1000,'\n');//Clears buffer; cin will stop on newlines/whitespaces, avoid garbage data by ignoring remaining data 
                      //unless you are exploiting the buffer with arrays using loops. 

---
Edit: Seems like it would work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
int main()
{
    string bread;
    cout<<"italian, wheat, or flatbread: ";
    cin>>bread;
    cin.clear();
    cin.ignore(1000,'\n');
    while (bread != "italian" && bread != "Italian" && bread != "wheat" && bread != "Wheat" && bread != "flatbread" && bread != "Flatbread")
    {
        cout << "Please type in a bread that is listed.\n";
        cin >> bread;
        cin.clear();
        cin.ignore(1000,'\n');
    }
    return 0;
}
italian, wheat, or flatbread: Bob
Please type in a bread that is listed.
Italian

Process returned 0 (0x0)   execution time : 8.366 s
Press any key to continue.
Last edited on
closed account (j3Rz8vqX)
Im stumped as too why the number of comparisons and using that certain operator allowed for a restart...


While loops will repeat the loop while true.
I'm not good at explaining the usage of && and ||.

The below may help:
http://www.cplusplus.com/doc/tutorial/operators/
in the section: Logical operators ( !, &&, || )
Sorry for the somewhat late reply, and thanks Dput. All of your code worked although I don't understand all of it. I'm a bit more comfortable with using cin because I have more experience with it for now.

1
2
3
cin.clear();
cin.ignore(1000, 'n')


Also, I will read up on operators a bit more. Thanks again guys, marked as solved.

Topic archived. No new replies allowed.