Jump to content


Check out our Community Blogs

neo1691

Member Since 18 Feb 2012
Offline Last Active Aug 06 2013 10:31 PM
-----

#639558 C++ loop help

Posted by neo1691 on 23 September 2012 - 02:55 AM

Hi.
I compiled and debugged your code.
I see you have used multiple while loops in your code.

Also as you say the 4th part of your code is wrong because you have not written any code to exit the loop.


while(choice >=1 && choice <=4)
		 {
			 switch (choice)
			 {

			 case QUIT_WAY2:
				 cout << "Enter a length OF THE triangle base" <<endl;
				 cin >> base;
				 cin >> choice;
				 break;

			 }
		 }

Well this is how your code should be structured (I am skipping re-writing the whole code again)


int main
{
    //declare your variables

    //display the menu for the user

    //get the input in a variable called choice

    while(1) //infinite loop
    {
        switch (choice)
        {
        case CIRCLE:
            //code to calculate circle area
            //get input again from user in choice
            break;



        case RECT:
            //code to calculate rectangle area
            //get input again from user in choice
            break;


        case TRIANGLE:
            //code to calculate TRIANGLE area
            //get input again from user in choice
            break;

        case EXIT:
            exit(1); //you programs terminates here

        }
    }
}





Thats all, just one while statement and you are fine. You can also use a do-while loop (it will be much better menu driven program if used).

Hope that helps
  • 1


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download