Solved

Still having some menu problems and quit does not work

Posted on 2004-09-14
21
187 Views
Last Modified: 2012-05-05
Here is my problem, this is a currency conversion program that has gone through many changes and I have thououghly confused myself.  I thouhgt I had it several times but keep trying different things and lost now.  I needs to accept the input and print out like 4000 British Pounds Sterling is equivalent to us$ 20.00  or such and it need to end when i enter q and not go into  a loop.

Please help

program:

#include <stdio.h>

#define Euro 0
#define Canadian_Dollar 1
#define Pound 2
#define Mark 3
#define YEN 4

char E[] = "Euro";
char CAD[] = "Canadian Dollar";
char UP[] = "United Kingdom Pound";
char GM[] = "German Deutsche Marks";
char JY[] = "Japan Yen";

void menu (int*);
float calculateConversion(int, float[], float);
int main()
{
      int j;
      float rate[5]={1.21,0.77,1.78,0.62,0.0091};
      for (j=0; j<5; j++)
      {
      int choice;
      float us;
      float conversion;
      menu(&choice);
      /*prompt user to enter amount of US to convert*/
        printf("Amount? $");
        /*store user entry into memeory*/;
        scanf("%f", &us);
      conversion=calculateConversion(choice, rate, us);
      /*print results of conversion*/
        printf("$%.2f, The amount of that currency in US Dollars is: $%.2f\n\n\n", us, conversion);
        }
getchar();
return 0;
}


/*function definition by reference*/
void menu(int *CHOICE)
{
     /*local variables for menu()*/
     int choice;
     do
     {
     /*show options to user*/
    /* printf("Currency Conversion Program\n");
     printf("Available currencies and equilvalence to US $ 1.00\n");
     printf("Press [1] for Euro \n");
     printf("Press [2] for Canadian Dollar\n");
     printf("Press [3] for United Kingdom Pound\n");
     printf("Press [4] for German Deutsche Marks\n");
     printf("Press [5] for Japan Yen\n");
     printf("Which currency? ");*/
     
     printf("Currency Conversion Program\n");
     printf("Available currencies and equivalence to US$1.00:\n");
     printf("1.  %s \t             1.20567\n", E);
     printf("2.  %s \t  0.769686\n", CAD);
     printf("3.  %s  1.77701 \n", UP);
     printf("4.  %s 0.616420\n", GM);
     printf("5.  %s  \t   0.0090517\n", JY);
     printf("\n To End Program Press Q\n");
     
     
     
     
     /*store choice in memory*/
     scanf("%d", &choice);
     /*set value of referenced variable*/
     *CHOICE = choice;
     }while(*CHOICE < 1 || *CHOICE > 5);
     
     
}


/*function definition by value*/
float calculateConversion(int CHOICE, float rate[], float us)
{
   /*local variable for conversion*/
   int j;
   float conversion;
   for (j=0; j<5; j++){
   /*switch to user the case selected by user in reference funtion*/
   switch (CHOICE)
     {
       case 1:
            conversion=rate[0]*us;
            break;
       case 2:
            conversion=rate[1]*us;
            break;
       case 3:
            conversion=rate[2]*us;
            break;
       case 4:
            conversion=rate[3]*us;
            break;
       case 5:
            conversion=rate[4]*us;
            break;
       case 'Q':
                 break;
       
            }
     }
/*returns the new value to the main function*/
return conversion;
}
0
Comment
Question by:macshawn
  • 8
  • 6
  • 5
  • +2
21 Comments
 

Author Comment

by:macshawn
ID: 12061264
the menu choice has to print out with the conversion results, I think I explained that poorly.
thanks
shawn
0
 

Author Comment

by:macshawn
ID: 12061327
I get confused with trying to do both the conversion and the tracking of menu choices.  Help Please.
thanks
shawn
0
 
LVL 11

Expert Comment

by:avizit
ID: 12061431
1.  some other minor error first

for (j=0; j<5; j++)
    {
      int choice;
      float us;
      float conversion;



move those definition out of the foor loop you dont need to declare/define them everytime the loop is executed

0
 
LVL 11

Expert Comment

by:avizit
ID: 12061440
scanf("%d", &choice);
     /*set value of referenced variable*/
     *CHOICE = choice;
     }while(*CHOICE < 1 || *CHOICE > 5);


in above , you expect user to enter 'Q" to quit but you read only ints ..

why noy make  choice a 'char' the program will stll work fine
 plus you need logic to quit the menu drawing when the user enters '"q/Q"

