Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-30-2004, 04:23 PM   #1 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Question brand new to this

I am brand new to this language and I am trying to write a program that I am having a lot of problems with....can I get help - please!!

I need to write a C program that displays a title, "Currency Conversion," and then write the names of five currencies and their equivalents to ONE (1.0) US dollar. The conversions are hard coded equations. Insert comments in the program to document the program internally.

The following are the five currencies and their equivalents:
Euro 0.804645 EUR
Canadian 1.26066 CAD
Francs 5.27800 FRF
Japanese Yen 109.930 JPY
Italy Lire 1,558.09 ITL
julia is offline   Reply With Quote
Old 09-30-2004, 04:27 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,529
sde is on a distinguished road
you'll probably get a better response if you posted what you have so far .. or be specific about what you are having problems with .. if it is multiple things, then just start on one at a time.

i don't know c well, but others here do. good luck.

welcome to the boards.
__________________
Mike
sde is offline   Reply With Quote
Old 09-30-2004, 04:30 PM   #3 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Thank you - I will do that....
julia is offline   Reply With Quote
Old 09-30-2004, 04:34 PM   #4 (permalink)
keystoneman
Registered User
 
Join Date: Sep 2004
Location: Lancaster, Pa
Posts: 24
keystoneman is on a distinguished road
Send a message via AIM to keystoneman
Are you outputting every countries currency for the equivolent to one US dollar, or does the user enter how many US dollars and the program outputs the transition into the other currencys?
keystoneman is offline   Reply With Quote
Old 09-30-2004, 04:38 PM   #5 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Smile

No - that is hardcoded by me in whatever currency I choose. If you look at the original problem - I posted 5 of them.

thank you for the reply.....
julia is offline   Reply With Quote
Old 09-30-2004, 05:12 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Suppose I do it in C++, you'll be able to translate it in C easely if C is a *must*.
Here is one example:

Code:
double USAtoEuro(double usa_amount)
{
    return usa_amount * 0.08645l
}
Is this what you mean?
__________________
Valmont is offline   Reply With Quote
Old 09-30-2004, 05:34 PM   #7 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Unhappy

Valmont,

I am assuming that in the original post when it states "the conversions are hard coded equations" then what you have coded is probably the right idea but the curriences I have posted there are already equal to 1 US Dollar each so I guess what I need to do is figure out the equation in order to do the conversion - wouldn't you think so??? To you this is probably very very simple but I have only been into this for one week so far. So you probably need to think simple..
This is an example of what I might see as the output:

Currency Conversion

Euro 0.804645 EUR = 1 US Dollar
Canadian 1.26066 CAD = 1 US Dollar
Francs 5.27800 FRF = 1 US Dollar
Japanese Yen 109.930 JPY = 1 US Dollar
Italy Lire 1,558.09 ITL = 1 US Dollar
julia is offline   Reply With Quote
Old 09-30-2004, 06:42 PM   #8 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Quote:
I am assuming that in the original post when it states "the conversions are hard coded equations" then what you have coded is probably the right idea...
Beats me. I've never figured out what "hard coding" is.

I am still not sure what you want. But then again my native language isn't english. However, is this what you need:
Code:
#include <iostream>

//Tell system to use standard C++ only global file level.
using namespace std;
int main()
{
   //Add your own comments here
   cout<<"Currency Conversion"<<endl;
   cout<<"Euro 0.804645 EUR = 1 US Dollar\n";
   cout<<"Canadian 1.26066 CAD = 1 US Dollar\n"
   cout<<"Francs 5.27800 FRF = 1 US Dollar\n"
   cout<<"Japanese Yen 109.930 JPY = 1 US Dollar\n"
   cout<<"Italy Lire 1,558.09 ITL = 1 US Dollar\n"
   
   return 0;
}
Quote:
need to write a C program that displays a title, "Currency Conversion," and then write the names of five currencies and their equivalents to ONE (1.0) US dollar.
Quote:
...so I guess what I need to do is figure out the equation in order to do the conversion - wouldn't you think so???
You're one question mark away of convincing me. But my anwer is "no".

