Solved

Selection number from a menu

Posted on 2004-08-08
8
259 Views
Last Modified: 2010-04-15
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;                          
}
                   
               



0
Comment
Question by:Rfusco
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11748201
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.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11748217
option = getch();

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

   // action for option 2 here
}
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11748231
The symbol or must be double for conditionals
Example:

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

}
0
 

Author Comment

by:Rfusco
ID: 11748898
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;                          
}
                   
               



0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 11749111
Well, I will retouch your code a bit:

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

void  main(void)           /* DON'T USE FLOAT TO DECLARE MAIN FUNCTION */
{                        /*beginning function*/
float CAD,EUR,GBP,JPY,RUR,US;         /* THAT'S OK */                                        

    CAD=1.31751;    /* THAT'S OK, ALTHOUGH COULD GIVE YOU A WARNING MESSAGE */
    EUR=0.829643;   /* YOU CAN PUT A F AFTER THE NUMBER TO SPECIFY IT IS FLOAT */          
    GBP=0.548242;   /* SOMETHING LIKE:      GBP=0.548242f;   */
    JPY=110.631;
    RUR=29.1800;                                                                                  
    US=1.00;

    /* ALL THAT IS OK */                                
    printf("Currency Conversion\n\n");      
    printf("%f European Euro is equivalent to %f US Dollar\n",EUR,US);
    printf("%f United Kingdom Pound is equivalent to %f US Dollar\n",GBP,US);
    printf("%f Japanese Yens is equivalent to %f US Dollar\n",JPY,US);  
    printf("%f Canadian Dollars is equivalent to %f US Dollar\n",CAD,US);
    printf("%f Russian Rubles is equivalant to %f US Dollar\n",RUR,US);
 
getch();   /* WAITS UNTIL A KEY IS PRESSED, getch() WAIT FOR A KEY AND TELLS YOU WHICH */

convert();  /* I HAVE ADDED THIS LINE, SEE COMMENT BELOW, THE FUNCTION WILL BE EXECUTED NOW */

return 0;  /* THE PROGRAM WILL TERMINATE HERE, WILL DO NOTHING MORE */                        
}                      

/* HERE IS THE POINT, YOU CAN'T DECLARE A FUNCTION CALLED main() AGAIN, MAYBE YOU CAN NAME IT 'convert' */
void convert (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;  /* THIS VARIABLE NAME IS REPEATED, WILL GENERATE ERROR */

/* HERE COMES MY getch() SUGGESTION, getch() WILL TELL YOU WHICH KEY WAS PRESSED */
    char option; /* YOU CAN USE THIS VARIABLE TO OBTAIN KEY PRESSED */

    option = getch();  /* NOW OPTION HAVE THE KEY PRESSED */

/* AT THIS POINT YOU HAVE TO EVALUATE option TO MAKE PROPER CONVERSION */
    if (option=='1') {

      /* CONVERT EURO AND PRESENT RESULT */

    }
/* ETCETERA, ETCETERA, ETCETERA */

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;                          
}

------------------------------------------------------------------------------------------------------

WELL, THERE A LOT OF THING TO DO, BUT YOU MUST READ MORE C LANGUAGE THEORY, IF NOT, IT WILL BE IMPOSSIBLE
TO YOU TO TERMINATE YOUR WORK.
0
 

Expert Comment

by:aakash_mandhar
ID: 11749797
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
0
 

Expert Comment

by:aakash_mandhar
ID: 11749801
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
0
 
LVL 9

Expert Comment

by:ankuratvb
ID: 11749882
>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.

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now