473,769 Members | 6,305 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Currency conversion program

I am very new to C code and I'm having a lot of trouble with a homework
assignment.

This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.

It runs fine if the user enters a number, but if the user enters a
letter it loops.

I have been working on this for 3 hours now, trying different things
left and right. I'm sure it has something to do with the isdigit
function. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.

I am fried, please help!

---------------------------------------

#include <stdio.h>
#include <system.h>
#include <ctype.h>

main ()

{

//set variables
float fUSD, fEUR, fResponse;

//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0;

//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***Cu rrency Conversion***\n ");
printf("\nConve rts Euro into US dollar\n");
printf("\nPleas e enter the amount in Euro: ");

//get user input
scanf("%f", &fResponse);

//check for number greater than zero
while (fResponse<=0.0 ) {
if (isdigit(fRespo nse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fRespo nse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);

}

printf("\nThe value of the Euro you entered converts to $%.2f US
dollars.\n", (fUSD/fEUR) * fResponse);

printf("\n\nPre ss any key to close this window. . .");

//getch function to leave results on screen until any key is chosen
getch ();

}

Nov 15 '05 #1
14 12742
Just starting out wrote:
I am very new to C code and I'm having a lot of trouble with a homework
assignment.
This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.
It runs fine if the user enters a number, but if the user enters a
letter it loops. why it loops almostly is due to the loop conditions, focus on the
conditions to check out what's the key point of the problem by
unfolding your loop statement.
I have been working on this for 3 hours now, trying different things
left and right. I'm sure it has something to do with the isdigit
function. maybe, but, I dont's think it is indispensable. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.
I am fried, please help!

------------------------------*---------

#include <stdio.h>
#include <system.h>
#include <ctype.h>
main () undefined behavior.
int main(void)
{
//set variables
float fUSD, fEUR, fResponse;
//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0; I think it may be better to initialize like the following:
fUSD = 0.0;
fEUR = 0.0;
fResponse = 0.0;
or
fUSD = 0f;
fEUR = 0f;
fResponse = 0f;
//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***C urrency Conversion***\n ");
printf("\nConv erts Euro into US dollar\n");
printf("\nPlea se enter the amount in Euro: ");
//get user input
scanf("%f", &fResponse);
//check for number greater than zero
while (fResponse<=0.0 ) { if (isdigit(fRespo nse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fRespo nse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}

I think the code fragment "get user input" above could be replaced by
this one:
*************** *************** *************** **************
while(scanf("%f ", &fResponse) == 0)
{
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
getchar();
}
*************** *************** *************** ***************
if "scanf("%f" , &fResponse)" get a digital data, it will return the
number of data it gets,and return 0 while it gets nothing.in other
words it gets nothing means the user input the wrong format data.
getchar() will eat the new-line character left by the previous scanf()
before the next loop starting. printf("\nThe value of the Euro you entered converts to
$%.2f USdollars.\n" , (fUSD/fEUR) * fResponse);
printf("\n\nP ress any key to close this window. . .");
//getch function to leave results on screen until any key is chosen
getch ();


}

Nov 15 '05 #2
Just starting out wrote:
I am very new to C code and I'm having a lot of trouble with a homework
assignment.

This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.

It runs fine if the user enters a number, but if the user enters a
letter it loops.

I have been working on this for 3 hours now, trying different things
left and right. I'm sure it has something to do with the isdigit
function. So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.

I am fried, please help!

---------------------------------------

#include <stdio.h>
#include <system.h>
#include <ctype.h>

main ()

{

//set variables
float fUSD, fEUR, fResponse;

//initialize variables
fUSD = 0;
fEUR = 0;
fResponse = 0;

//set values
fUSD = 1.00;
fEUR = .7435;
//print headers for output screen
printf("\n***Cu rrency Conversion***\n ");
printf("\nConve rts Euro into US dollar\n");
printf("\nPleas e enter the amount in Euro: ");

//get user input
scanf("%f", &fResponse);

//check for number greater than zero
while (fResponse<=0.0 ) {
if (isdigit(fRespo nse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fRespo nse)==0)
printf("\nYou entered a letter.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);

}

printf("\nThe value of the Euro you entered converts to $%.2f US
dollars.\n", (fUSD/fEUR) * fResponse);

printf("\n\nPre ss any key to close this window. . .");

//getch function to leave results on screen until any key is chosen
getch ();

}


I would look at the use of the scanf() function. Did anything actually
get assigned to fResponse in scanf()?
Nov 15 '05 #3
Just starting out wrote:
I am very new to C code and I'm having a lot of trouble with
a homework assignment.

This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.

It runs fine if the user enters a number, but if the user enters a
letter it loops.
You need to check whether your scanf() function succeeds or
fails, and take some action if it fails.

(Read the manual for 'scanf' to find out how to do this).

Currently you do no check, so if the user enters a letter then
the program just carries on merrily as if they had entered
whatever the fResponse variable already contained.
//check for number greater than zero
while (fResponse<=0.0 ) {
if (isdigit(fRespo nse)) {


I am not sure if you understand what "isdigit" does. It
operates on a character, and checks to see if that character
is a '0', a '1', a '2' , ..., or a '9'.

fResponse is a float. Checking to see whether it contains
the integer code for a digit character is not very useful.

In fact this test can never succeed, because the ASCII
(I presume) codes for the digits are between 48 and 57, and
you only enter this test if fResponse is 0 or a negative number.

Nov 15 '05 #4
ke******@hotmai l.com wrote:
Just starting out wrote:
I am very new to C code and I'm having a lot of trouble with a homework
assignment .This program is supposed to take the amount of Euros that the user
enters and convert it to US dollars.It runs fine if the user enters a number, but if the user enters a
letter it loops.
why it loops almostly is due to the loop conditions, focus on the
conditions to check out what's the key point of the problem by
unfolding your loop statement.
Actually the problem is with scanf. See Old Wolf's reply.
I have been working on this for 3 hours now, trying different things
left and right. I'm sure it has something to do with the isdigit
function.
maybe, but, I dont's think it is indispensable.


The use of isdigit is definitely a problem. The OP needs to decide
whether to read a character at a time (in which case isdigit is usefule)
or whether to use scanf to read a number in which case the return value
of scanf should be checked.
So, I tried adding in a char cResponse and switching the
value to fResponse after checking for a number.I am fried, please help!
------------------------------*---------
#include <stdio.h>
#include <system.h>
#include <ctype.h>

main ()


undefined behavior.


No. It is perfectly valid (but bad style) on C89 and a constraint
violation on C99 where implicit int has been removed from the language.
int main(void)
That is correct.
{

//set variables
// style comments are only valid in C99, so the OP is not invoking the
compiler as either a proper C99 or a proper C89 compiler but in some
other mode. The OP should check the instructions for the compiler to see
how to make it behave decently.

Also, // comments are not advisable on news groups because they cause
problems when the line wraps.
float fUSD, fEUR, fResponse;

//initialize variables

> fUSD = 0;
You (kernelxu) seem to be inserting spaces at random in front of quote
characters. This makes it harder to read you post, so please avoid doing
this.
> fEUR = 0;
> fResponse = 0;

I think it may be better to initialize like the following:
fUSD = 0.0;
fEUR = 0.0;
fResponse = 0.0;
or
fUSD = 0f;
fEUR = 0f;
fResponse = 0f;


That will make absolutely no difference. The OP's initialisation is
perfectly OK.
> //set values
> fUSD = 1.00;
> fEUR = .7435;

//print headers for output screen
printf("\n*** Currency Conversion***\n ");
printf("\nCon verts Euro into US dollar\n");
printf("\nPle ase enter the amount in Euro: ");

//get user input
scanf("%f", &fResponse);
You probably want the return value of scanf. Check your text book to see
what it does with input that does not match the format specifier.
//check for number greater than zero

> while (fResponse<=0.0 ) {


if (isdigit(fRespo nse)) {
The use of isdigit here is wrong. You use it on characters, not floating
point numbers, please read that part of your text book again as well.
printf("\nThe number you entered is invald.\n");

> printf("Please enter a number larger than zero: ");
You need to end with a new line or flush stdout.
scanf("%f", &fResponse);
See previour comment on scanf.
}

> if (isdigit(fRespo nse)==0)
> printf("\nYou entered a letter.\n");

printf("Please enter a number larger than zero: ");
You need to end with a new line or flush stdout.
> scanf("%f", &fResponse);
See previour comment on scanf.
> }

I think the code fragment "get user input" above could be replaced by
this one:
*************** *************** *************** **************
while(scanf("%f ", &fResponse) == 0)
{
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");


The text of the second printf might not have been displayed. You either
need to flush stdout or finish the line with a new line character.
getchar();
}
This is still wrong.
*************** *************** *************** ***************
if "scanf("%f" , &fResponse)" get a digital data, it will return the
number of data it gets,and return 0 while it gets nothing.in other
words it gets nothing means the user input the wrong format data.
getchar() will eat the new-line character left by the previous scanf()
before the next loop starting.
Did you try running your code and entering more than a single bad
character? scanf stops as soon as ti hits a matching failure, so more
than one character might be left on the input stream.
printf("\nThe value of the Euro you entered converts to
$%.2f US
dollars.\n ", (fUSD/fEUR) * fResponse);

printf("\n\n Press any key to close this window. . .");
Again with the new line or flushing stdout.
//getch function to leave results on screen until any key is chosen
getch ();

C does not have a "getch" function.
}

--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #5
Thank you Flash Gordon for pointing out my fault.
I am a newbie of C too, I love the group very much.
Sometimes I just can't help myself to join the discussion.
Please forgive my imperence. I will pay more attention on learning.

Nov 15 '05 #6
I replaced the "get user input" code fragment with the while statement
you recommended. It's better in that it doesn't loop anymore, but if I
enter more than one letter it prints the two printf statements that
many times. (if i enter stop it prints it 4 times)

I'm looking into how to fix this part now. Thanks for your feedback!

Nov 15 '05 #7
Yes, I've scrapped the whole isdigit function for this program. I have
a better understanding of that function now and realize it is not
usable here.

Thanks for your response!

Nov 15 '05 #8
Flash,

Thanks for your feedback!

I'm using Miracle C compiler for my assignments.

I'm not sure what you mean by ending the printf statement with a new
line or flush stdout. I wanted the user input to appear at the end of
the "Please enter a number: " line. I checked my book for flush stdout
and it looks like I need a new book.

I replaced my original scanf with the "while(scanf... " code that
kernelxu suggested. The two printf lines both print according to the
number of letters the user enters. (ex. user enters STOP, the printf
lines both print 4 times).

Our class was told to use getch(); function in order to keep the window
from automatically closing. Maybe it's a Miracle C thing.

Nov 15 '05 #9
On 5 Sep 2005 11:54:12 -0700, "Just starting out"
<ke*********@gm ail.com> wrote:
Flash,

Thanks for your feedback!

I'm using Miracle C compiler for my assignments.

I'm not sure what you mean by ending the printf statement with a new
line or flush stdout. I wanted the user input to appear at the end of
the "Please enter a number: " line. I checked my book for flush stdout
and it looks like I need a new book.
You have to provide some context. What code are you talking about.
The fact that the google interface is broken means you have to do it
manually.

The function you want is fflush. What book are you using?

I replaced my original scanf with the "while(scanf... " code that
kernelxu suggested. The two printf lines both print according to the
number of letters the user enters. (ex. user enters STOP, the printf
lines both print 4 times).

Our class was told to use getch(); function in order to keep the window
I would be better if you were told to use a standard function like
getchar(). Not everyone has non-standard extensions like getch().
from automatically closing. Maybe it's a Miracle C thing.


No, it's a Windows thing. When main() returns, your window may close
before you can see its contents. Putting a getchar() prior to the
return is intended to insure the window stays open until you hit Enter
signifying you are done looking.
<<Remove the del for email>>
Nov 15 '05 #10

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

Similar topics

6
1702
by: News Guy | last post by:
Hello, Can someone tell me an easy way to convert a number with many trailing digits to a currency format and adding the '$' sign? Thanks. example Convert 34.77389993 to $34.77 Thanks, News Guy
2
574
by: Willing 2 Learn | last post by:
I'm still having trouble getting my program to do arithmetic in cents(keeping all #'s) then convert the answer in a format of dollars & cents. The main program should add, subtract, scalar multiply(by int)& show, have a constructor w/ & w/out arguments. Header file should have private data & all 6 functions from above.Class definition file should implement my ADT class. What I have so far: Main program #include "jahcurrency.h" #include...
10
3893
by: jayender.vs | last post by:
Hello guys, I need to know the Currency conversion code in Javascript Say for example i got 2 textbox .. where i enter a number (singapore doller value) and i provide a button and in the next text box i should get the value in US doller value. Waiting for ur response, Ciao, Jay
2
4628
by: Nissar Ahamed | last post by:
Currency Conversion is a tricky affair. Given the market rates, tt's not the same converting from USD to JPY (116.30) and from EUR to USD (1.3010) due to the conventions used for JPY and EUR. Has anyone got a simple solution that can do the job? I will need the source code if possible. EggHeadCafe.com - .NET Developer Portal of Choice http://www.eggheadcafe.com
21
4267
by: AsheeG87 | last post by:
Hey Everyone~ I'm still a C++ Rookie so please bear with me on this. I'm doing a temperature conversion program with prototype functions. Basicly, I was wondering if some of you would take a look at my code and critique it for me. I'm mostly concerned with how prototype functions work and if I designed them correctly in my code. Your participation would be greatly appreciated! My Code:
3
6399
by: Concem01 | last post by:
Hello, I am a student a UMUC online and am currently taking C programming. I have never done any programming at all and was wondering if someon can help me with my class assignment. the assignment is that I write a C program that displays the title "Currency Conversion," and then write the names of five currencies and their equivalents to the US dollar. The five currencies and their current exchange rate are; 1 Yen = 0.008431 1...
7
3770
by: tararreb | last post by:
#include<stdio.h> /*This line is standard input output, # is directive, include is keyword, and stdio.h is header file*/ #include<stdlib.h> /*This line is standard input output, # is directive, include is keyword, and stdlib.h is standard library defininition*/ #include<math.h> /*This line is standard input output, # is directive, include is keyword, and math.h is mathematical declarations*/ main (void)
1
1826
by: Don Hillgen | last post by:
I need to expand a field in a db2 table, must I write a conversion program???
0
9587
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
9423
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
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10045
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
9993
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
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5298
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...
3
2815
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.