Jump to content

Basic C Help -- Currency Converter

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
liljp617

liljp617

    Newbie

  • Members
  • Pip
  • 2 posts

Quote

Write a computer program in C which will request the user to enter a number representing the amount of foreign currency he/she would like to exchange. Your program should output a foreign currency conversion table for at least 6 different currencies, showing how much of each type of foreign currency can be obtained for the amount the user enters.

The example below shows a foreign currency exchange table for one unit of foreign currency. Your program is interactive and so should work for any input amount from the user not just 1.00. In the example table, the number entered by the user is 1, and it shows that 1 USD is equal to 0.547525 GBP, while 1 GBP is equal to 2.11928 AUD and so forth. If the user enters 2, your table should show how much 2 USD is in GBP, CAD, EUR, etc. along with how much 2 CAD would be in EUR, GBP, USD, etc, for each of the 6 currencies. The example uses real graphics not ASCII-art so it is ok if your table is not as visually pleasing.


So far this is what I've done:

Quote

/*
Takes currency input from user and outputs currency conversions for USD, GBP, CAD,
EUR, AUD, CYN (Chinese Yuan)
*/

#include <stdio.h>
#include <stdlib.h>

main()
{

/* Declare and Initialize Variables */
const float USD = 1.0000;
const float GBP = 0.5653;
const float CAD = 1.0637;
const float EUR = 0.7006;
const float AUD = 1.2335;
const float CYN = 6.8400;
const float UserInput = '\0';

/* Begin user interaction */
printf("Please input the amount of currency you wish to convert: ");
scanf("%d", &UserInput);
printf("\n");
printf("\n");

printf("|----------------------------------------------------|\n");
printf("| | USD | GBP | CAD | EUR | AUD | CYN |\n");
printf("|----------------------------------------------------|\n");
printf("| USD | | | | | | |\n");
printf("|----------------------------------------------------|\n");
printf("| GBP | | | | | | |\n");
printf("|----------------------------------------------------|\n");
printf("| CAD | | | | | | |\n");
printf("|----------------------------------------------------|\n");
printf("| EUR | | | | | | |\n");
printf("|----------------------------------------------------|\n");
printf("| AUD | | | | | | |\n");
printf("|----------------------------------------------------|\n");
printf("| CYN | | | | | | |\n");
printf("|----------------------------------------------------|\n");

system("PAUSE");
return 0;
}


All the "|" should line up obviously to make it look like a table, but the forum formatted it oddly.

The basic gist of the program is that the user will input a currency amount (in $US) they wish to convert. After they input the currency amount, the program is supposed to output that table with all the conversions. It should output the entire table every time they put in a currency amount to convert. Inside the actual cells, should be a number going two decimal places (I'm aware it's %.2f to get that).

Anyway, I don't even know if I've done everything up to this point correctly. But if I have, my next step (I assume) would be to do the actual conversions. And I don't particularly know how to do that or what to do. Would I do the conversions within each individual cell in the table? How would I do that? Or would I do them somewhere outside of the table and store them as a variable of some sort and then input that variable into the table somehow?

If someone could give an example of how they would do the first row (USD across) or describe how they would do it, that would be great.

Any help/direction/corrections is welcome and very much needed. I know it's not hard, but this is the very first bit of programming I've ever done so...

Thanks in advance for any help at all.

#2
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Interesting program :D If I where doing this I would create 6 arrays one to hold the conversions for USD, GBP, CAD, EUR, AUD, CYN and then loop through the array printing the numbers in a row.

I don't know the conversions but you could use arrays to hold the conversions. Of course you could also just perform the calculation in each cell. That would look a lot like a mess though.

Create an array like this:

double[] aUSD = array(); 

and in the array store all the different conversions.

Hope that makes sense. lol Well I'm off to do this in java lol

#3
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
I'm bored as ****, I'll code it for you since you at least tried to code something. Although I'm coding it in C++, I don't much feel like coding in C right now.

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
I'm sure if you do it in C++, he'll be able to work out the C for it.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Okay I coded it for you. You will more than likely notice that I did away with the table, and that was because I didn't feel like doing the formatting stuff for it. If you still want to do it like that you can do it, it will probably take you like 10 minutes.

Also, I got lazy in one part and just used exit(0) because I didn't feel like using another flag. Once again if you don't like that, you can just change it. You could also probably write a function to be used in my switch statement in case 2, it would make the code shorter and more structured. But, that's on you. Mmm, I think the code should be good to go, but if you have any problems with it then just post them here.

Oh I also gave the option of changing the conversion factors once the program has ensued.

Lol, **** I just realized that the exit(0) s create a memory leak, I'll work on it later if I feel like it.
Enjoy.

#include <iostream>

using namespace std;

struct Conversion_Factors {
       float USD;
       float GBP;
       float CAD;
       float EUR;
       float AUD;
       float CYN;
};

int Set_Factors(Conversion_Factors * c_f);
void Convert_And_Display(int amount, Conversion_Factors * c_f);
float Get_Correct_Format(float int_amount);

int main() {
    int dead = 0;
    int input = 0;
    Conversion_Factors * conv_f = (Conversion_Factors*)calloc(sizeof(conv_f), 1);
    if (conv_f == NULL) {
       cout << "Failed to allocate memory!\nBYE!\n";
       cin >> dead;
       return 0;
    }
    if (Set_Factors(conv_f) == -1) {
       cout << "BYE!\n";
       cin >> dead;
       free(conv_f);
       return 0;
    }
    
    cout << "Enter the amount to convert in USD: ";
    cin >> input;
    
    Convert_And_Display(input, conv_f);
    cin >> dead;
    free (conv_f);
    return 0;
}

int Set_Factors(Conversion_Factors * c_f) {
     int choice = 0;
     float conv_entry = 0;
     cout << "Enter 1 to use the standard conversion factors, and 2 to set your own.\n>";
     cin >> choice;
     
     switch (choice) {
            case 1: {
                 c_f -> USD = 1;
                 c_f -> GBP = .5653;
                 c_f -> CAD = 1.0637;
                 c_f -> EUR = 0.7006;
                 c_f -> AUD = 1.2335;
                 c_f -> CYN = 6.8400;
                 break;
            }
            case 2: {
                 cout << "USD = ";
                 cin >> conv_entry;
                 c_f -> USD = conv_entry;
                 conv_entry = 0;
                 
                 cout << "GBP = ";
                 cin >> conv_entry;
                 c_f -> GBP = conv_entry;
                 conv_entry = 0;
                 
                 cout << "CAD = ";
                 cin >> conv_entry;
                 c_f -> CAD = conv_entry;
                 conv_entry = 0;
                 
                 cout << "EUR = ";
                 cin >> conv_entry;
                 c_f -> EUR = conv_entry;
                 conv_entry = 0;
                 
                 cout << "AUD = ";
                 cin >> conv_entry;
                 c_f -> AUD = conv_entry;
                 conv_entry = 0;
                 
                 cout << "SYN = ";
                 cin >> conv_entry;
                 c_f -> CYN = conv_entry;
                 conv_entry = 0;
                 
                 break;
            }
            default: {
                     cout << "Your answer sucked!\n";
                     return -1;
            }
     }
     cout << "Thank you!\n";
     return 0;
}

void Convert_And_Display(int amount, Conversion_Factors * c_f) {
     cout << endl << endl;
     
     
     float USD_amount = (amount * c_f->USD);
     USD_amount = Get_Correct_Format(USD_amount);
     float GBP_amount = (amount * c_f->GBP);
     GBP_amount = Get_Correct_Format(GBP_amount);
     float CAD_amount = (amount * c_f->CAD);
     CAD_amount = Get_Correct_Format(CAD_amount);
     float EUR_amount = (amount * c_f->EUR);
     EUR_amount = Get_Correct_Format(EUR_amount);
     float AUD_amount = (amount * c_f->AUD);
     AUD_amount = Get_Correct_Format(AUD_amount);
     float CYN_amount = (amount * c_f->CYN);
     CYN_amount = Get_Correct_Format(CYN_amount);
     
    
     cout << "USD conversion: " << USD_amount << endl;
     cout << "GBP conversion: " << GBP_amount << endl;
     cout << "CAD conversion: " << CAD_amount << endl;
     cout << "EUR conversion: " << EUR_amount << endl;
     cout << "AUD conversion: " << AUD_amount << endl;
     cout << "CYN conversion: " << CYN_amount << endl;
     return;
}
float Get_Correct_Format(float int_amount) {
      float new_amount;
      int dead = 0;
      int i = 0;
      int str_l = 0;
      char * stand_in = (char*)calloc(sizeof(char), 20);
      if (stand_in == NULL) {
        cout << "Memory not allocated!\nBYE!\n";
        cin >> dead;
        exit(0);
      }
      char * new_stand_in = (char*)calloc(sizeof(char), 20);
      if (new_stand_in == NULL) {
        cout << "Memory not allocated!\nBYE!\n";
        cin >> dead;
        free(stand_in);
        exit(0);
      }
      sprintf(stand_in, "%f", int_amount);
      str_l = strlen(stand_in);
      for (i = 0; i < str_l; i++) {
          new_stand_in[i] = stand_in[i];
          if (stand_in[i] == '.') {
             new_stand_in[i + 1] = stand_in[i + 1];
             new_stand_in[i + 2] = stand_in[i + 2];
             break;
          }
      }

      sscanf(new_stand_in, "%f", &new_amount);
      free (stand_in);
      free (new_stand_in);
      return new_amount;
}


#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Wow, you really are bored!
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#7
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Lol, yeah. I think I'll get back to one of my projects.

#8
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
I did an ugly fix for the memory leak. It works logically, but it's ugly.

#include <iostream>

using namespace std;

struct Conversion_Factors {
       float USD;
       float GBP;
       float CAD;
       float EUR;
       float AUD;
       float CYN;
};

int Set_Factors(Conversion_Factors * c_f);
void Convert_And_Display(int amount, Conversion_Factors * c_f);
float Get_Correct_Format(float int_amount);

int main() {
    int dead = 0;
    int input = 0;
    Conversion_Factors * conv_f = (Conversion_Factors*)calloc(sizeof(conv_f), 1);
    if (conv_f == NULL) {
       cout << "Failed to allocate memory!\nBYE!\n";
       cin >> dead;
       return 0;
    }
    if (Set_Factors(conv_f) == -1) {
       cout << "BYE!\n";
       cin >> dead;
       return 0;
    }
    
    cout << "Enter the amount to convert in USD: ";
    cin >> input;
    
    Convert_And_Display(input, conv_f);
    cin >> dead;
    free (conv_f);
    return 0;
}

int Set_Factors(Conversion_Factors * c_f) {
     int choice = 0;
     float conv_entry = 0;
     cout << "Enter 1 to use the standard conversion factors, and 2 to set your own.\n>";
     cin >> choice;
     
     switch (choice) {
            case 1: {
                 c_f -> USD = 1;
                 c_f -> GBP = .5653;
                 c_f -> CAD = 1.0637;
                 c_f -> EUR = 0.7006;
                 c_f -> AUD = 1.2335;
                 c_f -> CYN = 6.8400;
                 break;
            }
            case 2: {
                 cout << "USD = ";
                 cin >> conv_entry;
                 c_f -> USD = conv_entry;
                 conv_entry = 0;
                 
                 cout << "GBP = ";
                 cin >> conv_entry;
                 c_f -> GBP = conv_entry;
                 conv_entry = 0;
                 
                 cout << "CAD = ";
                 cin >> conv_entry;
                 c_f -> CAD = conv_entry;
                 conv_entry = 0;
                 
                 cout << "EUR = ";
                 cin >> conv_entry;
                 c_f -> EUR = conv_entry;
                 conv_entry = 0;
                 
                 cout << "AUD = ";
                 cin >> conv_entry;
                 c_f -> AUD = conv_entry;
                 conv_entry = 0;
                 
                 cout << "SYN = ";
                 cin >> conv_entry;
                 c_f -> CYN = conv_entry;
                 conv_entry = 0;
                 
                 break;
            }
            default: {
                     cout << "Your answer sucked!\n";
                     return -1;
            }
     }
     cout << "Thank you!\n";
     return 0;
}

