Thread: Calling multiple functions

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    4

    Question Calling multiple functions

    I have spent countless hours on this and I am stuck.
    This program is currency conversion program. A user selects a currency then an amont anthe selects the currency to conver to.

    firstchoice() is the procedure the fisrt selection is made and in getchoice() the second selection is made.

    The amount thats enterd is fist converted to US and then to the currency.

    I cant get it to complie.

    Here is source Code
    #include <stdio.h>
    #include <math.h>
    int getchoice(void);
    int firstchoice(void);
    int beginconv(void);
    int main(void)
    {

    #define EURO .926698 //conversion rate for Euro
    #define FRANC 6.07874 // conversion rate for France Franc
    #define YEN 120.300 // conversion rate for Japanese Yen
    #define D_MARC 1.81246 // conversion rate for Germnay Deutshce Marc
    #define POUND 0.619579 // conversion rate for UK pound

    /************************************************** ***
    1 US dollar = 0.926698 Euro
    1 US dollar = 6.07874 Franc
    1 US dollar = 120.300 Yen
    1 US dollar = 1.81246 Deutsche Marc
    1 US dollar = 0.619579 Pound
    ************************************************** *****
    *****/


    float amount =0.00;


    printf("\n\n\n");
    printf("1. Franc\n");
    printf("2. yen\n");
    printf("3. Euro\n");
    printf("4. D_MArc\n");
    printf("5. Pound\n");
    printf("6. US\n");

    beginconv();


    }
    /////////////
    int beginconv(void)
    {
    int amount;
    int choice;
    float answer;
    int myVar1 ;
    int myVar2;

    myVar1 = selection1(FRANC, EURO, POUND);
    myVar2 = selection2(FRANC, EURO, POUND);



    printf("%s select cuurency you want to convert to: ");
    scanf ("%d", &choice);
    if (scanf (choice <= '6') || (choice >= '1') );


    switch( choice )
    {
    case 1: if ((myVar1 == int FRANC , int EURO, int POUND) &&(myVar2 == int FRANC, int EURO,int POUND));
    answer = (amount/selection1)*myVar2;
    break;

    case 2: printf("\n test 2\n");
    break;


    }

    return 0;
    }

    //////////////////
    int firstchoice(void)
    {
    int selection1;
    float currency1;
    float currency2;
    float currency3;
    float currency4;
    float currency5;
    float currency6;

    while ( (selection1 = getchoice()) != 'q')
    {
    switch (selection1)
    {
    case 'a' : currency1 = FRANC;
    break;
    case 'b' : currency2 = YEN;
    break;
    case 'c' : currency3 = EURO;
    break;
    case 'd' : currency4 = POUND;
    break;
    case 'e' : currency5 = D_MARC;
    break;
    case 'f' : currency6 = US;
    break;
    default : printf("Program error!\n");
    break;
    }
    }
    return 0;
    }


    //////////////////////
    int getchoice(void)
    {
    int selection2;
    float currency1;
    float currency2;
    float currency3;
    float currency4;
    float currency5;
    float currency6;

    while ( (selection2 = getchoice()) != 'q')
    {
    switch (selection2)
    {
    case 'a' : currency1 = FRANC;
    break;
    case 'b' : currency2 = YEN;
    break;
    case 'c' : currency3 = EURO;
    break;
    case 'd' : currency4 = POUND;
    break;
    case 'e' : currency5 = D_MARC;
    break;
    case 'f' : currency6 = US;
    break;
    default : printf("Program error!\n");
    break;
    }
    }
    return 0;
    }

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    Hi, I don't have time to point out every single error, there are quite a few, but here's a start.

    - Your #define's should go outside of main.

    - You never defined US.

    This is way off
    Code:
     myVar1 = selection1(FRANC, EURO, POUND);
    myVar2 = selection2(FRANC, EURO, POUND);
    Im not sure what selection1 or selection2 are supposed to be in beginconv, and if im not sure neither is the compiler Your declaring them as variables in other functions and using them as functions.

    And this
    Code:
     if (scanf (choice <= '6') || (choice >= '1') );
    Thats not how you use scanf, also your comparing choice an integer to a character value. You can do this but its prolly not what you want in this situation.

    There is alot alot more errors but I don't have time to go through it all, most of them are pretty severe Goodluck.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    Thanks for your input, this is my first C class and I am trying to slove a problem.

    The styles that I have adopted are from text books and the internet. I guess this might be as result of different writting styles or standards.


    Regards

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    I have corrected some errors, it now complies but it hangs up after one selection is made.

    #include <stdio.h>
    #include <math.h>
    int getchoice(void);
    int firstchoice(void);
    int beginconv(void);
    int main(void)
    #define EURO .926698 //conversion rate for Euro
    #define FRANC 6.07874 // conversion rate for France Franc
    #define YEN 120.300 // conversion rate for Japanese Yen
    #define D_MARC 1.81246 // conversion rate for Germnay Deutshce Marc
    #define POUND 0.619579 // conversion rate for UK pound



    /************************************************** ***
    1 US dollar = 0.926698 Euro
    1 US dollar = 6.07874 Franc
    1 US dollar = 120.300 Yen
    1 US dollar = 1.81246 Deutsche Marc
    1 US dollar = 0.619579 Pound
    ************************************************** *****
    *****/
    {
    int choice;
    float amount =0.00;


    printf("\n\n\n");
    printf("1. Franc\n");
    printf("2. yen\n");
    printf("3. Euro\n");
    printf("4. D_MArc\n");
    printf("5. Pound\n");
    printf("6. US\n");

    printf("select curency you want to convert to: ");
    scanf ("%d", &choice);
    firstchoice();
    //beginconv();


    }
    /////////////
    int beginconv(void)
    {
    int amount;
    int choice;
    float answer;
    float myVar1 ;
    float myVar2;
    firstchoice();
    getchoice();
    //myVar1 = selection1(FRANC, EURO, POUND);
    //myVar2 = selection2(FRANC, EURO, POUND);


    printf("%s select curency you want to convert to: ");
    scanf ("%d", &choice);
    if (scanf (choice <= '6') || (choice >= '1') );


    switch( choice )
    {
    case 1: if ((myVar1 == 6.07874 || myVar1 == .926698 || myVar1 == 0.619579) && (myVar2 == 6.07874 || myVar2 == .926698 || myVar2 == 0.619579))


    //if ((myVar1 == int FRANC , int EURO, int POUND) &&(myVar2 == int FRANC, int EURO,int POUND));
    answer = (amount/myVar1)*myVar2;
    break;

    case 2: printf("\n test 2\n");
    break;


    }

    return 0;
    }

    //////////////////
    int firstchoice(void)

    {
    int selection1;
    float currency1;
    float currency2;
    float currency3;
    float currency4;
    float currency5;
    float currency6;

    while ( (selection1 <'a' ||selection1 > 'f') )
    {
    switch (selection1)
    {
    case 'a' : currency1 = FRANC;
    break;
    case 'b' : currency2 = YEN;
    break;
    case 'c' : currency3 = EURO;
    break;
    case 'd' : currency4 = POUND;
    break;
    case 'e' : currency5 = D_MARC;
    break;
    //case 'f' : currency6 = US;
    // break;

    }
    }
    return selection1;
    }


    //////////////////////
    int getchoice(void)
    {
    float selection2;
    float currency1;
    float currency2;
    float currency3;
    float currency4;
    float currency5;
    float currency6;
    int choice;
    while (( selection2 == 6.07874 || selection2 == .926698 ||selection2 == 0.619579 ))

    {
    switch (choice)
    {
    case 'a' : currency1 = FRANC;
    break;
    case 'b' : currency2 = YEN;
    break;
    case 'c' : currency3 = EURO;
    break;
    case 'd' : currency4 = POUND;
    break;
    case 'e' : currency5 = D_MARC;
    break;
    // case 'f' : currency6 = US;
    // break;

    }
    }
    return choice;
    }

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    This works, It compiles, its not done though, I left the conversion part for you to do. Goodluck.
    Code:
    /*
     *  1 US dollar = 0.926698 Euro
     *  1 US dollar = 6.07874 Franc
     *  1 US dollar = 120.300 Yen
     *  1 US dollar = 1.81246 Deutsche Marc
     *  1 US dollar = 0.619579 Pound
     */
     
    #include <stdio.h>
    #include <math.h>
    
    #define EURO 0.926698     /* conversion rate for Euro                  */
    #define FRANC 6.07874    /* conversion rate for France Franc          */
    #define YEN 120.300      /* conversion rate for Japanese Yen          */
    #define D_MARC 1.81246   /* conversion rate for Germnay Deutshce Marc */
    #define POUND 0.619579   /* conversion rate for UK pound              */
    
    
    int get_selection(int selection);
    double conversion(double first_choice, double second_choice);
    
    int main(void)
    {
    	int choice;
    	double currency;
    	double convert_to;
    	double final_amount;
    
    	/* Get the first currency */
    	do {
    		printf("1. Franc\n");
    		printf("2. yen\n");
    		printf("3. Euro\n");
    		printf("4. D_MArc\n");
    		printf("5. Pound\n");
    
    		printf("\nSelect a currency: ");
    		scanf ("%d", &choice);
    		currency = get_selection(choice);
    	} while(choice < 1 || choice > 5);
    
    	/* Get the currency to convery to */
           	do {
    		printf("1. Franc\n");
    		printf("2. yen\n");
    		printf("3. Euro\n");
    		printf("4. D_MArc\n");
    		printf("5. Pound\n");
    		
    		printf("\nSelect currency you want to convert to: ");
    		scanf("%d", &choice);
    		convert_to = get_selection(choice);
    	} while(choice < 1 || choice > 5);
    	
    	final_amount = conversion(currency, convert_to);
    }
    
    /* Get selection from user */
    int get_selection(int selection)
    {
    	double currency;
    	
    	switch (selection) {
    	case 1: currency = FRANC;  break;
    	case 2: currency = YEN;    break;
    	case 3: currency = EURO;   break;
    	case 4: currency = D_MARC; break;
    	case 5: currency = POUND;  break;
    	default:
    		printf("Invalid entry\n\n");
    	}
    	
    	return (currency);
    }
    
    
    double conversion(double first_choice, double second_choice)
    {	
    	double converted;
    	/* Convert the currency here */
    	/* Then return converted */
    }

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    martina: Read this please.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    4
    Thanks for your assistance it has been greatly appreciated.
    however i have a few issuees here
    1. printf(" %s Select a currency: \n", user_Name);
    does not work it displays funny characters
    2. Values are not passed to this command, 1 is always the output regardless of selection.

    here is the code



    /*
    * 1 US dollar = 0.926698 Euro
    * 1 US dollar = 6.07874 Franc
    * 1 US dollar = 120.300 Yen
    * 1 US dollar = 1.81246 Deutsche Marc
    * 1 US dollar = 0.619579 Pound
    */

    #include <stdio.h>
    #include <math.h>

    #define EURO 0.926698 /* conversion rate for Euro */
    #define FRANC 6.07874 /* conversion rate for France Franc */
    #define YEN 120.300 /* conversion rate for Japanese Yen */
    #define D_MARC 1.81246 /* conversion rate for Germnay Deutshce Marc */
    #define POUND 0.619579 /* conversion rate for UK pound */


    int get_selection(int selection);
    double conversion(double first_choice, double second_choice);
    char get_user_name(void);
    char user_Name[20];

    int main(void)

    {
    int choice;
    int amount;
    double currency;
    double convert_to;
    double final_amount;
    char user_Name[20];
    get_user_name(); // Function call which returns the user name
    /* Get the first currency */
    do {
    printf("1. Franc\n");
    printf("2. yen\n");
    printf("3. Euro\n");
    printf("4. D_MArc\n");
    printf("5. Pound\n");

    printf(" %s Select a currency: \n", user_Name);
    scanf ("%d", &choice);
    currency = get_selection(choice);

    } while(choice < 1 || choice > 5);



    /* Get the currency to convery to */
    do {
    printf("1. Franc\n");
    printf("2. yen\n");
    printf("3. Euro\n");
    printf("4. D_MArc\n");
    printf("5. Pound\n");

    printf("\nSelect currency you want to convert to: ");
    scanf("%d", &choice);
    convert_to = get_selection(choice);
    } while(choice < 1 || choice > 5);

    printf("%s enter amount to convert: ", user_Name);
    scanf("%d", &amount);
    final_amount = conversion(currency, convert_to);
    }


    /* Get selection from user */
    int get_selection(int selection)
    {
    double currency;

    switch (selection) {
    case 1: currency = FRANC; break;
    case 2: currency = YEN; break;
    case 3: currency = EURO; break;
    case 4: currency = D_MARC; break;
    case 5: currency = POUND; break;
    default:
    printf("Invalid entry\n\n");
    }

    return (currency);
    }


    double conversion(double first_choice, double second_choice)
    {
    double converted;
    double amount =0.00;
    /* Convert the currency here */


    if ((first_choice ==FRANC||first_choice==EURO||first_choice==YEN||fi rst_choice==POUND||first_choice==D_MARC)&& (second_choice ==FRANC||second_choice==YEN||second_choice== EURO||second_choice==D_MARC||second_choice==POUND) )
    converted = (amount/first_choice)*second_choice;
    printf("answer is %f",converted);

    // testing command
    pirntf("this is %d what was enterd \n",amount);

    }
    ///////////////
    char get_user_name(void)
    {

    char user_Name[20]; // Declare variable


    printf("Enter your first name: "); // Prompt user for first name
    scanf("%s", user_Name); // Read in user entry
    printf("%s, your name is %s \n",user_Name);


    return user_Name; // Return value to calling function

    } // End get_user_name

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You obviously did not read what Hammer sent to you, did you! Please click on his link and read what that post has to say.
    The keyboard is the standard device used to cause computer errors!

  9. #9
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I threw this together in a couple minutes that I had to spare. It doesn't check for errors, so if you can use any part of it, you'd have to work on that. I only tried it with $200 USD.

    Code:
    #include <stdio.h>
    
    double convert(char choice, double us)
    {
       const double convert[] = {  0.926698,
                                   6.07874,
                                   120.300,
                                   1.81246,
                                   0.619579,
                                   1 };
       
       double result = 1.0f;
       
       switch(choice)
       {
          case '1': result = us * convert[(choice-'0')-1];
                    break;
          
          case '2': result = us * convert[(choice-'0')-1];
                    break;
          
          case '3': result = us * convert[(choice-'0')-1];
                    break;
         
          case '4': result = us * convert[(choice-'0')-1];
                    break;
          
          case '5': result = us * convert[(choice-'0')-1];
                    break;
          
          case '6': result = us * convert[(choice-'0')-1];
                    break;
       }
       
       return result;
    }
     
    int main()
    {
       const char menu[] = "1> Euro\n"
                           "2> Franc\n"
                           "3> Yen\n"
                           "4> Deutsche Marc\n"
                           "5> Pound\n"
                           "6> US\n\n--> ";
       
       const char *codes[] = { "Euro", "Franc", "Yen",
                               "Deutsche Marc",
                               "Pound", "USD" };
                           
       double getUnit, usd;
       char user;
       
       printf("%s", menu);
       scanf("%c%*c", &user);
       
       printf("\nEnter US amount to convert: ");
       scanf("%lf%*c", &usd);
       
       getUnit = convert(user, usd);
       
       printf("\n%.2lf dollars US = %.2lf %s",
               usd, getUnit, codes[(user-'0')-1]);   
          
       return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling non-static functions on static variables
    By pandu in forum C++ Programming
    Replies: 14
    Last Post: 06-19-2008, 03:07 AM
  2. calling multiple WSAStartup()
    By Da-Nuka in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-16-2005, 09:45 AM
  3. Replies: 4
    Last Post: 03-11-2005, 05:45 PM
  4. Calling App Functions from DLL
    By johnnie2 in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2003, 01:08 AM
  5. Recursion vs multiple functions
    By PJYelton in forum C++ Programming
    Replies: 4
    Last Post: 12-29-2002, 08:52 PM