0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12061461
Move the do while loop from menu() to main

main :
   do
          call menu
          act on choice (switch)
   while choice != exit

right now your control gets stuck in the menu until you tell it to exit
0
 
LVL 11

Expert Comment

by:avizit
ID: 12061465
why are you doing the following in a foor loop ?

for (j=0; j<5; j++)
    {
      int choice;
      float us;
      float conversion;
      menu(&choice);
      /*prompt user to enter amount of US to convert*/
      printf("Amount? $");
      /*store user entry into memeory*/;
      scanf("%f", &us);
      conversion=calculateConversion(choice, rate, us);
      /*print results of conversion*/
      printf("$%.2f, The amount of that currency in US Dollars is: $%.2f\n\n\n", us, conversion);
    }


i.e why do you need to draw the menu , 5 times !!! , calculate 5 times etc etc ..


==========
i think you should better start from scratch.

use the following simpler logic


main(){
  int continue = 1;
  while(continue )
     char c = menu();
     if(  (c =='q' ) || (c =='Q')  
          continue = 0;
     else {
       calculate();
     }
   }
   exit(0);
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12061483
Also

     /*store choice in memory*/
     scanf("%d", &choice);   ->> choice is an int * ... you dont need address of choice here ... scanf("%d", choice) will do
     /*set value of referenced variable*/
     *CHOICE = choice;
     }while(*CHOICE < 1 || *CHOICE > 5);


Just to add some details to my previous post:

You realize that execution begins with main.In your main, you call menu() ... What are the conditions when you can exit from function menu? Until you enter choice less than 1 or greater than 5, you cannot exit from the function. This control does not return to the main until you enter a value which corresponds to exit !!!

When control finally does reach main, choice contains a value which tells main that user wants to exit.

What was missing in this program?

The loop should be
       - read choice
       - take action

what you have done here is

loop 1
     - read choice

loop 2
     - take action

loop 1 (do while) is in menu and loop 2 (for) is in main. What you need to do is to get the do while loop shifted from menu()to the main() ... something like

main
       do
                    menu()
                    switch(choice)
                             ....
        while (choice != exit)
end main

menu
         printf (menu)
end menu

Note that menu() does not have a loop. It only prints a menu. The repeated priniting of menu effect is achieved by calling menu() from within the do while loop in main().

Program has been well written but it just needs some careful thought and moving around the statements a bit to get it working .... I would recommend sticking to it ... No better teacher than own mistakes :)

good luck
sunnycoder
0
 

Author Comment

