Having trouble implementing enumeration.

This is a discussion on Having trouble implementing enumeration. within the C++ Programming forums, part of the General Programming Boards category; I'm supposed to use Enumeration (Switch Statements) to determine from the user input three values which type of triangle is ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    28

    Having trouble implementing enumeration.

    I'm supposed to use Enumeration (Switch Statements) to determine from the user input three values which type of triangle is used. A Isoceles , Scalene, Equilateral, and last but not least values that don't correspond to a triangle.

    I'm just very confused with the entire topic and the tutorials don't seem to help.

    Would appreciate any references or actual in code uses of something similar so I can get a grasp on it.


    This is what ive gotten so far but im really confused on the entire usage of enumeration and/or switch statements.


    Any help would be greatly appreciated.



    Code:
    #include<iostream>
    #include<fstream>
    #include<cmath>
    #include<iomanip>
    #include<string>
    
    using namespace std;
    
    void screenHeader();
    void getValues(int &sOne, int &sTwo, int &sThree);
    void sendInput(int &sOne, int &sTwo, int &sThree);
    enum triangle {ISOSCELES, EQUILATERAL, SCALENE, NO_TRIANGLE}sOne, sTwo, sThree;
    float answer2 = 0 ;
    
    int main()
    {
       
       
       	int sOne, sTwo, sThree;
       	int another = 1; 
    	char answer;
     	system("clear");
            screenHeader();
      
     while(another != 0) {
    	getValues(sOne, sTwo, sThree);	
      	enum triangle {ISOSCELES, EQUILATERAL, SCALENE, NO_TRIANGLE}sOne, sTwo, sThree;	
       
    
    
       	cout << "                                    " << endl;
            cout << "Would you like to continue (Y or N)?  " ;
            cin  >> answer;
    	
    	if(answer != 'y' && answer != 'Y')
    	 {
    	   answer2++;
    	   another = 0 ;
    	 }	
    
            if(answer != 'y' && answer != 'Y')
            {
    	   cout << "Number of sets of input analyzed: " << answer2 << endl;
    	   cout << "PROGRAM TRIANGLE has terminated." << endl;
     	} 
       
      }
    
    
     return 0;
     }
    
    
    
    
    
    void screenHeader()
     {
       cout << "Program TRIANGLE   									       " << endl;
       cout << "-------------------------------------------------------------------------------------------" << endl;
       cout << "This program will request that you enter integar values for three sides of a triangle.     " << endl;
       cout << "The program will analyze the values that you enter and determine if the values constitute  " << endl;
       cout << "an isoceles trianle, an equilateral triangle, a scalene triangle, or that no triangle can  " << endl;
       cout << "be formed with the values entered.							       " << endl;
       cout << "-------------------------------------------------------------------------------------------" << endl;
    }
    
    
    void getValues(int &sOne, int &sTwo, int &sThree)
     {
      cout << "                                                    " << endl;
      cout << "Please enter a value for side 1 of your triangle:   " ;
      cin  >> sOne;
      cout << "Please enter a value for side 2 of your triangle:   " ;
      cin  >> sTwo;
      cout << "Please enter a value for side 3 of your triangle:   " ;
      cin  >> sThree;
      sendInput(sOne, sTwo, sThree);
    }
    
    void sendInput(int &sOne, int &sTwo, int &sThree)
     {
       cout << "                                                                                                  " << endl;  
       cout << "The lengths of the sides that you entered were:  " << sOne << ", " << sTwo << ", and " << sThree << "." << endl;
     }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    enum TRIANGLE
    {
      ISOSCELES,
      EQUILATERAL,
      SCALENE,
      NO_TRIANGLE
    };
    
    TRIANGLE Triangle;
    
    switch(Triangle)
    {
      case ISOSCELES:
      {
        std::cout << "isoceles!" << std::endl;
        break;
      }
    
       ...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Where is that placed , is basically my confusion?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    >>enum triangle {ISOSCELES, EQUILATERAL, SCALENE, NO_TRIANGLE}sOne, sTwo, sThree;

    Remove sOne, sTwo, sThree from above line to just declare type triangle but NOT declare any objects of type triangle at this time.
    _______________________________________
    >>while(another != 0) {
    >>getValues(sOne, sTwo, sThree);
    >>enum triangle {ISOSCELES, EQUILATERAL, SCALENE, NO_TRIANGLE}sOne, sTwo, sThree;

    Replace last line of above with declaration of a triangle type which will be intialized (or assigned) with the return value of a function call to a function which has code to calculate whether sOne, sTwo, sThree constitute a triangle, and if so, what type, by sending it sides sOne, sTwo, and sThree and has type triangle as the return type. Then use the return value as the parameter for the switch call. Use Magos example to build the switch statement. So something like this:
    Code:
     
    triangle result = calculateTriangleType(sOne, sTwo, sThree);
     
    switch(result)
    //etc.
     
    or if you want to short cut the process you could do this:
     
    switch(calculateTriangleType(sOne, sTwo, sThree))
     
    where calculateTriangleType() returns type triangle.
    You're only born perfect.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Still very lost as to where the switch statement gets placed as well as the placement of other items mentioned.

    This is how it looks at the moment.

    How exactly do I use this switch statement to make it serve a purpose?

    Code:
    #include<iostream>
    #include<fstream>
    #include<cmath>
    #include<iomanip>
    #include<string>
    
    using namespace std;
    
    void screenHeader();
    void getValues(int &sOne, int &sTwo, int &sThree);
    void sendInput(int &sOne, int &sTwo, int &sThree);
    calculateTriangleType(int &sOne,int &sTwo,int &sThree);
    enum triangle {ISOCELES, EQUILATERAL, SCALENE, NO_TRIANGLE};
    
    
    
     int main()
      {
     	int  setnumber = 0;   
    	int another = 1;
    	int sOne, sTwo, sThree;
    	char answer;  
    	system("clear");
            screenHeader();
      
        while(another != 0) {
    		getValues(sOne, sTwo, sThree); 
    		cout << "                                    " << endl;
            	cout << "Would you like to continue (Y or N)?  " ;
            	cin  >> answer;
    		setnumber++;
    	
          if(answer != 'y' && answer != 'Y')
    	  {
    	   another = 0 ;
    	  }	
    
          if(answer != 'y' && answer != 'Y')
              {
    	   cout << "Number of sets of input analyzed: " << setnumber << endl;
    	   cout << "PROGRAM TRIANGLE has terminated." << endl;
     	  } 
       
          }
    
    
     	return 0;
       }
    
       void screenHeader()
           {
       	cout << "Program TRIANGLE   									       " << endl;
       	cout << "-------------------------------------------------------------------------------------------" << endl;
       	cout << "This program will request that you enter integer values for three sides of a triangle.     " << endl;
       	cout << "The program will analyze the values that you enter and determine if the values constitute  " << endl;
       	cout << "an isoceles trianle, an equilateral triangle, a scalene triangle, or that no triangle can  " << endl;
       	cout << "be formed with the values entered.							       " << endl;
       	cout << "-------------------------------------------------------------------------------------------" << endl;
           }
    
    
    
        void getValues(int &sOne, int &sTwo, int &sThree)
           {
      	cout << "                                                    " << endl;
      	cout << "Please enter a value for side 1 of your triangle:   " ;
      	cin  >> sOne;
      	cout << "Please enter a value for side 2 of your triangle:   " ;
      	cin  >> sTwo;
      	cout << "Please enter a value for side 3 of your triangle:   " ;
      	cin  >> sThree;
      	sendInput(sOne, sTwo, sThree);
           }
    
         void sendInput(int &sOne, int &sTwo, int &sThree)
           {
       	cout << "                                                                                                  " << endl;  
       	cout << "The lengths of the sides that you entered were:  " << sOne << ", " << sTwo << ", and " << sThree << "." << endl;
           }
     enum TRIANGLE
    {
      ISOSCELES,
      EQUILATERAL,
      SCALENE,
      NO_TRIANGLE
    };
    
    TRIANGLE Triangle;
    
    switch(Triangle)
    {
      case ISOSCELES:
      {
        std::cout << "isoceles!" << std::endl;
        break;
      }
      case EQUILATERAL:
      {
        std::cout << "Equilateral!" << std::endl;
        break;
      }
      case SCALENE:
      {
        std::cout << "Scalene!" << std::endl;
        break;
      }
      case NO_TRIANGLE:
      {
        std::cout << "No Triangle!" << std::endl;
        break;
      }

  6. #6
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly.
    Code:
    switch (expression) {
    	case const-expr:	statements
    	case const-expr:	statements
    	default:	statements
    }
    Each case is labeled by one or more integer-valued constants or constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labeled default is executed if none of the other cases is satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.

    The break statment causes an immediate exit from the switch. Because cases serve just as labels, after the code for one case is done, execution falls through to the next unless you take explicit action to escape. break and return are the most common ways to leave a switch.

    Falling through cases is a mixed blessing. On the positive side, it allows several cases to be attached to a single action. For example:
    Code:
    int main(void) {
    	int tmp = 5;
    
    	switch (tmp) {
    		case 1:
    		case 2:
    		case 3:
    			/* Do something if 1, 2, or 3 */
    		break;	/* Break statment if case expression matched */
    		case 4:
    		case 5:
    			/* Do something if 4 or 5 */
    		break;	/* Break statment if case expression matched */
    	}
    
    	return 0;
    }
    Keep in mind, this implies that normally each case must end with a break to prevent falling through to the next. Falling through from one case to another is not robust, being prone to disintegration when the program is modified. With the exception of multiple labels for a single computation, fall-throughs should be used sparingly, and commented.

    In your case, you would use your switch statement as a supplement to an if/else statement. Here is a prime example:
    Code:
    #include <iostream>
    using namespace std;
    
    enum TRIANGLE
    {
    	ISOSCELES,
    	EQUILATERAL,
    	SCALENE,
    	NO_TRIANGLE
    };
    
    int main() {
    	TRIANGLE Triangle;
    
    	// Set value of Triangle
    	Triangle = EQUILATERAL;
    
    	// Find a match
    	switch(Triangle) {
    		// If Isosceles
    		case ISOSCELES:
    		{
    			cout << "Isoceles!" << endl;
    			break;
    		}
    		// If Equilateral
    		case EQUILATERAL:
    		{
    			cout << "Equilateral!" << endl;
    			break;
    		}
    		// If scalene
    		case SCALENE:
    		{
    			cout << "Scalene!" << endl;
    			break;
    		}
    		// If no triangle
    		case NO_TRIANGLE:
    		{
    			cout << "No Triangle!" << endl;
    			break;
    		}
    	}
    
    	return 0;
    }
    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Code:
    #include<iostream>
    //etc
    
    using namespace std;
     
    enum Triangle {ISOCELES, EQUILATERAL, SCALENE, NO_TRIANGLE};
    
    void screenHeader();
    void getValues(int &sOne, int &sTwo, int &sThree);
    void sendInput(int &sOne, int &sTwo, int &sThree);
    
    Triangle calculateTriangleType(int &sOne,int &sTwo,int &sThree);
    
    
    
     int main()
      {
    	int  setnumber = 0;   
    		int another = 1;
    	int sOne, sTwo, sThree;
    	char answer;
    	Triangle triangle;
    	system("clear");
    	screenHeader();
      
    	while(another != 0) {
    	  getValues(sOne, sTwo, sThree); 
     
    	  triangle = calculateTriangleType(sOne, sTwo, sThree);
     
    	  switch(triangle)
    	  {
    		case ISOSCELES:
    		 std::cout << "isoceles!" << std::endl;
    		 break;
    	   //etc.
    	 }			
      
      cout << "Would you like to continue (Y or N)?  " ;
      cin  >> answer;
      setnumber++;
    	
    
       if(answer != 'y' && answer != 'Y')
    	  {
    	   another = 0 ;
    	  } 
    
       if(answer != 'y' && answer != 'Y')
       {
    		 cout << "Number of sets of input analyzed: " << setnumber << endl;
    		 cout << "PROGRAM TRIANGLE has terminated." << endl;
     		} 
      }
      return 0;
    }
    
       void screenHeader()
       {
       }
    
    	void getValues(int &sOne, int &sTwo, int &sThree)
    	{
    	}
    	
       void sendInput(int &sOne, int &sTwo, int &sThree)
       {
       }
    
    Triangle calculateTriangleType(int &sOne,int &sTwo,int &sThree)
    {
    	//declare value to return
    	Triangle result;
     
       //determine whether the parameters create a triangle or not and if they do. what type it is.
       if(sOne == sTwo && sOne == sThree)
    	 result = EQUILATERAL;
     
       //etc.
     
       return result;
    }
    You're only born perfect.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Switch statement:

    A switch statement is like a series of if statements. switch(num) says, "let's take a look at the value of num":
    Code:
    int num = 2;
    switch(num)
    {
    ...
    }
    This case statement:
    Code:
    case 0:
    	cout<<"num is equal to 0"<<endl;
    	break;
    ...
    ...
    }
    say, "if num is equal to 0, execute the following indented lines, and if num doesn't equal 0, skip the lines." In the code above, if num is equal to 0, then a message is displayed. Then, the 'break' says, "the switch is done--skip down to the line after the closing brace of the switch statement and continue execution there."

    You can also have a 'default' in a switch:
    Code:
    switch (num)
    {
    ...
    ...
    ...
    default:
    	cout<<"no match found"<<endl;
    }
    The default says, "if num hasn't equaled any of the cases, then execute the following indented lines."

    You can do the exact same thing that a switch statement does with a bunch of if statements:
    Code:
    int num = 2;
    if(num==0)
        cout<<"num is equal to 0"<<endl;
    else if(num==1)
        cout<<"num is equal to 1"<<endl;
    else if(num==2)
        cout<<"num is equal to 2"<<endl;
    else
        cout<<"no match found"<<endl;
    but a switch() is more efficient because of internal memory stuff.

    Enumerations:
    Instead of an enumeration, you could do this:
    Code:
    int triangle = 1;
    switch (triangle)
    {
    case 0:
    	//call some function
    	break;
    case 1:
    	//call another function
    	break;
    case 2:
    	//call a third function
    	break;
    case 3:
    	cout<<"It's not a triangle."<<endl;
    }
    But, it's not exactly clear to someone reading the code what the cases 0, 1, 2 mean. Enumerations let you assign words to integers to make your code clearer. The line:

    enum Triangle{ISOSCELES, EQUILATERAL, SCALENE, NO_TRIANGLE};

    says, "ISOSCELES is going to be the same thing as 0, EQUILATERAL is going to be the same thing as 1, SCALENE is going to be the same thing as 2, NO_TRIANGLE is going to be the same thing as 3."
    The integer values are determined just like for an array index: the first word in the enum list is 0, the next is 1, etc.

    Now, instead of using plain old integers, you can get fancy and create a Triangle type variable, and assign it a word:

    Triangle T = ISOSCELES;

    That is the same thing as saying T = 0. Then, in your switch you would have this:
    Code:
    enum Triangle{ISOSCELES, EQUILATERAL, SCALENE, NO_TRIANGLE};
    
    Triangle T= ISOSCELES;
    
    switch (T)
    {
    case ISOSCELES:
    	//call some function
    	break;
    case EQUILATERAL:
    	//call another function
    	break;
    case SCALENE:
    	//call a third function
    	break;
    case NO_TRIANGLE:
    	cout<<"It's not a triangle."<<endl;
    }
    You don't even need to know the integer values of the words you created in your enumeration. All you need to do is compare the word assigned to T to each of the possible words. If the words are equal, then that case will be executed. Now, someone reading your code will know very easily: if it's an isosceles triangle, then a certain function is called, if it's an equlateral triangle another function is called, etc.
    Last edited by 7stud; 03-02-2005 at 06:13 PM.

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    28
    Wow thanks a lot for the great detail in explanation and help thus far. It has helped me greatly actually understand what their purpose is and how they are implemented.

    Thank you ALL very much.

    I sure hope I can grasp all of this soon.

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 Issues
    By cdn_bacon in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2007, 03:26 PM
  3. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  4. A little trouble implementing a blur filter
    By omishompi in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2006, 09:57 AM
  5. enumeration
    By C-Struggler in forum C Programming
    Replies: 5
    Last Post: 03-13-2003, 09:36 AM

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