Solved

Currency Program

Posted on 2004-08-07
8
516 Views
Last Modified: 2010-04-15
I am new to programming. How would I start to go about expanding the program to accept one input currency, which is error checked as a valid entry and then display its equivalency? This is a C program. Do I use an array or a loop of some kind?

/*Currency Conversion Program created by Rebecca Fusco*/

#include <stdio.h>                       /*header*/
float main(void)                         /*five selected world currency conversions to 1.00 US dollar,float datatype*/

{                                       /*beginning function*/
/*world country selections for currency conversions:Canada,Europe,United Kingdom,Japan,Russia,United States*/                                      
float CAD,EUR,GBP,JPY,RUR,US;                                                

/*assign values to selected countries,reference retrieved from live market
  at http://www.xe.com/effective as of 8/4/04 18:44 GMT*/                                        
CAD=1.31751;
EUR=0.829643;              
GBP=0.548242;
JPY=110.631;
RUR=29.1800;                                                                                  
US=1.00;
                                 
printf("Currency Conversion\n\n");          /*displays title and double space*/
/*declare variables for currency rate from European Euro converted to the United States Dollar*/                                                          
printf("%f European Euro is equivalent to %f US Dollar\n",EUR,US);
/*declare variables for currency rate from United Kingdom Pound to the United States Dollar*/                                                                          
printf("%f United Kingdom Pound is equivalent to %f US Dollar\n",GBP,US);
/*declare variables for currency rate from Japanese Yens to the United States Dollar*/
printf("%f Japanese Yens is equivalent to %f US Dollar\n",JPY,US);  
 /*declare variables for currency rate from Canadian Dollars to the United States Dollar*/
printf("%f Canadian Dollars is equivalent to %f US Dollar\n",CAD,US);
/*declare variables for currency rate from Russian Rubles converted to the United States Dollar*/                                                          
printf("%f Russian Rubles is equivalant to %f US Dollar\n",RUR,US);
 
getch();
return 0;                          
}                                   /*ends*/                                  
                                                 

0
Comment
Question by:Rfusco
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 250 total points
ID: 11744688
assumming the currency sign ALWAYS goes before the number:

<source code removed by sunnycoder, Page Editor>

Now you can invoke this function to separate currency from number, then you can compare currencies sign  with strcmp() to establish currency.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11745086
Hi,

First of all,main() has a signature of int main() and not float main().

To get a value from the user,you can use scanf()
float curFloat;//for a float value
scanf("%f",&curFloat);

or

double curDouble;//for a double value
scanf("%lf",&curDouble);

If you want the user to enter the currency symbol as well,you'll have to get the user input in the form of a string(a char array) and convert it using atof() as jaime explained.

To get the input in the form of string,use fgets().
#define MAX 100
char cur[MAX];
fgets(cur,sizeof(cur),stdin);

You can use scanf() also for getting the user input into a string:
scanf("%s",cur);

But this would take space as a delimiter and end the string the moment it encountered a space.
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11745089
fgets is a better option because it prevents the user from overrunning the amount of space you've reserved for the cur[] string.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11746409
I think the main issue in the homework is not the currency/amount splitting, just a colateral topic, that's why I have posted, so, why to remove it if author has read?, it will not be useful to others members?. Even I have not tested it, I am sure it will need some arrangements (have you tested it?). So if author make some arrangements I encourage her to publish it, because that demonstrates that my answer is not a full solution.
BTW. I think it is not a good idea a page editor have have the level of participation in his own TA. You have covered more than 50% of questions. I have not seen this case in the other dozen TA I participate (Just a personal opinion).


0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11746523
The author have to do that:

-Store a conversion table anywhere
-Present a menu with options
-Ask for user input
-Parse currency symbol (split)   <------ This is my portion, mandatory?
-COMPARE CURRENCY SYMBOL WITH THOSE IN TABLE TO ESTABLISH CURRENCY SELECTED <--- have not supplied this
-Make proper conversion
-Present results

I think my posted code is not a whole homework, and can't run alone with proper managing code (before and after invoking function), even not a half homework, just a portion, maybe it is not a requirement, but this is a speculation from you and me.
Anyway, I think I have not violated MA and have not intention to violate MA. I will not spend time posting CS because that's not meaninful to me, but if you want, I wil discuss it there.
Jaime.
PS. I guess my list above is not a MA violation, but a legitime answer.
 
0
 

Expert Comment

by:akaryu98
ID: 13541926
i looked this up because i need help with two different IT classes ia m taking. i wouldn't be paying money for this membership otherwise. I always ackwoledge any resources copied off the web. there is no damage in that. as long as the rules of resources, validity, and opens source are respected
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question