I'm trying to implement a menu choice, so the user can define what kind of triangle they have, with the focus on class implementation.
I'm trying to define a class attribute that offers the choices, but then depending on the choice, sends the compiler to the appropriate attribute.
Example:

int Triangle::CalcDecider(int choice1)
{
    if (choice1 == 1)
    {
          Cool.Equilateral();
    }
    //Additional code choices 
}

Where Triangle is the class, CalcDecider is the function, and Cool is the object I created.

Should I scrap this idea and use a switch instead? I've never used a switch before, but it was recommended that I follow that route.
Is it even possible to follow the route I'm taking?

A switch statement is really nothing more than a fancy set of if statements and makes the code easier to follow. The default case is not required but often useful just like the else statement.

int Triangle::CalcDecider(int choice1)
{
   switch(choice1)
   {
          case 1:
               Cool.Equilateral();
               break;
          case 2:
               Cool.Square();
               break;
          default:
                cout << "Huh?\n";
                break;
    }
    //Additional code choices 
}

There are times when if statements are better, such as checking for ranges of numbers if( choice1 >= 1 and choice1 <= 10) That would take 10 cases in a switch statement: case 1: case 2: ... case 10:

case 2:
               Cool.Square();
               break;
          default:
                cout << "Huh?\n";
                break;

I've never heard of that kind of triangle. Are you sure it's a valid form?

Ok, so I changed the if statements to a switch case as follows:

//Deciding which equations to use
int Triangle::CalcDecider(int choice1)
{
    switch(choice1)
    {
    case 1:
          Cool.Equilateral();
          break;
    case 2:
          Cool.Isosceles();
          break;
    case 3:
         Cool.Right();
         break;
    case 4:
         Cool.Obtuse();
         break;
    case 5:
         Cool.Acute();
         break;
    case 6:
         Cool.TriangleHelp();
         choice1 = Cool.TriangleType();
         break;
    default:
        std:cerr << "Invalid Choice. Try again\n" << endl;
        choice1 = Cool.TriangleType();
    }
}

but I still get the error that "Cool" is an undeclared function, which is the same error I was getting with the if statements. I did declare it already, up in main with my Triangle Cool; initialization.
To fix this, I simply put the switch case as part of main, instead of being a property of the Triangle class (which does work). I really want it out of main though, as a property (or function, or whatever it needs to be). Is it possible to call properties from a switch property?

Well I guess if you can define Cool outside main , And as a GLOBAL var is. The problem will be solved.

So try defining Cool outside main . or any other function So that it becomes global.


Hope that works ;)

I've never heard of that kind of triangle. Are you sure it's a valid form?

You mean you have never heard of a four-sided triangle :twisted:

Alright, so I declared Cool directly above main (along with pi), which I suppose makes it a global variable, and all works well.
The forums say that global variables are to be avoided however.
What do I do to make the initialization of Cool a parameter? After reading online, I believe I need a formal parameter...

That should be calling members of the Triangle class which is not dependent on any given instance. So just remove Cool.

//Deciding which equations to use
int Triangle::CalcDecider(int choice1)
{
    switch(choice1)
    {
    case 1:
          Equilateral();
          break;
    case 2:
          Isosceles();
          break;
    case 3:
         Right();
         break;
    case 4:
         Obtuse();
         break;
    case 5:
         Acute();
         break;
    case 6:
         TriangleHelp();
         choice1 = TriangleType();
         break;
    default:
        std:cerr << "Invalid Choice. Try again\n" << endl;
        choice1 = TriangleType();
    }
}

Or, some people might code it like this, which is the same thing as above

//Deciding which equations to use
int Triangle::CalcDecider(int choice1)
{
    switch(choice1)
    {
    case 1:
          this->Equilateral();
          break;
    case 2:
          this->Isosceles();
          break;
    case 3:
         this->Right();
         break;
    case 4:
         this->Obtuse();
         break;
    case 5:
         this->Acute();
         break;
    case 6:
         this->TriangleHelp();
         choice1 = this->TriangleType();
         break;
    default:
        std:cerr << "Invalid Choice. Try again\n" << endl;
        choice1 = this->TriangleType();
    }
}

This worked perfectly as well.
Thanks again!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.