by:macshawn
ID: 12061488
c:\program files\miracle c\conversionex2.c: line 20: Parse Error, expecting `SEP'
'int main() int j'
aborting compile

I get this error now, what did I do wrong?

#include <stdio.h>

#define Euro 0
#define Canadian_Dollar 1
#define Pound 2
#define Mark 3
#define YEN 4

char E[] = "Euro";
char CAD[] = "Canadian Dollar";
char UP[] = "United Kingdom Pound";
char GM[] = "German Deutsche Marks";
char JY[] = "Japan Yen";

void menu (int*);
float calculateConversion(int, float[], float);
int main()

int j;
float rate[5]={1.21,0.77,1.78,0.62,0.0091};
for (j=0; j<5; j++)
char choice[];
float us;
float conversion;
{
      /**int j;
      float rate[5]={1.21,0.77,1.78,0.62,0.0091};
      for (j=0; j<5; j++)***/
      {
      /*int choice;
      float us;
      float conversion;*/
      menu(&choice);
      /*prompt user to enter amount of US to convert*/
        printf("Amount? $");
        /*store user entry into memeory*/;
        scanf("%f", &us);
      conversion=calculateConversion(choice, rate, us);
      /*print results of conversion*/
        printf("$%.2f, The amount of that currency in US Dollars is: $%.2f\n\n\n", us, conversion);
        }
getchar();
return 0;
}


/*function definition by reference*/
void menu(int *CHOICE)
{
     /*local variables for menu()*/
     char choice;
     do
     {
     /*show options to user*/
    /* printf("Currency Conversion Program\n");
     printf("Available currencies and equilvalence to US $ 1.00\n");
     printf("Press [1] for Euro \n");
     printf("Press [2] for Canadian Dollar\n");
     printf("Press [3] for United Kingdom Pound\n");
     printf("Press [4] for German Deutsche Marks\n");
     printf("Press [5] for Japan Yen\n");
     printf("Which currency? ");*/
     
     printf("Currency Conversion Program\n");
     printf("Available currencies and equivalence to US$1.00:\n");
     printf("1.  %s \t             1.20567\n", E);
     printf("2.  %s \t  0.769686\n", CAD);
     printf("3.  %s  1.77701 \n", UP);
     printf("4.  %s 0.616420\n", GM);
     printf("5.  %s  \t   0.0090517\n", JY);
     printf("\n To End Program Press Q\n");
     
     
     
     
     /*store choice in memory*/
     scanf("%d", &choice);
     /*set value of referenced variable*/
     *CHOICE = choice;
     }while(*CHOICE < 1 || *CHOICE > 5);
     
     
}


/*function definition by value*/
float calculateConversion(char CHOICE, float rate[], float us)
{
   /*local variable for conversion*/
   int j;
   float conversion;
   for (j=0; j<5; j++){
   /*switch to user the case selected by user in reference funtion*/
   /*switch (CHOICE)*/
         while (CHOICE != 'Q')
         switch (CHOICE)
     {
       case 1:
            conversion=rate[0]*us;
            break;
       case 2:
            conversion=rate[1]*us;
            break;
       case 3:
            conversion=rate[2]*us;
            break;
       case 4:
            conversion=rate[3]*us;
            break;
       case 5:
            conversion=rate[4]*us;
            break;
       case 'Q':
       
                 break;
       default:
                 printf("Enter a valid option/n");
            }
     }
/*returns the new value to the main function*/
return conversion;
}
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12061497
int main has to be followed by an opening brace {
0
 
LVL 11

Expert Comment

by:avizit
ID: 12061499
int main()
int j;


there should be a '{' after int main()


0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:avizit
ID: 12061501
heh :p
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12061503
:)
0
 

Author Comment

by:macshawn
ID: 12061534
OH, I feel like Homer Simpson, Doh, but this is another one probably:

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\conversionex2.c
main

c:\program files\miracle c\conversionex2.c: line 22: Parse Error, expecting `','' or `SEP'
'char choice'
aborting compile

#include <stdio.h>

#define Euro 0
#define Canadian_Dollar 1
#define Pound 2
#define Mark 3
#define YEN 4

char E[] = "Euro";
char CAD[] = "Canadian Dollar";
char UP[] = "United Kingdom Pound";
char GM[] = "German Deutsche Marks";
char JY[] = "Japan Yen";

void menu (int*);
float calculateConversion(int, float[], float);
int main()
{
int j;
float rate[5]={1.21,0.77,1.78,0.62,0.0091};
for (j=0; j<5; j++)
char choice;
float us;
float conversion;

      /**int j;
      float rate[5]={1.21,0.77,1.78,0.62,0.0091};
      for (j=0; j<5; j++)***/
      {
      /*int choice;
      float us;
      float conversion;*/
      menu(&choice);
      /*prompt user to enter amount of US to convert*/
        printf("Amount? $");
        /*store user entry into memeory*/;
        scanf("%f", &us);
      conversion=calculateConversion(choice, rate, us);
      /*print results of conversion*/
        printf("$%.2f, The amount of that currency in US Dollars is: $%.2f\n\n\n", us, conversion);
        }
getchar();
return 0;
}


/*function definition by reference*/
void menu(int *CHOICE)
{
     /*local variables for menu()*/
     char choice;
     do
     {
     /*show options to user*/
    /* printf("Currency Conversion Program\n");
     printf("Available currencies and equilvalence to US $ 1.00\n");
     printf("Press [1] for Euro \n");
     printf("Press [2] for Canadian Dollar\n");
     printf("Press [3] for United Kingdom Pound\n");
     printf("Press [4] for German Deutsche Marks\n");
     printf("Press [5] for Japan Yen\n");
     printf("Which currency? ");*/
     
     printf("Currency Conversion Program\n");
     printf("Available currencies and equivalence to US$1.00:\n");
     printf("1.  %s \t             1.20567\n", E);
     printf("2.  %s \t  0.769686\n", CAD);
     printf("3.  %s  1.77701 \n", UP);
     printf("4.  %s 0.616420\n", GM);
     printf("5.  %s  \t   0.0090517\n", JY);
     printf("\n To End Program Press Q\n");
     
     
     
     
     /*store choice in memory*/
     scanf("%d", &choice);
     /*set value of referenced variable*/
     *CHOICE = choice;
     }while(*CHOICE < 1 || *CHOICE > 5);
     
     
}


