Soda Machine

Aug 12, 2014 at 2:40am
I am making a very simple so to say "Soda Machine". I want it to ask the user if they would like a free soda, if they say yes it will keep going on with the program and if they say no it will just end the program or maybe say thank have a nice day. I have it written out to where it says welcome to the soda machine and then gives them the 5 choices. Then it asks which one they want and you are able to enter whatever you would like and then it simply prints out what you chose.

What I want it to do like I said was to ask if they want one or not. If they say yes keeps going on and if no stops.

I am a little stuck on how to make that happen. I would think I need to use an IF and ELSE statement but not really sure how to implement them into the program. Any help is great!

Thanks. :)

Oh and also if there is anything I should change please let me know!

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
#include <iostream>
#include <string>

using namespace std;

int main()

{
    char yes;
    char no;
    cout << "Do you want a free soda?\n";


    int x;
    string mystr;
    cout << "Welcome to the soda machine.\n";
    cout << "We have 5 different sodas to choose from. Sprite, Coke, Pepsi, \nMtn. Dew, Dr. Pepper\n";

    cout << "Which beverage would you like?";
    getline (cin,mystr);
    cout << "You picked:" << " " << mystr;



}

Last edited on Aug 12, 2014 at 2:42am
Aug 12, 2014 at 2:44am
And also if anyone knows how to make it so that they are only able to select one of the 5 ones that are listed and not just type anything random in and if they were to, it could maybe respond that is not one of the 5 beverages.
Aug 12, 2014 at 6:51am
you would only use one if statement just like in my code below and i use do while loop so they will only able to select one of the choices if they choose one that is not listed in choices they will be asked again to choose.




FIX 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
#include <iostream>
#include <string>

using namespace std;

int main()

{   string mystr= " " ;
    string yes_or_no=" ";
    cout << "Do you want a free soda?\n";
    cin>>yes_or_no;
    if(yes_or_no == "yes" )
    {
    do
    {cout << "Welcome to the soda machine.\n";
    cout << "We have 5 different sodas to choose from. Sprite, Coke, Pepsi, \nMtn. Dew, Dr. Pepper\n";
    cout << "Which beverage would you like?";
    cin>>mystr;}while( mystr != "sprite" &&mystr != "coke"&&mystr != "pepsi"&& mystr != "mountain dew"&&mystr !=  "dr.pepper");
    cout << "You picked:" << " " << mystr;
    }
else
    {
    return 0;
    }


}
Last edited on Aug 12, 2014 at 6:51am
Aug 12, 2014 at 3:16pm
Awesome! Thank you so much.
Aug 12, 2014 at 7:53pm
Better format 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
#include <iostream>
#include <string>

using namespace std;

int main(){
    string mystr= " " ;
    string yes_or_no=" ";
    cout << "Do you want a free soda?\n";
    cin>>yes_or_no;
    if(yes_or_no == "yes" )
    {
        do
        {
             cout << "Welcome to the soda machine.\n";
             cout << "We have 5 different sodas to choose from. Sprite, Coke, Pepsi, \nMtn. Dew, Dr. Pepper\n";
             cout << "Which beverage would you like?";
             cin>>mystr;
        }while( mystr != "sprite" &&mystr != "coke"&&mystr != "pepsi"&& mystr != "mountain dew"&&mystr !=  "dr.pepper");
             cout << "You picked: " << mystr;
    }
    else
    {
        return 0;
    }
}
Last edited on Aug 12, 2014 at 7:57pm
Aug 12, 2014 at 8:01pm
PS:
You don't need to do this:
cout<<"You picked:"<<" "<<mystr;
You can just do this:
cout<<"You picked: "<<mystr
And for a char, you don't use "", you use ''. You use "" for strings. Except the program will still work fine, it's just a good habit.
Aug 13, 2014 at 10:07am
You don't need to do this:


the OP wants to do in that way

And for a char, you don't use "", you use ''. You use "" for strings. Except the program will still work fine, it's just a good habit.


i often use string rather than char, i only use char for conversion or that the user dont need to enter an input
Last edited on Aug 13, 2014 at 10:08am
Aug 13, 2014 at 10:46am
mark this as solved now
Topic archived. No new replies allowed.