-
Enumeration Issues
Hey all, I am trying to compile a simple program that uses the basics on enumeration types and I am having no luck. The program basically has an enumeration called Die with characteristics of the sides of a die and a variable of itself called roll. Basically the user enters a value and for the side of the die and the program displays the output. Here is the code....
Code:
//Die Enumeration Example
//May 2, 2007
//Demos enum with a simple program where the user selects the side of a die
#include <iostream>
using namespace std;
enum Die{side1, side2, side3, side4, side5, side6}; // declare Die enumeration and it's variable roll
int main()
{
enum Die Roll;
int c; // user input variable
bool match = false; //test for match
cout << "Die" << endl; //Program info
cout << "Created By ....." << endl;
cout << "May 2, 2007" << endl;
cout << endl;
cout << "Please enter a roll number: "<< endl;
cin >> c;
for (Roll = side1, Roll <= side6, Roll++) //find the value entered by the user
{
if (c - Roll = 0)
{
match = true; //if there's a match exit loop
break;
}
}
if (match = true) //if value entered matches a side of the die
{ //display corresponding result
switch (Roll)
{
case side1:
case side2:
case side3:
case side4:
case side5: cout << endl << "Sorry! You must roll a 6." << endl;
case side6: cout << endl << "6 that's great, you can go." << endl;
}
else
cout < endl << "No! That's not (1 - 6)!" << endl;
}
//Exit Sequence
cout << "Please press enter to continue..." << endl;
cin.ignore(1);
cin.get();
return 0;
}
and here is what the compiler gives me.....
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(22) : error C2146: syntax error : missing ';' before identifier 'cout'
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(30) : error C2676: binary '++' : 'Die' does not define this operator or a conversion to a type acceptable to the predefined operator
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(30) : error C2143: syntax error : missing ';' before ')'
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(30) : error C2143: syntax error : missing ';' before ')'
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(32) : error C2106: '=' : left operand must be l-value
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(50) : error C2181: illegal else without matching if
c:\documents and settings\owner\my documents\visual studio 2005\isaakj\assignment4\die\die\die.cpp(51) : error C2563: mismatch in formal parameter list
I was under the understanding that enum defaulted to int type if another type wasn't declared but the errors state that Roll (which should be int) can't be compared to c (int). I can't seem to figure it out. Do you guys have any ideas.....
-
for()'s three parts are separated by semicolon's, not commas.
You's using an assignment in an if() instead of comparsion, and that if() will always be true because of it.
You if statement lacks a correct closing brace. Properly indent your code, and you braces will no match up correctly.
Last, one of your cout's reads: "cout < endl..." - need another < there.
Also: Incrementing an enum? This doesn't seem too intuitive to me. (And the compiler doesn't seem to like it.) Perhaps you should just use an integer to represent what the value the dice is giving you is?
Edit: You make the assignment in if() mistake twice, with the second being rather contorted such that I suspect you might not understand it. Both = (assignment) and == (equality) are operators. The former assigns values to variables, the latter compares two things for equality. Usually you'll want to use the latter, ==, in if() statements. (Although you can use = ) Such as:
Code:
if(my_bool_variable == true)
{
// Code
}
Will exectute the code in the curly braces if my_bool_variable is true at that point in the execution of the program. This however, which is technically valid syntax, will act differently:
Code:
if(my_bool_variable = true)
{
// Code
}
(Many modern compilers will throw a warning for this) This, when executed, first assigns true to my_bool_variable. Since the = operator returns the value that it assigns, true is returned to the if() statement, and the code within will always execute. (And my_bool_variable will always get set to true.)
-
Thanks for the help with all my sloppy mistakes....after reading your post I realized how tires I am. Sorry for waisting your time with all the silly errors but thank you very much for pointing them out. Here is the new code....
Code:
#include <iostream>
using namespace std;
enum Die{side1, side2, side3, side4, side5, side6}; // declare Die enumeration and it's variable roll
int main()
{
enum Die Roll;
int c; // user input variable
bool match = false; //test for match
cout << "Die" << endl; //Program info
cout << "May 2, 2007" << endl;
cout << endl;
cout << "Please enter a roll number: "<< endl;
cin >> c;
for (Roll = side1; Roll <= side6; Roll++) //find the value entered by the user
{
if (c - Roll == 0)
{
match = true; //if there's a match exit loop
break;
}
}
if (match == true) //if value entered matches a side of the die
{ //display corresponding result
switch (Roll)
{
case side1:
case side2:
case side3:
case side4:
case side5: cout << endl << "Sorry! You must roll a 6." << endl;
case side6: cout << endl << "6 that's great, you can go." << endl;
}
}
else
{
cout << endl << "No! That's not (1 - 6)!" << endl;
}
//Exit Sequence
cout << "Please press enter to continue..." << endl;
cin.ignore(1);
cin.get();
return 0;
}
The only error left is the following :
l:\isaakj\assignment4\die\die\die.cpp(30) : error C2676: binary '++' : 'Die' does not define this operator or a conversion to a type acceptable to the predefined operator
Shouldn't I be able to increment Die since it is of type int? I was looking in one of the C programming books since my C++ doesn't go into enum much at all and that book shows incrementing the enum so wouldn't it work here?
-
Each value in an enum is an integer, but is nonetheless not an int. Instead of running a loop to check values, just use the switch, and let the default case catch an invalid input error for you. Incidentally, your switch lacks proper breaks, so you will find that a die roll other than a 6 results in both the "Sorry! You must roll a 6." and "6 that's great, you can go." messages.
-
without the loop though how do I get the user input from c to Roll?
Thanks for the tips with the breaks I totally missed that.
-
Thanks for your help all, I got it all figured out. Yesterday I was just too tired and sloppy but I got it all cleared up in about 5min this morning.