473,662 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1974
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
3110
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 matches from another text file, which has about 170 entries. The second text file is read into an array. The process then scans through the big file for certain possible patterns - it will find those in about 1 out of 25 lines,, when it finds one,...
2
2866
by: aj902 | last post by:
Hello , I am trying to create a program where all detail, http://www.albany.edu/~csi333/projects.htm
13
2458
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 month. We are assuming that the year will be valid 4 digit integer. So you don't have to think much about that(in terms of validation) except for the month of February. If the month is February, then you have to check whether that year is Leap...
4
2204
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 getting a program up and running with proper files and documentation to be handed in for a grade (on Microsoft Visual Studio .NET 2003). I am being graded on how well I incorporate advanced C++ features such as inheritance, polymorphic programming,...
16
2524
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 uses IE to talk with a server. The user on the client (IE) sees an ASP net page containing a TextBox. He can write some text in this text box and push a submit button.
8
2736
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 the sequence nos and it should be such that i can access it through the structure .plz help me .
4
2192
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 implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
6
2122
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 implement all three techniques as programs. In these programs, as well as solving the problem, you will also measure how long the program takes to run. The programs are worth 80% of the total mark. The final 20% of the marks are awarded for a...
1
5675
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 frmRandom, i tried it with the Sleep API and a Do/Until Loop and neither worked, could you please help me, I have my code below. frmMachine 'Slot Machine, (c) 2006 Peter Browne, GPL Licensed ' 'Peter Browne 'Sheridan Corporate Centre '2155 Leanne...
1
1964
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 columns and 4 rows: P2 # Comment lines # They follow the P2 line. # There could be 0, 1 or more 10 4 255
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8762
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.