473,781 Members | 2,718 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
14 12744
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_Keit h) 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.c om, 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_Keit h) 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
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
3895
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
4270
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
9474
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
10308
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
10143
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...
0
8964
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
7486
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
6729
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
5507
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4040
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
3
2870
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.