need help with C program

D

Dr_Z2A

Ok so a couple months ago I wrote a currency converter and never got it
to compile (my memorization of syntax sucked then) and I just had the
printed out copy lying in a stack of papers. So I found it the other
day and decided to fix it up. I have one problem that I can't figure
out in it still. When I run the program it prompts for the option
number as it is supposed to, but when I enter in the number it outputs
that I have entered an invalid option, so I figure my problem is in the
scanf or switch statement in the main function. Can anyone enlighten
me as to what I have done wrong? Heres the source code:

/* currency changer by Dr. Z2A July 25 2005 */
#include <stdio.h>
/*declare functions */
void euro();
void sfranc();
void pound();
/* main function */
main()
{
int c;

printf("welcome to the Dr. Z2A currency changer\n");
printf("Note that this changer is accurate as of July 25 2005 and may
be inaccurate if used in the future\n");
printf("To calculate dollars to euros press 1\n");
printf("To calculate dollars to swiss francs press 2\n");
printf("To calculate dollars to british pounds press 3\n");
scanf("%d",&c);
/* switch branch */
switch (c) {
case '1':
euro();
case '2':
sfranc();
case '3':
pound();
default:
printf("invalid option\n");
}

printf("Thank you for using the currency changer\n");
return 0;
}
/* other function definitions */
void euro()
{
double d,e; /* d for dollars, e for euro */

printf("Enter the amount of Euros that you would like to convert: ");
scanf("%f",e);
d = e * 0.832185; /* assign value of d */
printf("The value in dollars is %f",d);
}

void sfranc()
{
double d,f;
printf("Enter the amount of Swiss Francs that you would like to
convert: ");
scanf("%f",f);
d = f * 1.29979;
printf("The value in dollars is %f",d);
}

void pound()
{
double d,p;
printf("Enter the amount of British Pounds that you would like to
convert: ");
scanf("%f",p);
d = p * 0.5757;
printf("The value in dollars is %f",d);
}
 
R

Robert Gamble

Dr_Z2A said:
Ok so a couple months ago I wrote a currency converter and never got it
to compile (my memorization of syntax sucked then) and I just had the
printed out copy lying in a stack of papers. So I found it the other
day and decided to fix it up. I have one problem that I can't figure
out in it still. When I run the program it prompts for the option
number as it is supposed to, but when I enter in the number it outputs
that I have entered an invalid option, so I figure my problem is in the
scanf or switch statement in the main function. Can anyone enlighten
me as to what I have done wrong? Heres the source code:

/* currency changer by Dr. Z2A July 25 2005 */
#include <stdio.h>
/*declare functions */
void euro();
void sfranc();
void pound();

should be:
void euro (void);
void sfranc (void);
void pound (void);
/* main function */
main()

should be:
int main (void)
{
int c;

printf("welcome to the Dr. Z2A currency changer\n");
printf("Note that this changer is accurate as of July 25 2005 and may
be inaccurate if used in the future\n");
printf("To calculate dollars to euros press 1\n");
printf("To calculate dollars to swiss francs press 2\n");
printf("To calculate dollars to british pounds press 3\n");
scanf("%d",&c);
/* switch branch */
switch (c) {
case '1':
euro();
case '2':
sfranc();
case '3':
pound();
default:
printf("invalid option\n");
}

Your cases have no break statements and your switch variable is an
integer whose proper values consist of 1, 2, and 3, not '1', '2', and
'3' which represent the integer value of the characters 1, 2, and 3 in
your character set. Rewritten this would look like:

switch (c) {
case 1: euro(); break;
case 2: sfranc(); break;
case 3: pound(); break;
default: printf("invalid option\n");
}
printf("Thank you for using the currency changer\n");
return 0;
}
/* other function definitions */
void euro()

should be:
void euro (void)
{
double d,e; /* d for dollars, e for euro */

printf("Enter the amount of Euros that you would like to convert: ");
scanf("%f",e);

%f expects pointer to float, you are passing double. You want this:
scanf("%lf", &e);
d = e * 0.832185; /* assign value of d */
printf("The value in dollars is %f",d);

You'll want a newline in this printf format string.
}

void sfranc()
{
double d,f;
printf("Enter the amount of Swiss Francs that you would like to
convert: ");
scanf("%f",f);

%f expects pointer to float, you are passing double. You want this:
scanf("%lf", &f);
d = f * 1.29979;
printf("The value in dollars is %f",d);

You'll want a newline in this printf format string.
}

void pound()
{
double d,p;
printf("Enter the amount of British Pounds that you would like to
convert: ");
scanf("%f",p);

%f expects pointer to float, you are passing double. You want this:
scanf("%lf", &p);

d = p * 0.5757;
printf("The value in dollars is %f",d);

You'll want a newline in this printf format string.

Robert Gamble
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top