0

I had to write a Red Black Tree for my Algorithms class and then write a menu to use for inserting, deleting, searching etc... for elements. So I thought that a Switch Statement would be the way to go, but when it breaks out of a case it always goes directly to the return statement and ends the program even though I'm not entering "0".

I've been up for about 24 hours straight and programming for the last 16 of that, so I apologize if its something stupid or I didn't explain it very well.

while ( true )
{
    int userinput = NULL;
    PrintMenu();
    cin >> userinput;
    cin.clear();
    cin.ignore( 10000 , '\n' );
    switch ( userinput )
    {
        case 0:
            {
            return 0;
            }
        case 1:
            {
                while (true )
                {
                cout << "Enter an integer to be entered into the Red Black Tree or any letter to exit:\n";
                if ( !(cin >> userinput) )
                    break;
                cin.clear();
                cin.ignore( 10000, '\n' );
                data.Insert( userinput );
                data.PrintInOrder();
                }
            }
        case 2:
            {
                while (true )
                {
                cout << "WARNING : This mode allows entering of duplicate numbers.\n";
                cout << "Enter an integer to be entered into the Red Black Tree or any letter to exit:\n";
                if ( !(cin >> userinput) )
                    break;
                cin.clear();
                cin.ignore( 10000, '\n' );
                data.Insert( userinput );
                data.PrintInOrder();
                }
            }
        case 3:
            {
                while (true )
                {
                cout << "Enter an integer to search the Red Black Tree for or any letter to exit:\n";
                if ( !(cin >> userinput) )
                    break;
                cin.clear();
                cin.ignore( 10000, '\n' );
                data.Search( userinput );
                }
            }
        case 4:
            {
                while (true )
                {
                cout << "Enter an integer to be deleted from the Red Black Tree or any letter to exit:\n";
                if ( !(cin >> userinput) )
                    break;
                cin.clear();
                cin.ignore( 10000, '\n' );
                data.Delete( userinput );
                data.PrintInOrder();
                }
            }
        case 5:
            {
                while (true )
                {
                cout << "WARNING : This mode deletes all copies of an integer.\n";
                cout << "Enter an integer to delete from the Red Black Tree or any letter to exit:\n";
                if ( !(cin >> userinput) )
                    break;
                cin.clear();
                cin.ignore( 10000, '\n' );
                data.DeleteAll( userinput );
                data.PrintInOrder();
                }
            }
        case 6:
                data.PrintInOrder();
                break;
        case 7:
            {
                cout << "This Function tests if the tree passes all 5 Criteria of a Red Black Tree.\n";
                data.IsRBT();
                cout << "Test Finished, if you see no Violations then it passed. Press any letter to exit.\n";
                if ( !(cin >> userinput) )
                    break;
            }
    }
    system("CLS");
    data.PrintInOrder();
}
2
  • Are you trying to loop...?
    – nhgrif
    Commented Nov 12, 2013 at 13:17
  • remove the cin.clear(), see what happens. Besides, you have some break issues I think ... Commented Nov 12, 2013 at 13:20

2 Answers 2

4

You are missing break statements between the cases, meaning if case 1 is executed then case 2, 3, 4, 5 and 6 are also executed.

The break statement inside the loop only breaks out of the loop, and the innermost loop as well not any other loop. If you want to break out of the outer loop you have to have e.g. some state variable that is checked by the outer loop. Like

bool go_on = true;
while (go_on)
{
    // Some code...

    switch (some_condition)
    {
    case 1:
        while (true)
        {
            if (some_other_condition)
            {
                go_on = false;  // Tell outer loop to stop
                break;  // Break out of inner loop
            }
        }
        break;  // End the "case"

    // More cases...
    }
}

Also, you have lots of common code used in multiple places, having that is not a good design pattern. Think about what happens when you want to change it, but forget to change it in one place.

1
  • Yeah, I know its not the best code, I just really wanted to get something working before go to my physics class and go to bed. Thanks for mentioning I forgot my break statements... I feel dumb now.
    – RJ Bernau
    Commented Nov 12, 2013 at 13:40
1

what you are doing is:

switch(cond)
{
case 0:
  f0();
case 1:
  f1();
case 2:
  f2()
}

lets assume cond == 1. this means f1() and f2() will be executed because, you have no breaks for your cases.

I think what you want to do is:

switch(cond)
    {
    case 0:
      f0();
      break;
    case 1:
      f1();
      break;
    case 2:
      f2();
      break;
    default:  // you should always treat the default case in a switch statement
       //some for of error handling or pint a message 
      break;
    }

Assume cond == 1 (again); this time only f1() will execute

Not the answer you're looking for? Browse other questions tagged or ask your own question.