473,412 Members | 2,096 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,412 software developers and data experts.

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***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");

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

//check for number greater than zero
while (fResponse<=0.0) {
if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==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\nPress any key to close this window. . .");

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

}

Nov 15 '05 #1
14 12712
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***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");
//get user input
scanf("%f", &fResponse);
//check for number greater than zero
while (fResponse<=0.0) { if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==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\nPress 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***Currency Conversion***\n");
printf("\nConverts Euro into US dollar\n");
printf("\nPlease enter the amount in Euro: ");

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

//check for number greater than zero
while (fResponse<=0.0) {
if (isdigit(fResponse)) {
printf("\nThe number you entered is invald.\n");
printf("Please enter a number larger than zero: ");
scanf("%f", &fResponse);
}
if (isdigit(fResponse)==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\nPress 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(fResponse)) {


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******@hotmail.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("\nConverts Euro into US dollar\n");
printf("\nPlease 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(fResponse)) {
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(fResponse)==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\nPress 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*********@gmail.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
Just starting out wrote:
Flash,

Thanks for your feedback!
Provide context otherwise we can't see what you are replying to. I, for
one, don't bother to remember every single post I make to news groups
and have my reader configured to only show unread posts. Others might
not have even seen my post. Check any post by CBFalconer and read his
sig for instructions on how to work around the crappy interface Google
provide.
I'm using Miracle C compiler for my assignments.
Irrelevant.
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 use
fflush(stdout);
to flush the output. If you don't then the C language does not guarantee
that the message will actually be displayed before the user presses
return when entering data. It's called line buffering.
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).
If I recall correctly I posted comments about the problems with his
code. Read up on how scanf works. Of course, since you have not posted
or quoted the code I can't see to tell you the specific problems.
Our class was told to use getch(); function in order to keep the window
from automatically closing. Maybe it's a Miracle C thing.


It may well be, but it is not part of the C language.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #11
Flash Gordon wrote:
Just starting out wrote:
...
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 use
fflush(stdout);
to flush the output. If you don't then the C language does not
guarantee that the message will actually be displayed before the
user presses return when entering data.


Even using fflush(stdout), the C language won't make that guarantee.
[Try redirecting stdout to an old line buffered printer one day.]
It's called line buffering.


Indeed. Whilst the _stream_ can be flushed, there's no guarantee
that the 'display device' won't have it's own buffering. Using
fflush() will work for 99.999% of consoles, but for the maximum
chance of the prompt appearing, it should end with a newline.

i.e. use...

puts("Enter an integer:");

....over...

printf("Enter an integer: ");
fflush(stdout);
Our class was told to use getch(); function in order to keep the
window from automatically closing. Maybe it's a Miracle C thing.


No, it's largely a windoze thing. Nonetheless, there are better
options,
although that discussion is off-topic in clc.

--
Peter

Nov 15 '05 #12
"Peter Nilsson" <ai***@acay.com.au> writes:
[...]
Indeed. Whilst the _stream_ can be flushed, there's no guarantee
that the 'display device' won't have it's own buffering. Using
fflush() will work for 99.999% of consoles, but for the maximum
chance of the prompt appearing, it should end with a newline.

i.e. use...

puts("Enter an integer:");

...over...

printf("Enter an integer: ");
fflush(stdout);


As much as I'm a fanatic for portable code, I don't agree. Printing a
prompt without a newline makes for a much friendlier interactive
program. On a system where the printf/fflush form doesn't work, I
wouldn't be sure that prompting would work at all.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #13
Barry Schwarz <sc******@deloz.net> writes:
[...]
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.


No, you don't have to do it manually. All you have to do is (all
together now):

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

and it will quote the previous article and fill in proper
attributions.

Google's unforgivable blunder is, first, putting this in a hidden menu
rather than making it the default, and second, not fixing it to this
day.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #14
>> Indeed. Whilst the _stream_ can be flushed, there's no guarantee
that the 'display device' won't have it's own buffering. Using
fflush() will work for 99.999% of consoles, but for the maximum
chance of the prompt appearing, it should end with a newline.

i.e. use...

puts("Enter an integer:");

...over...

printf("Enter an integer: ");
fflush(stdout);


As much as I'm a fanatic for portable code, I don't agree. Printing a
prompt without a newline makes for a much friendlier interactive
program. On a system where the printf/fflush form doesn't work, I
wouldn't be sure that prompting would work at all.


For whatever it's worth, there are hardcopy printers, some with
keyboards, that don't print anything but whole lines. The prompt
wouldn't appear as ink on paper until after you'd answered it. There
are others where the prompt is printed but the printhead is in
the way until the paper has advanced.

Gordon L. Burditt
Nov 15 '05 #15

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

Similar topics

6
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 ...
2
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...
10
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...
2
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...
21
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...
3
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...
7
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,...
1
by: Don Hillgen | last post by:
I need to expand a field in a db2 table, must I write a conversion program???
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...
0
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,...
0
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,...
0
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...
0
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...

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.