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. Carrie Robinson Registered Member

    Messages:
    16
    I'm need some help in writing a Currency Conversion Program in Basic C.

    I have started the program but I don't know how to list to countries into the program?

    Here is what I have so far

    main function: first function called by the operating system
    inputs: five different currenices
    and their equivalent to the US dollar
    output: return "list the equivalents"

    #include <stdio.h>
    int main(void)
    {
    printf("\nCurrency Conversion n\n");

    return 0;

    }
     
    Last edited: Feb 18, 2003
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. chroot Crackpot killer Registered Senior Member

    Messages:
    2,350
    Carrie, I'd be glad to help you, but I'm afraid you haven't given us enough information to understand your problem.

    Can you please provide the "problem statement" that defines exactly what you program is supposed to do?

    - Warren
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. Carrie Robinson Registered Member

    Messages:
    16
    Write a C program that displays a title, "Currency Conversion," and then write the names of five currencies and their equivalents to the US dollar. The conversions are hard coded equations. Insert comments in the program to document the program internally. Attach a design flow chart and a version control sheet to a hard copy of the source code of the program.
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. chroot Crackpot killer Registered Senior Member

    Messages:
    2,350
    Okay, so the meat of the program is something like the following:

    printf("1 US dollar = 1.3 Euro\n");
    printf("1 US dollar = <blah> Canadian dollar\n");

    and so on -- with the right numbers, of course.

    - Warren
     
  8. Carrie Robinson Registered Member

    Messages:
    16
    that is correct
     
  9. chroot Crackpot killer Registered Senior Member

    Messages:
    2,350
    So, uh.. what's your question, Carrie?

    - Warren
     
  10. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    #include <stdio.h>

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

    printf("\n\n Enter amount to be converted");
    printf("\n in the form 2.22 AU, where the");
    printf("\n where the two letters represent");
    printf("\n a country.");
    printf("\n\n Enter amount to convert: ");

    switch(inputamount)
    {
    case "au":
    result = inputamount * 1.5;
    break;
    case "ro":
    result = input amount * 0.3;
    break;
    /* Add in as many possibilities as you like */
    default:
    result = inputamount * 1.0;
    break;
    }
    printf("\n\n In our currency that is: %f", result);
    getch();
    return 0;
    }

    You may wish to add a "tolower" function above the switch and make all the country codes lower case, or upper, whichever you prefer. Oh, also, I forgot to assign the variables to each input.

    Just thought I'd space it out a bit.
     
    Last edited: Feb 19, 2003
  11. chroot Crackpot killer Registered Senior Member

    Messages:
    2,350
    Adam,

    The spec she provided for her program made no mention of any input or calculations to be performed. It said it should "write the names of five currencies and their equivalents to the US dollar."

    A program with five printfs meets this specification.

    Perhaps the spec is not well-formed -- and that would fully be the teacher's fault -- but it doesn't say anything about choosing countries, getting input, and calculating anything.

    - Warren
     
  12. Carrie Robinson Registered Member

    Messages:
    16
    Thanks for the help. I really appreicate.
     
  13. Carrie Robinson Registered Member

    Messages:
    16
    /* Write a beginning of a C program that displays "Currency Conversion" and then write
    the names of five currencies and their equivalents to US dollars. We need to be prepared
    to demonstrate our program.
    */

    #include

    int main (int argr, char *argv[])
    {
    float inputamount;

    printf("n\n Enter amount to be converted ");
    printf("n\n the form 2.22 AU, where the ");
    printf("\nwhere the two letters represent");
    printf("n\nEnter amount to convert: ");
    }
    (inputamount);
    {
    case "au":
    (result = input amount*1.5);
    break;
    case "ro":
    (result = input amount*0.3);
    break;
    case "cad":
    (result = input amount*.062818);
    break;
    case "eur":
    (result = input amount*0.8713);
    break;
    /* Add in as many possibilities as you like*/
    default:
    result = inputamount * 1.0;
    break;

    printf("\n\nIn our currency that is: %f",result);
    return 0;
    }

    This is what I typed in but I get an error line 14: function not declared '("n\nEnter amount to converted")

    Please let me know what I did wrong.
     
  14. mouse can't sing, can't dance Registered Senior Member

    Messages:
    671
    switch(inputamount) instead of (inputamount)

    although i don't have c compiler here to check it.
     
  15. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    I wrote mine in a hurry on here, no compiler. I'm doing it properly now...
     
  16. mouse can't sing, can't dance Registered Senior Member

    Messages:
    671
    a scanf somewhere in the code would also help, or catching the arguments from the argv for input.
     
  17. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    	float inputamount, result;
    	int countrycode;
    
    	inputamount = atoi(argv[1]);
    	countrycode = atoi(argv[2]);
    
    	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 * 3.3;
    		break;
    	case 5:
    		result = inputamount * 0.4;
    		break;
    	default:
    		result = inputamount * 1.0;
    		break;
    	}
    	return result;
    }
    
    Of course this is using it from the command line, no help or instructions. Like you start a little DOS window and type "programme-name amount-of-money number-representing-a-country", separated by spaces. For example you might type "cc 250.50 1" where the last argument means Ausrtalia.
     
  18. Carrie Robinson Registered Member

    Messages:
    16
    /*Cathy Robinson Currency Conversion Program*/

    /* Write a beginning of a C program that displays "Currency Conversion" and then write
    the names of five currencies and their equivalents to US dollars. We need to be prepared
    to demonstrate our program.
    */

    #include


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

    inputamount = atoi(argv[1]);
    countrycode = atoi(argv[2]);

    switch(countrycode)
    {
    case 1:
    result = inputamout * 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.0;
    }
    return result;
    when I compile the program, this is what error message is returnedMiracle C Compiler (r3.2), written by bts.
    line 8: #include ignored--bad delimiter.
    Compiling c:\program files\miracle c\cathy robinson currency program(1).c
    main

    c:\program files\miracle c\cathy robinson currency program(1).c: line 16: function not declared
    '= atoi(argv[1])'
    aborting compile

    Please let me know what I have done wrong?
     
  19. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    float inputamount, result;
    int countrycode;
    
    inputamount = atoi(argv[1]);
    countrycode = atoi(argv[2]);
    
    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.0;
    }
    return result;
    }
    
    
    You missed an "n" in the "inputamount" in the first case of the switch.

    Also, make sure you include both stdio.h and stdlib.h.
     
  20. Carrie Robinson Registered Member

    Messages:
    16
    /*Cathy Robinson Currency Conversion Program*/

    /* Write a beginning of a C program that displays "Currency Conversion" and then write
    the names of five currencies and their equivalents to US dollars. We need to be prepared
    to demonstrate our program.
    */

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

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

    printf(input amount = atoi [argv[1]);
    printf(countrycode = atoi(argv[2]);

    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.0;
    }
    return result;
    }





    I made the changes to the program and I still get an error message
    C:\Program Files\Miracle C\cathy robinson currency program(1).c: line 16: variable 'amount' not found
    '(input amount = atoi [argv[1])'
    aborting compile
     
  21. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    "variable 'amount' not found"

    This means you have typed "input amount" instead of "inputamount" on line 16.
     
  22. Carrie Robinson Registered Member

    Messages:
    16
    How would I design a flow chart for this program

    How would I design a flow chart for this program?


    /*Cathy Robinson Currency Conversion Program*/

    /* Write a beginning of a C program that displays "Currency Conversion" and then write
    the names of five currencies and their equivalents to US dollars. We need to be prepared
    to demonstrate our program.
    */

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

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

    printf(inputamount = atoi(argv[1]);
    printf(countrycode = atoi(argv[2]);

    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.0;
    }
    return result;
    }

    I also get this error when I try to compile the program:
    c: line 16: function not declared
    '(inputamount = atoi(argv[1])'
    aborting compile
     
    Last edited: Feb 21, 2003
  23. Adam §Þ@ç€ MØnk€¥ Registered Senior Member

    Messages:
    7,415
    Yuck, flow charts. Flow charts really aren't very good. They're too simplistic for decent programmes.

    Hard to do it just in text, and I don't remember which shape is used for each type of thing in the chart. Have some of your friends check it over before you hand it in. Hopefully someone here at sciforums will be able to draw one up for you, as an example.


    PS: Have you got the programme working, and can you use it correctly from the command line?
     
Thread Status:
Not open for further replies.

Share This Page