/*function definition by value*/
float calculateConversion(char CHOICE, float rate[], float us)
{
   /*local variable for conversion*/
   int j;
   float conversion;
   for (j=0; j<5; j++){
   /*switch to user the case selected by user in reference funtion*/
   /*switch (CHOICE)*/
         while (CHOICE != 'Q')
         switch (CHOICE)
     {
       case 1:
            conversion=rate[0]*us;
            break;
       case 2:
            conversion=rate[1]*us;
            break;
       case 3:
            conversion=rate[2]*us;
            break;
       case 4:
            conversion=rate[3]*us;
            break;
       case 5:
            conversion=rate[4]*us;
            break;
       case 'Q':
       
                 break;
       default:
                 printf("Enter a valid option/n");
            }
     }
/*returns the new value to the main function*/
return conversion;
}
0
 

Author Comment

by:macshawn
ID: 12061541
I have to get past these errors before I can try anything else :)
0
 
LVL 11

Expert Comment

by:avizit
ID: 12061580
as i said earlier

===============
for (j=0; j<5; j++)
char choice;
float us;
float conversion;

     /**int j;
     float rate[5]={1.21,0.77,1.78,0.62,0.0091};
     for (j=0; j<5; j++)***/
     {
============

take the declaration out of the "for"

to get rid of the compiler error you can
change the above to

char choice ;
float us;
float conversion;
for (j=0; j<5; j++)
   { .......



that should make the compiler warning go away , but i would still doubt why you need that for loop ? what does it do ? why you have to do something for 5 times .? etc etc






0
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12063047
You can add the following lines in the code of void menu() function

if (choice == 'q' || choice == 'Q') {
    exit(0);
}

just after scanning the input.
0
 
LVL 9

Accepted Solution

by:
ankuratvb earned 500 total points
ID: 12064782
Hi,

According to the C standard,you should declare all your variables at the start of a block.
As avizit has pointed out,move your declarations at the start of main().

Also,the only reason i can see for using the j=0;j<5; for loop is for printing the converted currency value in all 5 available currencies.the loop exists in the calculateconversion function as well.

That conflicts with asking the user for the choice of converted currency but other than that,there seems no reason for that for loop.

If you want the program to keep asking for amounts of US to convert till the user presses Q to exit,the for loop in main() should be an infinite loop.

Then,you could check the user's choice for 'Q' or 'q' and use exit() to exit from the program.
0
 

Author Comment

by:macshawn
ID: 12068122
Ok, taking all advice and using Avizit's model to start over, I get this error message, I'm sure it is an easy one, just staring at it too long and can't see it, also, please check over my logic so far.  Thanks
shawn


current error message:

Miracle C Compiler (r3.2), written by bts.
Compiling c:\program files\miracle c\week5nb.c
main

c:\program files\miracle c\week5nb.c: line 13: Parse Error, expecting `SEP'
'int continue = 1'
aborting compile


New start:


#include<stdio.h>


main(){

char E[] = "Euro";
char CAD[] = "Canadian Dollar";
char UP[] = "United Kingdom Pound";
char GM[] = "German Deutsche Marks";
char JY[] = "Japan Yen";


  int continue = 1;
  while(continue )
     char c = menu();
     
           printf("Currency Conversion Program\n");
           printf("Available currencies and equivalence to US$ 1.00:\n");
           printf("1. %s \t      1.21\n", E);
           printf("2. %s \t      0.77\n", CAD);
           printf("3. %s \t      1.78\n", UP);
           printf("4. %s \t      0.62\n", GM);
           printf("5. %s \t      0.0091\n", JY);
           printf("\n To End Program Press Q\n");
              
           printf("Which Currency? \n");
           scanf("%s ", c);
           
     
     if(  (c =='q' ) || (c =='Q')  
          continue = 0;
     else {
       calculate();
       
       
     }
   }
   exit(0);
}

0
 

Author Comment

by:macshawn
ID: 12069544
can someone figure out my error above please, so I can do the rest of the program, :)
0
 

Author Comment

by:macshawn
ID: 12070137
Thanks all Im starting over again in a new post, with a totally new approach, one I had before I got totally lost, the points need to be split but my fuzz eyes and brain cannot remember how to split them.
thanks all for putting up with stupid newbie questions.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 12071989
Hi macshawn,

This is the second time I have seen you close a question improperly. If you had taken time to read the link I posted for you last time, you would have known why is it important, rather as important as asking a question.

No offense intended, but I will be steering clear of your questions in future.

sunnycoder
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays 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

12 Experts available now in Live!

Get 1:1 Help Now