# Thread: Looping switch structure Help (newbie)

1. ## Looping switch structure Help (newbie)

Hello,

I'm doing the "currency conversion program" that you all have seen a million times already. I coded the program and it runs.
When invalid choice is made from my "menu" it ends the program
(pick 1-6 and someone picks 9).

what I'm wanting to do is to loop the switch so that if user picks "9" it will printf "Please try again" and loop the menu.

ANY help would be great because my brain is mush right now

Code:
```#include <stdio.h>

main()

{

float fr[6], input2;
int input1;

fr[0] = 1;
fr[1] = 0.7435;
fr[2] = 1.190;
fr[3] = 102.1;
fr[4] = 1.280;
fr[5] = 23.01;

while ( input1 < 6 )
{
printf("\n\t************************");
printf("\n\t* Currency  Conversion *");
printf("\n\t*        Ver 1.2       *");
printf("\n\t************************\n");
printf("\n\tSelect a currency to be converted or quit (1-6).\n");
printf("\n1)  Euro                   %.4f     \n", fr[1]);
printf("2)  Canadian Dollar        %.3f   \n", fr[2]);
printf("3)  Japanese Yen           %.1f   \n", fr[3]);
printf("4)  Australian Dollar      %.3f   \n", fr[4],);
printf("5)  Czech Republic Koruny  %.2f   \n", fr[5],);
printf("6)  Quit\n");
printf("\n\n\n");
printf("\n\nSelection: ");
scanf("%d", &input1);
switch (input1) {
case 1:
printf("\n You have selected Euro.\n");
break;
case 2:
printf("\n You have selected Canadian Dollar.\n");
break;
case 3:
printf("\n You have selected Japanese Yen.\n");
break;
case 4:
printf("\n You have selected Australian Dollar.\n");
break;
case 5:
printf("\n You have selected Czech Republic Koruny.\n");
break;
case 6:
printf("\n Thank you. \n");
break;
default:
printf("\n Sorry that is not a valid selection.\n");
}
if (input1 < 6) {
printf("\n Enter amount to be converted:");
scanf("%f", &input2);
}
else {
printf("\n Hope you enjoyed my program,");
input2 = 6;
}

if (input2 <= 0.00) {
printf("\n  Error: Please input a positive amount.\n");
}
else {
switch (input1) {
case 1:
printf("\n %.4f Euro is equivalent to %.2f American Dollars.\n",input2,input2/fr[1]);
break;
case 2:
printf("\n %.3f Canadian Dollars is equivalent to %.2f American Dollars.\n",input2,input2/fr[2]);
break;
case 3:
printf("\n  %.1f Japanese Yen is equivalent to %.2f American Dollars.\n",input2,input2/fr[3]);
break;
case 4:
printf("\n  %.3f Australian Dollar is equivalent to %.2f American Dollars.\n",input2,input2/fr[4]);
break;
case 5:
printf("\n  %.2f Czech Republic Koruny is equivalent to %.2f American Dollars.\n",input2,input2/fr[5]);
break;
case 6:
printf("\n Have a nice day!\n");
}  //end switch
} //end else
printf("\n Press Enter key to continue.......");

getchar();
printf("\n\n\n\n\n\n\n\n\n");
} //end while loop

} // end main program```

2. Just change your while loop to only match the exit condition:
Code:
`while ( input1 != 6)`
As a side note, your while() loop is checking the value of input1 before it's set to anything. You can either initialize it to something (anything but 6) at declaration or change your loop from a while to a do-while.

3. Thank you for your help!, I will also initialize the input1 at declaration.

Thanks again.