Link to home
Start Free TrialLog in
Avatar of Rfusco
Rfusco

asked on

Selection number from a menu

This is my first program attempt ever. I am having a problem with simply knowing how to go about selecting one currency number from the menu and validate if it is correct? Any advice on what I have so far please would be greatly appreciated. I also am unsure of how to print the symbol "or"Thanks  th/******************************************************************************************************/

/*Currency Conversion Program created by Rebecca Fusco 8/4/04*/

#include <stdio.h>              /*header*/

/*five selected world currency conversions to 1.00 US dollar,float datatype*/
float main(void)

{                        /*beginning function*/
/*world country selections for currency conversions:Canada,Europe,United Kingdom,Japan,Russia,United States*/                                      
float CAD,EUR,GBP,JPY,RUR,US;                                                

/*assign values to selected countries,reference retrieved from live market
  at http://www.xe.com/effective as of 8/4/04 18:44 GMT*/                                        
    CAD=1.31751;
    EUR=0.829643;              
    GBP=0.548242;
    JPY=110.631;
    RUR=29.1800;                                                                                  
    US=1.00;
                                 
    printf("Currency Conversion\n\n");          /*displays title and double space*/
/*declare variables for currency rate from European Euro converted to the United States Dollar*/                                                          
    printf("%f European Euro is equivalent to %f US Dollar\n",EUR,US);
/*declare variables for currency rate from United Kingdom Pound to the United States Dollar*/                                                                          
    printf("%f United Kingdom Pound is equivalent to %f US Dollar\n",GBP,US);
/*declare variables for currency rate from Japanese Yens to the United States Dollar*/
    printf("%f Japanese Yens is equivalent to %f US Dollar\n",JPY,US);  
 /*declare variables for currency rate from Canadian Dollars to the United States Dollar*/
    printf("%f Canadian Dollars is equivalent to %f US Dollar\n",CAD,US);
/*declare variables for currency rate from Russian Rubles converted to the United States Dollar*/                                                          
    printf("%f Russian Rubles is equivalant to %f US Dollar\n",RUR,US);
 
getch();
return 0;                          
}                                   /*ends*/    

/**************************************************************************************************/                              
  /*Select a currency conversion type by code number 8/11/04*/
#include <stdio.h>                            
float main(void)
{
    printf("Select a currency type\n\n");
    printf("1=European Euro 2=United Kingdom Pound 3=Japanese Yens 4=Canadian Dollars 5=Russian Rubles\n");
    printf("Select the 4=Canadian Dollars currency type\n\n");
}
int EUR,GBP,JPY,CAD,RUR,US;  
    EUR=1;
    GBP=2;
    JPY=3;
    CAD=4;
    RUR=5;
while (CAD=<5>3);
if (CAD=4);

printf("Number 4 Canadian Dollars has been selected\n\n");

float CAD,EUR,GBP,JPY,RUR,US;  
    CAD=1.31751;
    EUR=0.829643;              
    GBP=0.548242;
    JPY=110.631;
    RUR=29.1800;                                                                                  
    US=1.00;  
    printf("The selected currency type is the Canadian dollar of %f equivlwnt to %f US Dollar\n"CAD,US};
getch();
return 0;                          
}
                   
               



Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Main function could be void main(void) but never float

float main(void)
{
    printf("Select a currency type\n\n");
    printf("1=European Euro 2=United Kingdom Pound 3=Japanese Yens 4=Canadian Dollars 5=Russian Rubles\n");
    printf("Select the 4=Canadian Dollars currency type\n\n");
}   <---- you must not close braces here to avoid program to end
int EUR,GBP,JPY,CAD,RUR,US;  
    EUR=1;
    GBP=2;
 

You have used getch() in your code, but you must save return in a variable, something like:

char option;

option = getch();

then you can use consecutive if() statements to take action for every option, or use a switch() statement.
option = getch();

if (option == '1') {      <--- notice double equal sign and simple quotes
   // some action here
} else if (option=='2') {

   // action for option 2 here
}
The symbol or must be double for conditionals
Example:

if (a==5 || a==2) {

}
Avatar of Rfusco
Rfusco

ASKER

I tried to understand what you were telling me to do but I am not sure. When I change to void main (void)  my program will no longer compile. I really do not understand the getch concept nor the if or if else very well. What am I doing wrong with the changes? Thanks an /******************************************************************************************************/

/*Currency Conversion Program created by Rebecca Fusco 8/4/04*/

#include <stdio.h>              /*header*/

/*five selected world currency conversions to 1.00 US dollar,float datatype*/
float main(void)

{                        /*beginning function*/
/*world country selections for currency conversions:Canada,Europe,United Kingdom,Japan,Russia,United States*/                                      
float CAD,EUR,GBP,JPY,RUR,US;                                                

/*assign values to selected countries,reference retrieved from live market
  at http://www.xe.com/effective as of 8/4/04 18:44 GMT*/                                        
    CAD=1.31751;
    EUR=0.829643;              
    GBP=0.548242;
    JPY=110.631;
    RUR=29.1800;                                                                                  
    US=1.00;
                                 
    printf("Currency Conversion\n\n");          /*displays title and double space*/
/*declare variables for currency rate from European Euro converted to the United States Dollar*/                                                          
    printf("%f European Euro is equivalent to %f US Dollar\n",EUR,US);
/*declare variables for currency rate from United Kingdom Pound to the United States Dollar*/                                                                          
    printf("%f United Kingdom Pound is equivalent to %f US Dollar\n",GBP,US);
/*declare variables for currency rate from Japanese Yens to the United States Dollar*/
    printf("%f Japanese Yens is equivalent to %f US Dollar\n",JPY,US);  
 /*declare variables for currency rate from Canadian Dollars to the United States Dollar*/
    printf("%f Canadian Dollars is equivalent to %f US Dollar\n",CAD,US);
/*declare variables for currency rate from Russian Rubles converted to the United States Dollar*/                                                          
    printf("%f Russian Rubles is equivalant to %f US Dollar\n",RUR,US);
 
getch();
return 0;                          
}                                   /*ends*/    

/**************************************************************************************************/                              
  /*Select a currency conversion type by code number 8/11/04*/
#include <stdio.h>                            
void main (void)
{
    printf("Select a currency type\n\n");
    printf("1=European Euro 2=United Kingdom Pound 3=Japanese Yens 4=Canadian Dollars 5=Russian Rubles\n");
    printf("Select the 4=Canadian Dollars currency type\n\n");
int EUR,GBP,JPY,CAD,RUR,US;  
    EUR=1;
    GBP=2;
    JPY=3;
    CAD=4;
    RUR=5;
char CAD;
CAD=(4);
if EUR==1);
{
else if(GBP==2);
else if (JPY==3);
else if (CAD==4);
else if (RUR==5);
}
printf("Number 4 Canadian Dollars has been selected\n\n");

float CAD,EUR,GBP,JPY,RUR,US;  
    CAD=1.31751;
    EUR=0.829643;              
    GBP=0.548242;
    JPY=110.631;
    RUR=29.1800;                                                                                  
    US=1.00;  
    printf("The selected currency type is the Canadian dollar of %f equivlwnt to %f US Dollar\n"CAD,US};
getch();
return 0;                          
}
                   
               



ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK I hope this helps you.....


int main()
{
while(1) /* infinite loop which keeps taking input */
{
fflush(stdin); /////  this is very important part while scanning number from the keyboard because if you enter a character your program will get into an infinite loop , that is it will never terminate (Fflush(stdin) flushed the standard input stream)

ch=getch();

switch(ch) //is the best thing to use for menu driven programs
{
case '1':
            printf("You selected 1\n");
            break;

case '2':
            printf("You selected 2\n");
            break;

case '3':
            printf("You selected 3\n");
            break;

case '4':
            printf("You selected 4\n");
            break;

case '5':
            printf("You selected 5\n");
            break;

default:
           printf("You selected something other than 1 to 5");

}

}

}


// what the swirch statement does is it is equal to several nested if-else statements.... In a menu u have fixed number of choices and for each choice you can write the corresponding code after the case 'value' for the input selected from menu... For any other value the code is written in default label...

Hope this helps....

Regards
Aakash
Ok.. In mty previous submission i did not want to code the whole program and give it to you because it will hamper your learning... Get the concept and do the rest yourself...

Regards
Aakash
>Main function could be void main(void) but never float

main function,according to the ANSI and the ISO standard, has a signature of int main(),and it
should always be used.

Why?
Because main() is called by the calling environment,usually the OS,and the C program should return a
value to the calling environment,as to whether its execution was successful or it ended in an error.

Rebecca,
What compiler are you working on?
You're using getch(),you'll also have to include the relevant header file as well.
If you're on Borland's compiler,#include<conio.h>
If on Linux,include the curses library.