Eqations huh? Another try: Is this what you mean?
1 EUR = 1/0.804645 = 1.24278 USD
1 CAD = 1/1.26066 = 0.79323 USD
1 FRF = 1/5.278 = 0.18947 USD
1 JPY = 1/109.930 = 0.0090967 USD
1 ITL = 1/1558.09 = 0.000642 USD
__________________
Valmont is offline   Reply With Quote
Old 10-01-2004, 05:26 AM   #9 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Valmont,

I thank you very very much for your help. I am going to ask for an exact explaination of what is needed and then take it from there. If I need any help with it - I will surely post it!!

Thank you again.

Julia
julia is offline   Reply With Quote
Old 10-01-2004, 11:00 AM   #10 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Valmont,

I am back with a better understanding of what I need. Okay - here is the beginning code that I have:

#include<stdio.h>
int main(void)
{
printf("Currency Conversion.\n");
printf("\n\n");
printf("Conversion rates as of Sept. 30, 2004.\n");

Now here is what I don't know how to code. I have to hardcode the equations of the conversion rates - so

Euro (EUR) Euro (the exchange rate is)1.2422 divided by /1 US Dollar = 0.8050
Canada (CAD) Canadian (rate is) 0.7912 / 1 US dollar = 1.2634
China (CNY) Chinese Renminbi (rate) 0.1208 / 1 US dollar = 8.2781
Japan (JPY) Japanese Yen (rate) 0.0091 / 1 US dollar = 110.180
Sweden (SEK) Swedish Krona (rate) 0.1373 / 1 US dollar = 7.2833

Does this make better sense?? I want to make it look in order though when it displays on the screen, so what do you think?
julia is offline   Reply With Quote
Old 10-01-2004, 04:00 PM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Lets wait until a C coder shows up. I am not one of them.
__________________
Valmont is offline   Reply With Quote
Old 10-02-2004, 12:24 PM   #12 (permalink)
julia
Registered User
 
Join Date: Sep 2004
Posts: 9
julia is on a distinguished road
Wink

Valmont,

That is fine with me --- I really appreciate you trying to help anyway - thank you.
julia is offline   Reply With Quote
Old 10-03-2004, 01:33 AM   #13 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
I'll give it a shot..
In about two hours I should have an idear... Once I figure it out
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-03-2004, 04:20 AM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
And here it is...

A small program (err.. 260 lines of code...)
It will work as a converter between different currencys, the main part to handle this is a code snipit like this:
Code:
  if(from_currency != US_CURR)
    {
      /* we need to calculate the from_ammount to USD, 
	 then back to to_ammount */
      to_ammount = US_CURR/from_currency*from_ammount;
      goto calculate_me;
    }
  else
    {
      to_ammount = from_ammount;
    calculate_me:
      /* Just calculate to_currency from the from_ammount*/
      to_ammount *= to_currency;
    }
Else the program will work as this:
Quote:
To convert CAD to DKK:
~> program -f c -t d
To convert USD to DKK:
~> program -t d
To convert CAD to USD:
~> program -f c
To list known currencys:
~> program -l
To print help info:
~> program -h
Here is the program:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* how many numbers can there be in a desired ammount ? */
#define BUFF_LEN 80

/* what rate ONE (1) USD is worth in given currency */
const float CA_CURR =    1.263400;
const float CN_CURR =    8.278100; 
const float DK_CURR =    6.140000;
const float EU_CURR =    0.804645;
const float FR_CURR =    5.278000;
const float IT_CURR = 1558.090000;
const float JP_CURR =  110.180000;
const float SE_CURR =    7.283300;
const float US_CURR =    1.000000;


int help(char*);
int currency(void);
float accept_currency(char);
char* get_ilc(float, char*);


int main(int argc, char** argv)
{
  float from_currency = US_CURR, to_currency = US_CURR;
  float from_ammount, to_ammount;
  int opt; /* counter in given arguments to program */
  /* the International Language Code for the currency */
  char *from_ilc, *to_ilc;
  char buffer[BUFF_LEN];
  /* make sure, theres atleast one usefull argument */
  if(argc <= 1)
    {
      goto help_info;
    }
  /* 
   * searching through the arguments to find our options 
   * skipping first argument, which is the program name 
   */
  for(opt = 1; opt < argc; opt++)
    {
      /* cirkle through them all, 
	  to find the accepted argument */
      switch(argv[opt][0])
	{
	  /* just list the current currency */
	case 'l':
	  goto curr_list;
	  /* we're dealing with from currency */ 
	case 'f':
	  goto from_curr;
	  /* and to currency */
	case 't':
	  goto to_curr;
	  /* or our usual help info */
	case 'h':
	  goto help_info;
	case '-':
	  /* look for the 'f', 't' or 'h' argument again */
	  switch  (argv[opt][1])
	    {
	      /* Just list the currency */
	    case 'l':
	    curr_list:
	      currency();
	      return 0;
	      /* dealing with the from currency delimiter */
	    case 'f':
	    from_curr:
	      /* 
	       * if the next char isn't NULL
	       * we must assume it is the currency
	       * else we must assume the next arguments 
	       * first char is the currency
	       */
	      if(argv[opt][2] != '\0')
		from_currency = accept_currency(argv[opt][2]);
	      else
		from_currency =  accept_currency(argv[++opt][0]); 
	      if (!from_currency)
		{
		  printf("\n[ERROR] Unable to determain FROM currency.\n");
		  goto help_info;
		}
	      goto out;
	      /* dealing with the to currency delimiter */
	    case 't':
	    to_curr:
	      if(argv[opt][2] != '\0')
		to_currency = accept_currency(argv[opt][2]);
	      else
		to_currency =  accept_currency(argv[++opt][0]);
	      if(!to_currency)
		{
		  printf("\n[ERROR] Unable to determain TO currency.\n");
		  goto help_info;
		}
	      goto out;

	    default:
	      goto help_info;
	    }
	default:
	help_info:
	  help(argv[0]);
	  return -1;
	out:
	  break;
	}
    }
  /* start by printing the current currency listings */
  currency();
  /* if we want to convert from the same currency 
   * as we would like to go to, theres no need to 
   * look any further
   */
  if(from_currency == to_currency)
    {
      printf("\nNo need to calculate, what ever you want you've got..\n");
      return 1;
    }
  /* decide the International Language Codes for the currencys used in this run */
  from_ilc = get_ilc(from_currency, from_ilc);
  to_ilc = get_ilc(to_currency, to_ilc);
  /* read in the ammount of from currency */
  printf("Please give the ammount of %s you want to exchange into %s\n\t%s: ",
		from_ilc, to_ilc, from_ilc);
  if(!fgets(buffer, BUFF_LEN, stdin))
    printf("[ERROR] Reading from stdin.\n");
  if(!sscanf(buffer, "%f", &from_ammount))
    printf("[ERROR] Converting input to float.\n");
  /* do all the calculations */
  if(from_currency != US_CURR)
    {
      /* we need to calculate the from_ammount to USD, 
	 then back to to_ammount */
      to_ammount = US_CURR/from_currency*from_ammount;
      goto calculate_me;
    }
  else
    {
      to_ammount = from_ammount;
    calculate_me:
      /* Just calculate to_currency from the from_ammount*/
      to_ammount *= to_currency;
    }
  printf("Your desired ammount: %f %s is worth %f %s\n", 
	 from_ammount, from_ilc, to_ammount, to_ilc);
  free(from_ilc);
  free(to_ilc);
  return 0;
}

int currency(void)
{
  /* print an overview of, what currency we have here */
  printf("\t Currency conversion.\nConversion rates as of " __DATE__ "\n");
  printf("At this moment the currency is as follows:\n");
  printf("[c] CAD %f\t=\t%f USD\n", CA_CURR, US_CURR);
  printf("[C] CNY %f\t=\t%f USD\n", CN_CURR, US_CURR);
  printf("[d] DKK %f\t=\t%f USD\n", DK_CURR, US_CURR);
  printf("[e] EUR %f\t=\t%f USD\n", EU_CURR, US_CURR);
  printf("[f] FRF %f\t=\t%f USD\n", FR_CURR, US_CURR);
  printf("[i] ITL %f\t=\t%f USD\n", IT_CURR, US_CURR);
  printf("[j] JPY %f\t=\t%f USD\n", JP_CURR, US_CURR);
  printf("[s] SEK %f\t=\t%f USD\n", SE_CURR, US_CURR);
  printf("[u] USD %f\t=\t%f USD\n", US_CURR, US_CURR);
  return 0;
}


int help(char* name)
{
  printf("\nUsage: %s [-f from_currency] [-t to_currency] [-l] [-h]\n\n", name);
  printf(" <-f>:\n");
  printf("   Selecting the FROM currency, meaning the currency you want\n");
  printf("   to change from.\n");
  printf(" <-t>:\n");
  printf("   Selecting the TO currency, meaning the currency you want\n");
  printf("   to change to.\n");
  printf(" <-l>:\n");
  printf("   List the current currency available in the program\n");
  printf("   The charactor given in the square brackets is the desired\n");
  printf("   argument parsed to (-f/t) in selecting this currency\n");
  printf(" <-h>:\n");
  printf("   Print this help info.\n\n");
  printf("EXAMPLES:\n");
  printf("\t %s -f f -t e\n", name);
  printf("Will assume your converting Francs into Euro\n");
  printf("\t %s -t e\n", name);
  printf("Will assume your converting USD into Euro\n\n");
  return 0;
}

float accept_currency(char ch)
{
  switch(ch)
    {
    case 'c':
      return CA_CURR;
    case 'C':
      return CN_CURR;
    case 'd':
      return DK_CURR;
    case 'e':
      return EU_CURR;
    case 'f':
      return FR_CURR;
    case 'i':
      return IT_CURR;
    case 'j':
      return JP_CURR;
    case 's':
      return SE_CURR;
    case 'u':
      return US_CURR;
    }
  return 0.0;
}

char* get_ilc(float curr, char *buff)
{
  buff = (char*) malloc(4*sizeof(char));
  /* since switch can't take float, 
     we must do this awfull thing */
  if(curr == CA_CURR)
    strcpy(buff, "CAD");
  else
    if(curr ==  CN_CURR)
      strcpy(buff, "CNY");
    else
      if(curr == DK_CURR)
	strcpy(buff, "DKK");
      else
	if(curr == EU_CURR)
	  strcpy(buff, "EUR");
	else
	  if(curr == FR_CURR)
	    strcpy(buff, "FRF");
	  else
	    if(curr == IT_CURR)
	      strcpy(buff, "ITL");
	    else
	      if(curr == JP_CURR)
		strcpy(buff, "JPY");
	      else
		if(curr == SE_CURR)
		  strcpy(buff, "SEK");
		else
		  if(curr == US_CURR)
		    strcpy(buff, "USD");
		  else
		    strcpy(buff, "NAN");
  return buff;
}
It took a wee bit longer than the predicted two hours.. But I was interrupted by my boss several times.
If you want to fetch it as one file: http://redhead.dk/download/pub/stuff/currency.c
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 10-03-2004 at 07:23 AM.
redhead is offline   Reply With Quote
Old 10-03-2004, 05:28 AM   #15 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Haha, neat console proggie. I think I am going to sell it to Wallstreet

Julia, wich part is according to you "hardcoding" ? So that I know next time when people mention it.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 08:06 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting