Vending Machine

I'm trying to create a vending machine for a text based RPG and I'm not sure how to go about this. I want someone to be able to enter "A9" to pick an item. I'm using 3-dementional arrays to keep up with the row, column, and quantity of the items. I currently have a switch statement for the selection and from what I understand I need to use an enum since I'm trying to do a comparison of both int & char:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum vfood;


switch(vmachine){
		case 1:
			cout <<"Vending Machine: Omnomnom 0001" <<endl;
			while(leave == 1){
				cout <<"Enter a letter followed by a number (A0) to purchase the item" <<endl;
				cout <<"A0" << setw (10) << "A1" << setw (10) << "A2" << setw (10) << "A3" << setw (10) << "A4" <<endl;
				cout <<"B0" << setw (10) << "B1" << setw (10) << "B2" << setw (10) << "B3" << setw (10) << "B4" <<endl;
				cout <<"C0" << setw (10) << "C1" << setw (10) << "C2" << setw (10) << "C3" << setw (10) << "C4" <<endl;
				cout <<"D0" << setw (10) << "D1" << setw (10) << "D2" << setw (10) << "D3" << setw (10) << "D4" <<endl;
				cout <<"" <<endl;
				cin >>vfood;
					switch(vfood){
						case 'A0':
						case 'B0':
						default:
					}
			break;
			}


Errors:
cin >>vfood;
type name is not allowed
expected a declarator in condition declaration

Any help or suggestions much appreciated. ;D
make a header with the enum full def. I don't think you can do enum declarations... or you can be ddo you have that enum defined somewhere... obviously you don't in the code your showing

Also I never tried giving an enum a value through input... I'm not sure if you can cin >> enumType... You might be able to do numbers....

As in [code] enum enumType { ball = 1, football = 2) and you assign it numbers... I'm not sure though.

I think switch cases can only be numbers as well.

I have never even used a switch in a program before lol so I don't know. Look at the tut from this site.
Doing cin>>vfood; as you have there is basically like writing cin>>int; You need to actually create an instance of the enum vfood before you can use it. And you'll need to actually define that enum if you haven't done that.
you need to define your enumeration, then you can use the enum values as cases in the switch statement. Also, you should ask for user input before the while loop begins. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
enum vfood{A0,A1,A2,...};

cout <<"Vending Machine: Omnomnom 0001" <<endl;
cout <<"Enter a letter followed by a number (A0) to purchase the item" <<endl;
cout <<"A0" << setw (10) << "A1" << setw (10) << "A2" << setw (10) << "A3" << setw (10) << "A4" <<endl;
cout <<"B0" << setw (10) << "B1" << setw (10) << "B2" << setw (10) << "B3" << setw (10) << "B4" <<endl;
cout <<"C0" << setw (10) << "C1" << setw (10) << "C2" << setw (10) << "C3" << setw (10) << "C4" <<endl;
cout <<"D0" << setw (10) << "D1" << setw (10) << "D2" << setw (10) << "D3" << setw (10) << "D4" <<endl;
cout <<"" <<endl;
cout <<"Enter your selection now: "<< endl;
cin >> choice;
 while (choice >= A0 && choice <= D9)
{
        switch (choice)
        {
                case A0 : whatever statement you want to execute;
                case A1 : ""                                  "";
                ....
        }
        cout <<"Enter your choice: ";
        cin << choice;
}
Can I cin >> to an enum? Again I'm wanting the person to be able to enter "B6" in one line and have it checked with a switch statement to determine the items information. I currently have:

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
void main()
{
//Vending Switch Food
enum vfood{A0, A1, A2, A3, A4, B0, B1, B2, B3, B4, C0, C1, C2, C3, C4, D0, D1, D2, D3, D4};

switch(vmachine)
	{
		case 1:
			switch (vfood)
			{
				cout <<"Vending Machine: Omnomnom 0001" <<endl;
				cout <<"Enter a letter followed by a number (A0) to purchase the item" <<endl;
				cout <<"A0" << setw (10) << "A1" << setw (10) << "A2" << setw (10) << "A3" << setw (10) << "A4" <<endl;
				cout <<"B0" << setw (10) << "B1" << setw (10) << "B2" << setw (10) << "B3" << setw (10) << "B4" <<endl;
				cout <<"C0" << setw (10) << "C1" << setw (10) << "C2" << setw (10) << "C3" << setw (10) << "C4" <<endl;
				cout <<"D0" << setw (10) << "D1" << setw (10) << "D2" << setw (10) << "D3" << setw (10) << "D4" <<endl;
				cout <<"" <<endl;
				cin >>vfood;
					case 'A0':
						cout <<"You have selected: AO" <<endl;
					case 'B0':
						cout <<"You have selected: B0" <<endl;
				}
			break;
		case 2:
			cout <<"Vending Machine: Pew Pew 0001" <<endl;
			break;
		case 3:
			cout <<"Vending Machine: /Flex 0001" <<endl;
			break;
		case 4:
			cout <<"Vending Machine: Pot-ables 0001" <<endl;
			break;
		case 5:
			cout <<"Vending Machine: Fancy 0001" <<endl;
			break;
		default:
			cout <<"You can't count...." <<endl;
	}
	system("PAUSE"); //Testing purposes
}


However I am getting syntax errors for when I use vfood in my switch statement and cin statement. I can easily use enums to do # to char or vice versa, but I'm wanting to do BOTH, which has complicated the process:

Error 1 error C2059: syntax error : ')' c:\documents and settings\owner\my documents\visual studio 2010\projects\vending\vending\main.cpp 199

Error 2 error C2143: syntax error : missing ';' before '{' c:\documents and settings\owner\my documents\visual studio 2010\projects\vending\vending\main.cpp 200

Error 3 error C2275: 'main::vfood' : illegal use of this type as an expression c:\documents and settings\owner\my documents\visual studio 2010\projects\vending\vending\main.cpp 208

4 IntelliSense: expected a declarator in condition declaration c:\documents and settings\owner\my documents\visual studio 2010\projects\vending\vending\main.cpp 199

5 IntelliSense: type name is not allowed c:\documents and settings\owner\my documents\visual studio 2010\projects\vending\vending\main.cpp 208
if you're getting errors with the enum you might want to define it differently. I don't think the enums are set(alright i'm about to look for you)... hmm they start at 1 unless defined other wise.. meaning first one gets the value one increments as value changes etc
you have to manually define enum g{b = 0, c}; c now equals 1

you should comment in this code which error goes with which loc for future postings

i don't see any syntax errors in your code... I don't think those errors have anything to do with code you're showing it... Or my eye isn't trained enough to catch 'em
closed account (DSLq5Di1)
@brokenbot
Enumerations are zero based.

@dfurball
You cannot read directly into an enum, but you can overload the stream operator and translate your input to an index of vfood.

1
2
#include <iostream>
#include <cctype> 

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
std::istream& operator>>(std::istream& is, vfood& e)
{
    for (; is.clear(), is.sync() == 0;)
    {
        char letter;
        int number;

        if (is >> letter >> number) // validate input
        {
            if (std::isalpha(letter) && number >= 0 && number <= 4)
            {
                letter = static_cast<char>( std::toupper(letter) );
                if (letter >= 'A' && letter <= 'D')
                {
                    int index = 5 * (letter - 'A') + number;
                    e = static_cast<vfood>(index);

                    break;
                }
            }
        }
        std::cerr << "Invalid input\n";
    }
    return is;
}

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
...
		case 1:
			switch (vfood)
			{
				cout <<"Vending Machine: Omnomnom 0001" <<endl;
				cout <<"Enter a letter followed by a number (A0) to purchase the item" <<endl;
				cout <<"A0" << setw (10) << "A1" << setw (10) << "A2" << setw (10) << "A3" << setw (10) << "A4" <<endl;
				cout <<"B0" << setw (10) << "B1" << setw (10) << "B2" << setw (10) << "B3" << setw (10) << "B4" <<endl;
				cout <<"C0" << setw (10) << "C1" << setw (10) << "C2" << setw (10) << "C3" << setw (10) << "C4" <<endl;
				cout <<"D0" << setw (10) << "D1" << setw (10) << "D2" << setw (10) << "D3" << setw (10) << "D4" <<endl;
				cout <<"" <<endl;

                                vfood choice;
				cin >> choice;

                                switch (choice)
                                {
				case A0:
					cout <<"You have selected: AO" <<endl;
                                        break;
				case B0:
					cout <<"You have selected: B0" <<endl;
                                        break;
				}
			}
Thank you sloppy9, that is exactly what I needed to know. Work's perfect! :)

I've never used cctype before, but the checks will defiantly come in handy in the future.

Reference for others:
http://www.cplusplus.com/reference/clibrary/cctype/
Topic archived. No new replies allowed.