void Convert_And_Display(int amount, Conversion_Factors * c_f) {
     cout << endl << endl;
     
     
     float USD_amount = (amount * c_f->USD);
     USD_amount = Get_Correct_Format(USD_amount);
     if (USD_amount == 1.000009) {
        return;
     }
     
     float GBP_amount = (amount * c_f->GBP);
     GBP_amount = Get_Correct_Format(GBP_amount);
     if (GBP_amount == 1.000009) {
        return;
     }
     
     float CAD_amount = (amount * c_f->CAD);
     CAD_amount = Get_Correct_Format(CAD_amount);
     if (CAD_amount == 1.000009) {
        return;
     }
     
     float EUR_amount = (amount * c_f->EUR);
     EUR_amount = Get_Correct_Format(EUR_amount);
     if (CAD_amount == 1.000009) {
        return;
     }
     
     float AUD_amount = (amount * c_f->AUD);
     AUD_amount = Get_Correct_Format(AUD_amount);
     if (AUD_amount == 1.000009) {
        return;
     }
     
     float CYN_amount = (amount * c_f->CYN);
     CYN_amount = Get_Correct_Format(CYN_amount);
     if (CYN_amount == 1.000009) {
        return;
     }
    
     cout << "USD conversion: " << USD_amount << endl;
     cout << "GBP conversion: " << GBP_amount << endl;
     cout << "CAD conversion: " << CAD_amount << endl;
     cout << "EUR conversion: " << EUR_amount << endl;
     cout << "AUD conversion: " << AUD_amount << endl;
     cout << "CYN conversion: " << CYN_amount << endl;
     return;
}
float Get_Correct_Format(float int_amount) {
      float new_amount;
      int dead = 0;
      int i = 0;
      int str_l = 0;
      char * stand_in = (char*)calloc(sizeof(char), 20);
      if (stand_in == NULL) {
        cout << "Memory not allocated!\nBYE!\n";
        cin >> dead;
        return 1.000009;
      }
      char * new_stand_in = (char*)calloc(sizeof(char), 20);
      if (new_stand_in == NULL) {
        cout << "Memory not allocated!\nBYE!\n";
        cin >> dead;
        free(stand_in);
        return 1.000009;
      }
      sprintf(stand_in, "%f", int_amount);
      str_l = strlen(stand_in);
      for (i = 0; i < str_l; i++) {
          new_stand_in[i] = stand_in[i];
          if (stand_in[i] == '.') {
             new_stand_in[i + 1] = stand_in[i + 1];
             new_stand_in[i + 2] = stand_in[i + 2];
             break;
          }
      }

      sscanf(new_stand_in, "%f", &new_amount);
      free (stand_in);
      free (new_stand_in);
      return new_amount;
}


#9
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
All that code for something so simple :eek: You must have been bored lol

#10
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

MeTh0Dz|Reb0rn said:

Lol, yeah. I think I'll get back to one of my projects.
How about developing that CC IDE?
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#11
MeTh0Dz

MeTh0Dz

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,119 posts
Mmm, just 182 lines. And it could probably be brought down to 140 or 150 if he wrote 2 functions. One two check for no memory allocation in the Get_Correct_Format() function and one to get data to fill the Conversion_Factor structure. I also used dynamic memory allocation which leads to more error checking, and gave the option to edit the structure with the conversion factors. I increased the reuseability of his code, by a lot.

#12
liljp617

liljp617

    Newbie

  • Members
  • Pip
  • 2 posts
I really really really appreciate the effort and time you put into that. However, I suppose I should have mentioned this is my second week of computer science classes, so I'm not exactly sure if my professor would be fond of me bringing in a code weeks ahead of what we're supposed to know (especially since this is my start at programming and it's obvious I don't know anything as extensive as that yet).