CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2015
    Posts
    4

    Assistance with enumeration in combination with user-defined functions.

    Hello:

    I am having a little trouble with an assignment that involves enumeration. While my code seems to work, my instructor informed me that I am only to use user-defined function, with for statements, while loops, switch statements in the main body. Embarrassingly, for whatever reason, I can't seem to wrap my head how I am supposed to rewrite my switch statement as a user-defined function.

    Here is what the assignment is asking for:

    1a) Define an enumeration type, triangleType, that has the value scalene, isosceles, equilateral, and noTriangle.

    b) Write a function, triangleShape that takes as parameters three numbers, each of which represents the length of a side of thed triangle. The function should return the shape of the triangle. (Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side).

    c) Write a program that prompts the user to input the length of the side of a triangle and outputs the shape of the triangle.

    And here is what I have so far:

    Code:
    #include <iostream>
    using namespace std;
    
    //user-defined (enumerator) data type
    typedef int side;
    enum triangleType { scalene, isosceles, equilateral, noTriangle } shape;
    
    void sideTri(side&, side&, side&);
    triangleType triangleShape(side&, side&, side&, triangleType&);
    
    
    int main() {
    	side a;
    	side b;
    	side c;
    	int type;
    
    
    	type = triangleShape(a, b, c, shape);
    
    
    	switch (type) {
    	case 0:
    		cout << "This is a scalene triangle." << endl;
    		break;
    	case 1:
    		cout << "This is an isosceles triangle." << endl;
    		break;
    	case 2:
    		cout << "This is an equilateral triangle." << endl;
    		break;
    	case 3:
    		cout << "This is not a triangle." << endl;
    		break;
    	}
    
    	return 0;
    }
    
    void sideTri(side& a, side& b, side& c)
    {
    	cout << "Enter side a: ";
    	cin >> a;
    
    	cout << "Enter side b: ";
    	cin >> b;
    
    	cout << "Enter side c: ";
    	cin >> c;
    }
    
    triangleType triangleShape(side& a, side& b, side& c, triangleType& shape) 
    {
    
    	if (a == b && b == c)			        return equilateral;
    
    	else if (a == b || b == c || c == a)		return isosceles;
    
    	else if (a != b && b != c && c != a)		return scalene;
    
    	else						return noTriangle;
    }
    Any help is greatly appreciated. Thank you.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Assistance with enumeration in combination with user-defined functions.

    Where are you calling sideTri() to enter the values?

    In triangleShape, there is no need to pass the parameters by reference as the values are not being changed. As the type is POD pass by value is fine. Also the parameter shape is not used as the type is the return value.

    In the switch statement, the cases should be the various enum names (scalene, noTriangle etc) rather than the numbers.

    The switch statement can be easily put in a function. Just have a function called something like displayType(triangleType tType){...} and call it from main() after you have called triangleShape(). In fact you could do something like displayType(triangleShape(a, b, c)); In displayType() just have the switch statement.

    In fact you don't need a switch statement at all! You could have an array of the names indexed by the type. Also if you have a struct containing the lengths of the sides then sideTri() could return a type of this struct and then your main program would be just 1 line!

    Code:
    struct Sides
    {
    	side a, b, c;
    };
    ...
    
    Sides sideTri();
    triangleType triangleShape(Sides sides);
    void displayType(triangleType tType);
    ...
    int main()
    {
    	displayType(triangleShape(sideTri()));
    }
    NB How are you determining if the entered side values are actually a triangle?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 2015
    Posts
    208

    Re: Assistance with enumeration in combination with user-defined functions.

    Quote Originally Posted by nicbarker View Post
    Embarrassingly, for whatever reason, I can't seem to wrap my head how I am supposed to rewrite my switch statement as a user-defined function.
    I would ask my teacher to be more specific but maybe it's just a question of using symbolic names in the switch rather than the integers they encode.

    Note that there's a new kind of enum (called strongly typed enum, or enum class) that would more or less force you to do that because it's considered safer.

  4. #4
    Join Date
    Oct 2015
    Posts
    4

    Re: Assistance with enumeration in combination with user-defined functions.

    Quote Originally Posted by 2kaud View Post
    Where are you calling sideTri() to enter the values?

    In triangleShape, there is no need to pass the parameters by reference as the values are not being changed. As the type is POD pass by value is fine. Also the parameter shape is not used as the type is the return value.

    In the switch statement, the cases should be the various enum names (scalene, noTriangle etc) rather than the numbers.

    The switch statement can be easily put in a function. Just have a function called something like displayType(triangleType tType){...} and call it from main() after you have called triangleShape(). In fact you could do something like displayType(triangleShape(a, b, c)); In displayType() just have the switch statement.

    In fact you don't need a switch statement at all! You could have an array of the names indexed by the type. Also if you have a struct containing the lengths of the sides then sideTri() could return a type of this struct and then your main program would be just 1 line!

    Code:
    struct Sides
    {
    	side a, b, c;
    };
    ...
    
    Sides sideTri();
    triangleType triangleShape(Sides sides);
    void displayType(triangleType tType);
    ...
    int main()
    {
    	displayType(triangleShape(sideTri()));
    }
    NB How are you determining if the entered side values are actually a triangle?
    Hello, 2kaud, unfortunately my instructor would prefer me not to use arrays yet. I don't know the logic behind that but so be it. In regard to the assignment, I am using passing by reference due to the fact that later on she informed us we will be doing something later with code that will require us to this method. The manner in which I am determining if the value actually give me can be seen in my new code. I would appreciate it if you would take a look:

    Code:
    #include <iostream>
    using namespace std;
    
    //user-defined (enumerator) data type
    typedef int side;
    enum triangleType { scalene, isosceles, equilateral, noTriangle } shape;
    
    //Prototypes of user-defined functions.
    void sideTri(side&, side&, side&);
    triangleType triangleShape(side&, side&, side&, triangleType&);
    void determinedisplayTri(triangleType&);
    
    int main() 
    {
    	side a;
    	side b;
    	side c;
    	int type;
    
    
    	type = triangleShape(a, b, c, shape);
    
    	return 0;
    }
    
    //Headers for user-defined functions.
    
    
    void sideTri(side& a, side& b, side& c)
    {
    	do
    	{
    
    		cout << "Enter side a: ";
    		cin >> a;
    
    		cout << "Enter side b: ";
    		cin >> b;
    
    		cout << "Enter side c: ";
    		cin >> c;
    		if (a <= 0 || b <= 0 || c <= 0)//Error checking
    		{
    			cout << "Error: Cannot have an input with a value equal or less than zero.\nPlease check your values.\n" << endl;
    		}
    	} while (a <= 0 || b <= 0 || c <= 0);
    }//end of sideTri
    
    triangleType triangleShape(side& a, side& b, side& c, triangleType& shape) 
    {
    
    	if (a == b && b == c)			        return equilateral;
    
    	else if (a == b || b == c || c == a)		return isosceles;
    
    	else if (a != b && b != c && c != a)		return scalene;
    
    	else if (a + b < c || b + c < a || a + c < b)					return noTriangle;
    }//End of triangleType
    
    
    void determinedisplayTri(triangleType& type)
    {
    	switch (type)
    	{
    	case scalene:
    		cout << "This is a scalene triangle." << endl;
    		break;
    	case isosceles:
    		cout << "This is an isosceles triangle." << endl;
    		break;
    	case equilateral:
    		cout << "This is an equilateral triangle." << endl;
    		break;
    	case noTriangle:
    		cout << "This is not a triangle." << endl;
    		break;
    	}
    }//End of determineTri
    Sorry it took me so long to reply, and thank you for your input. It is greatly appreciated.

  5. #5
    Join Date
    Oct 2015
    Posts
    4

    Re: Assistance with enumeration in combination with user-defined functions.

    New code I have seem to almost work. However, it give scalene when it should be giving me notTriangle for certain values. Any help is appreciated. Here is my code:

    Code:
    #include <iostream>
    using namespace std;
    
    //user-defined (enumerator) data type
    typedef int side;
    enum triangleType { scalene, isosceles, equilateral, noTriangle };
    
    //Prototypes of user-defined functions.
    void sideTri(side&, side&, side&);
    triangleType triangleShape(side&, side&, side&, triangleType&);
    void determinedisplayTri(triangleType&);
    
    int main() 
    {
    	side a;
    	side b;
    	side c;
    	triangleType type;
    
    	sideTri(a, b, c);
    	type = triangleShape(a, b, c, type);
    	determinedisplayTri(type);
    	
    
    	system("pause");
    	return 0;
    }
    
    //Headers for user-defined functions.
    
    
    void sideTri(side& a, side& b, side& c)
    {
    	do
    	{
    
    		cout << "Enter side a: ";
    		cin >> a;
    
    		cout << "Enter side b: ";
    		cin >> b;
    
    		cout << "Enter side c: ";
    		cin >> c;
    		if (a <= 0 || b <= 0 || c <= 0)//Error checking
    		{
    			cout << "Error: Cannot have an input with a value equal or less than zero.\nPlease check your values.\n" << endl;
    		}
    	} while (a <= 0 || b <= 0 || c <= 0);
    }//end of sideTri
    
    triangleType triangleShape(side& a, side& b, side& c, triangleType& type) 
    {
    
    	if (a == b && b == c)			        return equilateral;
    
    	else if (a == b || b == c || c == a)		return isosceles;
    
    	else if (a != b && b != c && c != a)		return scalene;
    
    	else if (a + b < c || b + c < a || a + c < b)	 return noTriangle;
    }//End of triangleType
    
    
    void determinedisplayTri(triangleType& type)
    {
    	switch (type)
    	{
    	case scalene:
    		cout << "This is a scalene triangle." << endl;
    		break;
    	case isosceles:
    		cout << "This is an isosceles triangle." << endl;
    		break;
    	case equilateral:
    		cout << "This is an equilateral triangle." << endl;
    		break;
    	case noTriangle:
    		cout << "This is not a triangle." << endl;
    		break;
    	}
    }//End of determineTri
    Thanks.

  6. #6
    Join Date
    Jun 2015
    Posts
    208

    Re: Assistance with enumeration in combination with user-defined functions.

    Quote Originally Posted by nicbarker View Post
    However, it give scalene when it should be giving me notTriangle for certain values.
    It's because of the order of the tests.

    Even if it's not a triangle it can still test positive for isosceles and scalene.

    I would make sure it really is a triangle by putting the no triangle test first. Then if it's not equilateral nor isosceles you know it must be scalene without even testing for that. The order of the equilateral and isoscelets tests also matters since an equilateral triangle also tests positive for isosceles.

    But note that your test for no triangle is slightly flawed when put first. For example 1,1,2 which is a line will pass as a triangle. So it must be tuned.
    Last edited by tiliavirga; November 19th, 2015 at 01:47 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured