Web Development

General discussion


Simple C Programming Question

By tturner33 ·
For an assignment, we have to write a program that allows a user to select one of five choices of foreign currency and then convert it to US dollars. I have the program compiling without errors but it returns a conversion of $0.00. Can someone look at my code and tell me what I'm missing? The code is listed below (Sorry about the word-wraps). Thanks.

/* cur_conv.c - Currency Conversion Program Version 1.1 */
/* Currency Conversion data as of 12/03/04 */
/* Source data provided by http://finance.yahoo.com */

#include <stdio.h>
#define ARS 0.3356;
#define GBP 1.9434;
#define EUR 1.3453;
#define CHF 0.8841;
#define ZMK 0.0002;
/* Main Program */
int main (void)
float usd;
float conversion;
float newrate;
int selection;
printf("This program will convert foreign currency to US Dollars\n");
printf("Argentine Peso\t(ARS) * 0.3356 = USD\n"); /* First conversion rate */
printf("British Pound\t(GBP) * 1.9434 = USD\n"); /* Second conversion rate */
printf("European Euro\t(EUR) * 1.3453 = USD\n"); /* Third conversion rate */
printf("Swiss Franc\t(CHF) * 0.8841 = USD\n"); /* Fourth conversion rate */
printf("Zambian Kwacha\t(ZMK) * 0.0002 = USD\n\n"); /* Fifth conversion rate */

printf("Enter currency of your choice:\t[1] ARS\t[4] CHF\n"); /* Tabs entered for readability */
printf("\t\t\t\t[2] GBP\t[5] ZMK\n");
printf("\t\t\t\t[3] EUR ");
scanf("%d", &selection);

switch (selection) /* switch statements section */
case '1' : conversion = ARS; break;
case '2' : conversion = GBP; break;
case '3' : conversion = EUR; break;
case '4' : conversion = CHF; break;
case '5' : conversion = ZMK; break;

printf("Enter the amount of foreign currency to be converted ");

newrate = conversion * usd;
printf("That amount in US Dollars is: $ %.2f\n", newrate);
return 0;

This conversation is currently closed to new comments.

Thread display: Collapse - | Expand +

All Comments

Collapse -

by Gary_W In reply to Simple C Programming Ques ...

Take away the single quotes from around your case
conditions. The selection variable you are testing is a number, not a character.
The quotes mean the value tested is a character.

What was happening is no condition was being met, so the conversion variable was never set. This is why you always want to include the "default" condition in your switch, which would have caught the error:

printf("switch value not found");


Collapse -

by tturner33 In reply to

Thanks Gary. It was one of those "can't see for the forest for the trees!" Next week I need to make it more interactive, looping until the user chooses to quit. Thanks again.

Collapse -

by tturner33 In reply to Simple C Programming Ques ...

This question was closed by the author

Related Discussions

Related Forums