switch statement that uses loop

This is a discussion on switch statement that uses loop within the C++ Programming forums, part of the General Programming Boards category; Hi I am trying to use a loop in this switch statement so i can have the user select another ...

  1. #1
    Registered User mike's Avatar
    Join Date
    Aug 2001
    Posts
    12

    switch statement that uses loop

    Hi
    I am trying to use a loop in this switch statement so i can have the user select another choice.
    here is my code. choice is an interger variable and the cases are funtion calls. they all work except i cant figure out the loop and where to put it. I thought it would go in the default section of the switch. hope you can help


    switch(choice)
    {
    case 1: smallest(first,second,third,fourth,fifth);
    break;
    case 2: largest(first,second,third,fourth,fifth);
    break;
    case 3: sum(first,second,third,fourth,fifth);
    break;
    case 4: average(first,second,third,fourth,fifth);
    break;
    case 5: printDisplay(first,second,third,fourth,fifth);
    break;
    default: cout<<"\aError! choice is (1) or (2) or (3) or (4) or (5) please try again\n";
    break;

    }

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    591
    Put the switch stament in the loop like so

    choice == TRUE;
    while(choiche)
    {
    switch(choice)
    {
    case 1: smallest(first,second,third,fourth,fifth);
    break;
    case 2: largest(first,second,third,fourth,fifth);
    break;
    case 3: sum(first,second,third,fourth,fifth);
    break;
    case 4: average(first,second,third,fourth,fifth);
    break;
    case 5: printDisplay(first,second,third,fourth,fifth);
    break;
    default: cout<<"\aError! choice is (1) or (2) or (3) or (4) or (5) please try again\n";


    ""Ask the user if they want to continue"" and set choice to FALSE if they don't""
    }
    }
    Last edited by Barjor; 02-22-2002 at 04:19 PM.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,136
    That will work but you used the same var twice when you should use 2 different variables for the loop and the switch where the one in the loop is set to true originally. Also, a do...while loop will work in this situation too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  2. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  3. switch statement inside a do while
    By Chaplin27 in forum C++ Programming
    Replies: 4
    Last Post: 09-14-2004, 09:33 PM
  4. Switch statement
    By big146 in forum C++ Programming
    Replies: 7
    Last Post: 06-25-2004, 08:16 AM
  5. begginner and I need help, switch statement
    By trkpony in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21