Unless you want to do precise error handling and throw a message if the user enters something other than q or Q, you can simply place the entire processing code into an infinite loop and break if scanf fails:
#include <stdio.h>
#include <stdlib.h>
/* Main Program */
int main(void)
{
float USD;
float CAD;
float EUR;
float GBP;
float CHF;
float YEN;
int input;
/*Title of program*/
printf("\t This program will convert foreign currency to US Dollars\n\n");
/*Assigning Values*/
USD = 1.0f; /* US Dollar*/
CAD = 1.2257f; /* Canadian Dollar*/
EUR = 0.752162f; /* European Euro */
GBP = .51573f; /* British Pound */
CHF = 1.134f; /* Swiss Franc*/
YEN = 1.038f; /* Japanese Yen*/
for (;;) {
printf("Please choose form the following list of currency or press q to quit.\n");
printf("\n");
printf("[1] Canadian Dollar\n");
printf("[2] British Pound\n");
printf("[3] European Euro\n");
printf("[4] Swiss Franc\n");
printf("[5] Japanese Yen\n");
printf("\n");
if (scanf("%d", &input) != 1)
break;
/*switching statements */
switch (input)
{
case 1 :
printf("%1.6f Canadian Dollars is equal to %1.1f US Dollar.\n", CAD,USD);
break;
case 2 :
printf("%1.6f European Euro is equal to %1.1f US Dollar.\n", EUR,USD);
break;
case 3 :
printf("%1.6f British Pound is equal to %1.1f US Dollar.\n", GBP,USD);
break;
case 4 :
printf("%1.6f Swiss Franc is equal to %1.1f US Dollar.\n", CHF,USD);
break;
case 5 :
printf("%1.6f Japanese Yen is equal to %1.1f US Dollar.\n", YEN,USD);
break;
}
}
return 0;
}
That's the easiest solution. If you want something more structured and good error messages, it takes more effort, usually in the form of string input and validation followed by conversions.
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Offline 11,807 posts
since Sep 2004