Beverage machine (noob)

Here is a example of simple use of switch statements.



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
#include <iostream>
using namespace std;

int main()
{
   int choice;
   char y='y';
   char again;
   do
   {
   cout<<"Chose a bevrage: \n";
   cout<<"1. Cola\n";
   cout<<"2. Sprite\n";
   cout<<"3. Water\n";
   cout<<"4. Fanta\n";
   cout<<"5. Beer\n";
   cin>>choice;
   switch (choice)
   {
       case 1:
       cout<<"You bought Cola\n";
       break;
       case 2:
       cout<<"You bought Sprite\n";
       break;
       case 3:
       cout<<"You bought Water\n";
       break;
       case 4:
       cout<<"You bought Fanta\n";
       break;
       case 5:
       cout<<"You bought Beer\n";
       break;
       default:
       cout<<"Wrong input!\n";
   }
       cout<<"Input again y/n :";
       cin>>again;
   }
    while (again==y);
    return 0;
}
[/output]
[/code]
And?!

By the way change

while (again==y);

to

while (again=='y');

EDIT; Oh, I am sorry. You are comparing two variables again and y.:)
In this case it would be better to define y as const char. For example

const char y = 'y';

But I have not understood what is the question?
Last edited on
What's the point of line 7? You don't use the variable anywhere.
Topic archived. No new replies allowed.