Writing a Currency Conversion Program in Basic C

Discussion in 'Computer Science & Culture' started by Carrie Robinson, Feb 18, 2003.

Thread Status:
Not open for further replies.
  1. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Re: How would I design a flow chart for this program

    You're getting this error because you changed things a bit. You have used:
    Where I used:
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Here is another version of the programme which you may find easier to deal with. No command line stuff needed, you just run it from the compiler like normal.

    Code:
    #include {stdio.h}
    #include {stdlib.h}
    
    int main(int argc, char *argv[])
    {
    	float inputamount, result;
    	int countrycode;
    
    	printf("\n\n Enter an amount of money: ");
    	scanf("%f", &inputamount);
    
    	printf("\n\n Enter a number for a country, 1 to 5: ");
    	scanf("%d", &countrycode);
    
    	switch(countrycode)
    	{
    		case 1:
    			result = inputamount * 1.5;
    			break;
    		case 2:
    			result = inputamount * 0.3;
    			break;
    		case 3:
    			result = inputamount * 2.3;
    			break;
    		case 4:
    			result = inputamount * 0.4;
    			break;
    		case 5:
    			result = inputamount * 1.1;
    			break;
    		default:
    			result = inputamount * 1.0;
    			break;
    	}
    	printf("\n\n The result is: %f \n\n", result);
    	getchar();
    	return 0;
    }
    
    Remember to change the {curly brackets} up the top there to the triangle ones. Also remember you can change the country codes around a bit, and you can change the conversion numbers there to whatever you want.
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Carrie Robinson Registered Member

    Messages:
    16
    No I have not got the program to run because the compiler gives me the following error message when I compile it.

    I also get this error when I try to compile the program:
    c: line 16: function not declared
    '(inputamount = atoi(argv[1])'
    aborting compile
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Cut and paste the last programme version I posted, exactly as it is. Then change the brackets around the stdio.h and stdlib.h to the triangle brackets. That should run perfectly.

    Which compiler are you using?
     
  8. Carrie Robinson Registered Member

    Messages:
    16
    Adam;

    Thank you for all your help the program is running fine now
     
  9. Carrie Robinson Registered Member

    Messages:
    16
    I need some more help with this problem

    Expand the "Currency Conversion," program to accept one input currency of your choice, which is error checked as a valid entry, and then display its equivalency in US dollars. The conversions are now calculated equations. Insert comments in the program to document the program internally.

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

    int main(int argc, char *argv[])
    {
    float inputamount, result;
    int countrycode;

    printf("\n\n Enter an amount of money: ");
    scanf("%f", &inputamount);

    printf("\n\n Enter a number for a country, 1 to 5: ");
    scanf("%d", &countrycode);

    switch(countrycode)
    {
    case 1:
    result = inputamount * 1.5;
    break;
    case 2:
    result = inputamount * 0.3;
    break;
    case 3:
    result = inputamount * 2.3;
    break;
    case 4:
    result = inputamount * 0.4;
    break;
    case 5:
    result = inputamount * 1.1;
    break;
    default:
    result = inputamount * 1.0;
    break;
    }
    printf("\n\n The result is: %f \n\n", result);
    getchar();
    return 0;
    }
     
  10. Carrie Robinson Registered Member

    Messages:
    16
    Could you tell me why I'm getting the following error when I compile line 29: function not declared
    '{total=atoi(money)*3.160)'
    aborting compile



    /*
    main function: first function called by the operating system
    inputs: none
    outputs: returns 0 if it exits properly
    */
    #include <stdio.h>
    #include <stdiolib.h>
    int main (void){
    int choice;
    int money;
    float total;
    char currency[17];
    /*establish areas in memory for storage data*/

    /*output text*/
    printf("Currency Conversion\n\n");
    printf("1. Argentine Peso 3.160=1 U.S. Dollar\n");
    printf("2. Australian Dollar 1.655=1 U.S. Dollar\n");
    printf("3. Brazilian Real 3.563=1 U.S. Dollar\n");
    printf("4. British Pound .630=1 U.S. Dollar\n");
    printf("5. Canadian Dollar 1.492=1 U.S. Dollar\n");
    /*ask user to decide what kind of currency to convert money to*/
    printf("What would you like to convert your money to?(1-5)");
    scanf("%s",&choice);

    printf("How much money do you want to convert?(US Dollars)");
    scanf("%t",&money);
    if(choice==1)
    {total=atoi(money)*3.160);
    if(choice==1) printf("You will have %u Argentine Pesos! \n" total);}
    if(choice==2)
    {total=atoi(money)*1.655;}
    if(choice==2)
    {printf("You will have %v Australian Dollars! \n" total);}
    if(choice==3)
    {total=atoi(money)*3.563;}
    if(choice==3)
    {printf("You will have %w Brazillian Reals! \n" total);}
    if(choice==4)
    {total=atoi(money)*.630;}
    if(choice==4)
    {printf("You will have %x British Pounds! \n" total);}
    if(choice==5)
    {total=atoi(money)*1.492;}
    if(choice==5)
    {printf("You will have %y Canadian Dollars! \n" total);}

    /*normal program end*/
    return 0;
     
  11. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    You don't have the right include...
    #include <stdlib.h>

    And as a side note. I'm all for helping out here, but heck we might as well send you the binary, you haven't done any of this assignment for yourself. Jeeesh...:bugeye:

    -AntonK
     
  12. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Code:
    /*main function: first function called by the operating system
    inputs: none
    outputs: returns 0 if it exits properly
    */
    #include {stdio.h}
    
    int main (void)
    {
    	int choice;
    	float money;
    	float total;
    	/*establish areas in memory for storage data*/
    
    	/*output text*/
    	printf("\n\nCURRENCY CONVERSION\n");
    	printf("1. Argentine Peso 3.160=1 U.S. Dollar\n");
    	printf("2. Australian Dollar 1.655=1 U.S. Dollar\n");
    	printf("3. Brazilian Real 3.563=1 U.S. Dollar\n");
    	printf("4. British Pound .630=1 U.S. Dollar\n");
    	printf("5. Canadian Dollar 1.492=1 U.S. Dollar\n");
    	printf("Enter the number for the currency to convert...");
    
    	/*ask user to decide what kind of currency to convert money to*/
    	printf("\n\n What would you like to convert your money to? (1-5): ");
    	scanf("%d",&choice);
    
    	printf("\n\n How much money do you want to convert? (US Dollars): ");
    	scanf("%f",&money);
    
    if(choice == 1)
    	{
    		total = money * 3.160;
    		printf("\n\nYou will have %f Argentine Pesos! \n\n", total);
    	}
    if(choice == 2)
    	{
    		total = money * 1.655;
    		printf("\n\nYou will have %f Australian Dollars! \n\n", total);
    	}
    if(choice == 3)
    	{
    		total = money * 3.563;
    		printf("\n\nYou will have %f Brazillian Reals! \n\n",total);
    	}
    if(choice == 4)
    	{
    		total = money * 0.630;
    		printf("\n\nYou will have %f British Pounds! \n\n", total);
    	}
    if(choice == 5)
    	{
    		total = money * 1.492;
    		printf("\n\nYou will have %f Canadian Dollars! \n\n", total);
    	}
    
    	getchar();
    
    	/*normal program end*/
    	return 0;
    }
    
    Okay, here is your programme working fine. Just make sure you change the {curly braces} around the INCLUDE to the triangle brackets.
     
  13. Carrie Robinson Registered Member

    Messages:
    16
    Adam;

    How I would perform a test to verify if the input value was correct?
     
  14. Carrie Robinson Registered Member

    Messages:
    16
    How would I place Else if statements into this program?
    To verify the input information.



    /*main function: first function called by the operating system
    inputs: none
    outputs: returns 0 if it exits properly
    */
    #include {stdio.h}

    int main (void)
    {
    int choice;
    float money;
    float total;
    /*establish areas in memory for storage data*/

    /*output text*/
    printf("\n\nCURRENCY CONVERSION\n");
    printf("1. Argentine Peso 3.160=1 U.S. Dollar\n");
    printf("2. Australian Dollar 1.655=1 U.S. Dollar\n");
    printf("3. Brazilian Real 3.563=1 U.S. Dollar\n");
    printf("4. British Pound .630=1 U.S. Dollar\n");
    printf("5. Canadian Dollar 1.492=1 U.S. Dollar\n");
    printf("Enter the number for the currency to convert...");

    /*ask user to decide what kind of currency to convert money to*/
    printf("\n\n What would you like to convert your money to? (1-5): ");
    scanf("%d",&choice);

    printf("\n\n How much money do you want to convert? (US Dollars): ");
    scanf("%f",&money);

    if(choice == 1)
    {
    total = money * 3.160;
    printf("\n\nYou will have %f Argentine Pesos! \n\n", total);
    }
    if(choice == 2)
    {
    total = money * 1.655;
    printf("\n\nYou will have %f Australian Dollars! \n\n", total);
    }
    if(choice == 3)
    {
    total = money * 3.563;
    printf("\n\nYou will have %f Brazillian Reals! \n\n",total);
    }
    if(choice == 4)
    {
    total = money * 0.630;
    printf("\n\nYou will have %f British Pounds! \n\n", total);
    }
    if(choice == 5)
    {
    total = money * 1.492;
    printf("\n\nYou will have %f Canadian Dollars! \n\n", total);
    }

    getchar();

    /*normal program end*/
    return 0;
    }
    --------------------------------------------------------------------------------
     
  15. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    have you actually written ANY code yourself? You're not even giving us your ideas of how to do any of this. You've simply asked how to write the ENTIRE thing. Try a book or two...or just Google for it... we're all willing to help, but not do it for you.

    Please Register or Log in to view the hidden image!



    -AntonK
     
  16. Carrie Robinson Registered Member

    Messages:
    16
    This is the program I have written and I'm trying to figure out how to make the Else if statements work to check for errors.
    I would appreciate any help.


    /* Cathy Robinson's Expanding on the Currency Program*/

    /* Requirements:Expand the "Currency Conversion," program to accept one input currency of your choice, which is error checked as a valid entry, and then display its equivalency in US dollars. The conversions are now calculated equations. Insert comments in the program to document the program internally.
    Attach a design flow chart .
    Attach a version control sheet
    Attach source code
    Also, copy and paste source code in the body of the message.*/

    /*main function: first function called by the operating system
    inputs: none
    outputs: returns 0 if program runs normal*/

    #include <stdio.h>
    int main (void)
    {
    int country;
    float inputamount;
    float result;
    /* establish areas in memory to storage data for program*/
    /*output data*/
    printf("\n\nCURRENCY CONVERSION\n\n");
    printf("In Japan,the US dollar equivalent is 118.398 Yen\n\n");
    float Yen;/*Japan Yen equivalent*/
    float result; /*US dollar equivalent*/
    printf("What is the amount of your money worth in Japan Yen?");
    printf("Enter the amount in US dollars:"); /*get data from user in US dollars*/
    scanf("%f",&yens); /*Yen rate is 118.398*/
    result = 111.398* Yen; /*US dollars into Japan Yen*/
    printf("What your money is worth $%.2f Yen\n\n\n", result); /*print currency rate for US
    dollars in Japan Yen*/
    }
    printf(" 2 Australian Dollar 1.655 = 1 US Dollar\n");
    printf(" 3 Sweden Kronor .1184 = 1 US Dollar\n");
    printf(" 4 Canada Dollar 1.492 = 1 US Dollar\n");
    printf(" 5 Switzerland Franc 1.36089 = 1 US Dollar\n");

    /* user needs to decide what country Currency they want to convert*/
    /* user needs to enter the country then press enter to select the country*/

    printf("\n\n What Country would like to convert your money to ? (1 - 5):");
    scanf("%d",&country);

    /*user needs to enter to inputamount and then press enter to see the conversion to
    the approiate country*/

    printf("\n\n What amount of money would they like convert? (US Dollars): ");
    scanf("%f",&inputamount);

    printf("\n\n Result of amount convert to US Dollars");
    scanf("%f",result);

    if(country==1){
    result = 118.398 * inputamount;
    printf("\n\nYou will have %f Japan Yen! \n\n", result); }
    /*displays amount of Japan Yen in US Dollar*/



    else if (country==2){
    result = 1.655 * inputamount;
    printf("\n\nYou will have %f Austalian Dollar! \n\n", result);}
    /*displays amount of Austalian Dollar in US dollar*/


    else if(country==3){
    result = .1184 * inputamount;
    printf("\n\nYou will have %f Sweden Krono! \n\n", result);}
    /*displays amount of Krono in US dollar*/


    else if(country==4) {
    result = 1.492 * inputamount;
    printf("\n\nYou will have %f Canada Dollar! \n\n", result);}
    /*displays amount of Canada Dollar in US dollars*/


    else if(country==5) {
    result = 1.36089 * inputamount;
    printf("\n\nYou will have Switzerland Franc! \n\n", result);}
    /*displays amount of Franc in US dollars*/

    /*pause until user press key to exit program*/

    printf("Press enter to end program...\n");
    getchar();
    /*program end normal*/
    return 0;
    }
     
  17. Carrie Robinson Registered Member

    Messages:
    16
    Never mind I figure it out.

    Adam thanks for all your help.
     
Thread Status:
Not open for further replies.

Share This Page