Enumeration Issues

This is a discussion on Enumeration Issues within the C++ Programming forums, part of the General Programming Boards category; Hey all, I am trying to compile a simple program that uses the basics on enumeration types and I am ...

  1. #1
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17

    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.....

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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.)
    Last edited by Cactus_Hugger; 05-02-2007 at 09:12 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    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?
    Last edited by cdn_bacon; 05-02-2007 at 09:23 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    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.

  6. #6
    Registered User cdn_bacon's Avatar
    Join Date
    Mar 2007
    Posts
    17
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding/Implementing Enumeration
    By Iconate in forum C Programming
    Replies: 15
    Last Post: 10-10-2008, 10:16 AM
  2. Enumeration problem
    By baniakjr in forum C++ Programming
    Replies: 8
    Last Post: 11-11-2006, 02:32 PM
  3. Having trouble implementing enumeration.
    By RP319 in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 07:03 PM
  4. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 09:01 PM

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