Currency conversion program

  • Thread starter Just starting out
  • Start date
J

Just starting out

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

}
 
K

kernelxu

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.
------------------------------­---------


#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 US
}
 
S

Stan Milam

Just said:
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()?
 
O

Old Wolf

Just said:
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.
 
F

Flash Gordon

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.
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.
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.

// 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.

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.
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.

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.

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.

You need to end with a new line or flush stdout.

See previour comment on scanf.

You need to end with a new line or flush stdout.

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

Again with the new line or flushing stdout.

C does not have a "getch" function.
 
K

kernelxu

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.
 
J

Just starting out

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!
 
J

Just starting out

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!
 
J

Just starting out

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.
 
B

Barry Schwarz

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>>
 
F

Flash Gordon

Just said:
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.
 
P

Peter Nilsson

Flash said:
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);

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

Keith Thompson

Peter Nilsson said:
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.
 
K

Keith Thompson

Barry Schwarz said:
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.
 
G

Gordon Burditt

Indeed. Whilst the _stream_ can be flushed, there's no guarantee
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
 

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
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top