473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

need help with C program

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);
}

Nov 15 '05 #1
1 1954
Dr_Z2A wrote:
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

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Spamtrap | last post by:
I only do occasional Perl programming and most things I write are short processes. I have something I'm working on that is scanning a text file with about 15 million lines and trying to extract...
2
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
by: vgame64 | last post by:
Hi, I have been struggling with writing a program for a few hours. The requirements are that: """You will be writing a program which will determine whether a date is valid in terms of days in that...
4
by: robinsand | last post by:
My apologies to those of you who are more advanced Visual C++ .NET programmers, but I am working on a project for an MBA course that is condensed into an eight-week schedule, and I need help...
16
by: pamelafluente | last post by:
I am still working with no success on that client/server problem. I need your help. I will submit simplified versions of my problem so we can see clearly what is going on. My model: A client...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
4
by: naknak4 | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
6
by: naknak | last post by:
Introduction This assignment requires you to develop solutions to the given problem using several different approaches (which actually involves using three different STL containers). You will...
1
by: peterggmss | last post by:
This is a slot machine game, 2 forms. One is the actual game (frmMachine) and the other is in the background and randomizes the images shown on frmMachine. I need to make frmMachine wait for...
1
by: raghavshastri | last post by:
You are to write a C++ program to perform a statistical analysis of the blobs in an image. The image will be a grayscale image in PGM format for simplicity. Here is a sample PGM image with 10...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.