# Calling multiple functions

• 04-07-2003
martina
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;
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));
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;
}
• 04-07-2003
SourceCode
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.
• 04-07-2003
martina
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
• 04-08-2003
martina
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 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));
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;
}
• 04-08-2003
SourceCode
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 */ }```
• 04-08-2003
Hammer
• 04-09-2003
martina
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;

// 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
• 04-09-2003
stumon
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.
• 04-09-2003
ronin
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; }```