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();
}
cin.clear()
, see what happens. Besides, you have somebreak
issues I think ...