Getting errors when using enum

Im trying to setup enum for the name of the coffee in my struct but I keep getting an invalid use code. What am I doing wrong? Thank you.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <fstream>
using namespace std;


struct Coffee
{
    enum name {Columbian = 1, French = 2, Italian = 3, House = 4};
    int sugar;
    int strength;
    int size;
    bool cream = false;
};
struct Customer
{
    char fname[20];
    int orderNum[100];
    Coffee pdata;
};

int main()
{

Customer order1;
int x;
int answer;
double names[4]={0, 0.25, 0.25, 0};
double array [5]={1.50, 2.10, 2.50, 3.00, 4.25};
string array1[5] = {"Small (1.50)", "Regular (2.10)", "Medium (2.50)", "Large 3.00", "Extra Large (4.25)"};
double total=0;

cout << "***********************************************\n";
cout << "THIS PROGRAM IS GOING TO TAKE YOUR COFFEE ORDER\n";
cout << "***********************************************\n";

cout << "Would you like a cup of coffee?\n";
cout << "1 - Yes\n";
cout << "2 - No\n";
cout << "3 - Quit\n";

cin >> x;

switch(x)
{
case 1: 
{
cout<<"Please enter your first name\n";
cin>>order1.fname;
cout << "Please enter the type of coffee:\n ";
cout << "1. Columbian\n 2. French Roast\n 3. Italian Blend\n 4. House?\n"; 
cin >> order1.pdata.name;
cout << "Please enter the size of the coffee:\n 0-Small\n 1-Regular\n 2-Medium\n 3-Large\n 4-Extra Large";
cin >> order1.pdata.size;
cout << "Please enter the amount of sugar: 0, 1, 2, 3 or 4?\n";
cin >> order1.pdata.sugar;
cout << "Please enter the strength of the coffee:\n 1-Light\n 2-Medium\n 3-Dark\n";
cin >> order1.pdata.strength;
cout << "Cream? Y or N: ";
cin >> order1.pdata.cream;
cout <<"\n\n\n";
cout << "YOUR ORDER\n";
cout << "----------\n";
cout <<"Order for: "<<order1.fname<<endl;
cout <<"Coffee type:  "<<order1.pdata.name<<endl;
cout <<"Coffee size: "<<order1.pdata.size<<endl;
cout <<"Amount of sugar:  "<<order1.pdata.sugar<<endl;
cout <<"Strength of Coffee:  "<<order1.pdata.strength<<endl;

if(order1.pdata.cream == 1)
{
    cout << "Cream: No";
}
if(order1.pdata.cream == 0)
{
    cout << "Cream: Yes";
}
total= (array[order1.pdata.size])+total;

cout << "\n\nYour total is "<<total;

ofstream myfile;
myfile.open("CustOrder.dat");
myfile << "Order: \n";
myfile << "Coffee Type: " <<order1.pdata.name <<endl;
myfile << "Size: " << order1.pdata.size <<endl;
myfile << "Sugar: " <<order1.pdata.sugar <<endl;
myfile << "Strength: " <<order1.pdata.strength <<endl;
myfile << "Cream: "<< order1.pdata.cream <<endl;
myfile << "Your total is: " << total <<endl;

break;
}
case 2:
{
cout<< "Would you like to see your order? ";
cin>>answer;
break;
}
case 3: 
{
cout<< "Goodbye";
return 0;
}
}

return 0;

}
Last edited on
And apparently I dont know how to use code tags either. My apologies
You don't need an enum. Change the enum statement to int name; and the code should compile and run.

I dont know how to use code tags

How to use code tags
http://www.cplusplus.com/articles/jEywvCM9/

Hint: you can edit your post and add them.
I have to enumerate it according to my assignment unfortunately. I actually have to do it for coffee name, size, strength.
If you need to use enums you need to learn how to use them properly. enums are not variables you can assign values to after being created, they are custom user-created data types.

https://www.learncpp.com/cpp-tutorial/45-enumerated-types/

Please read carefully the section on enum input/output.

Posting the full requirements of your assignment could be helpful for us to help you mashing the code so it works as you intend. I can see several areas that probably need changes/rewrites right now.
This is the part of the assignment:

Modify the struct from lab 4 so that the values for coffee name are enumerated as Columbian=1,
French Roast=2, Italian Blend=3, House=4; and the values for strength are enumerated as
1=light, 2=medium, 3=dark. Also, add to the struct an attribute for coffee size that will be
enumerated as small=0, regular=1, medium=2, large=3, extra-large =4. The cost is calculated as
follows small $1.50, regular $2.10, medium $2.50, large $3, extra-large $4.25.
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
#include <iostream>

struct Coffee
{
    // the enumeration type name_t is a distinct type
    enum name_t { Columbian = 1, French = 2, Italian = 3, House = 4 };

    // declare a member variable of type name_t
    name_t name ;

    // ...
};

struct Customer
{
    // ...
    Coffee pdata;
};

int main()
{
    Customer order1 ;

    // ...

    // read the integer value of coffee type into an int
    int coffee_type = -99 ; // iniotialise to an invalid value
    while( coffee_type < 1 || coffee_type > 4 ) // repeat till we get a valid input [1,4]
    {
        std::cout << "Please enter the type of coffee:\n "
                  << "1. Columbian\n 2. French Roast\n 3. Italian Blend\n 4. House\n"
                  << "? " ;
        std::cin >> coffee_type ; // for now, we assume that the user does enter an integer
    }

    // once we get here, we have a valid integer value for coffee type
    // a value of type int can be converted to the enumeration type via a cast
    order1.pdata.name = Coffee::name_t(coffee_type) ; // set the enum member to the valid value

    // just for now: so that we know what is going on
    // there is an implicit conversion from the enumeration type to an integer
    std::cout << "the integer value of order1.pdata.name is " << order1.pdata.name << '\n' ;

    // ...
}

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

int main()
{
    std::string coffee_name[]{"Columbian", "French", "Italian", "House"};
    enum coffee: int {Columbian, French, Italian, House};
    
    std::cout << coffee_name[ coffee(Columbian) ] << ' ' << coffee(Italian) << '\n';
    
    return 0;
}


Columbian 2
Program ended with exit code: 0
Topic archived. No new replies allowed.