Solved

trying to figure out how to print out menu choices

Posted on 2004-09-13
8
260 Views
Last Modified: 2010-04-15
I need to the output to look like this:
menu display, does ok

Which currency? does ok
Amount? does ok

Problem area:

example output, what it needs to look like, I cannot figure out how to print out the menu choice with the Currency choosen, do I have to do more case statements? Show me an example please.

what it should look like  " 44000 Italian Lira is equilivent to US$ 20.00"  Can't get the Italian Lira part.

here is my code:

/* Currency Conversion Program with Menu */

#include<stdio.h>
#include<stdlib.h>

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


     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 \t  1.77701 \n", UP);
     printf("4.  %s \t  0.616420\n", GM);
     printf("5.  %s \t     0.0090517\n", JY);
     printf("\n To End Program Press Q\n");
   
}

int main(void)
{
     int menuChoice;
     char again;
     float amount;
     float result;
     float conversion;

    do{
         menu();
        do{
             printf("\n Which currency? ");
             scanf("%d", &menuChoice);

             if(menuChoice <= 5)
            {
               printf("Amount? ");
               scanf("%f", &amount);
            }
            else
                    printf("Incorrect number entered, please choose between 1 to 5. \n");
        }while(menuChoice == 5);

          switch(menuChoice)
          {
             case 1: conversion = 1.20567;
                         break;

            case 2: conversion = 0.769686;
                         break;

               case 3: conversion = 1.77701;
                         break;

               case 4: conversion = 0.616420;
                         break;

               case 5: conversion = 0.0090517;
                         break;
                         
                case 'Q':
                      break;
          }
          result = amount/conversion;

          if(menuChoice <= 5)
          {
             printf("You choose %d\n" , menuChoice);
             printf("%.2f  " ,amount);
               printf("%.2f\n",conversion, "of currency ", result);
               getchar();
               again = getchar();
          }
     }while(again == 'y' || again == 'y');
     return 0;
}
0
Comment
Question by:macshawn
  • 5
  • 2
8 Comments
 
LVL 3

Accepted Solution

by:
CmdrRickHunter earned 250 total points
ID: 12049615
a coupple of techniques.
1) case statemnt.  you've shown you know how to do it, no problems there
2) instead of having a bunch of char[]'s with different names, create an array of arrays.
3) pointer logic:
const char* currencyType;

then, in the switches:

case 5: conversion = 0.0090517;
 currencyType = JY;


then later
printf("%.2f %s is equivilant to US $%.2f", amount, currencyType, result);


I think number three fits best with your code style so far.  Having a char[] array makes more sense usually... but since you allready have a switch going on, it just feels more natutral (to me) to use a pointer.
0
 

Author Comment

by:macshawn
ID: 12050555
Hey thanks, but I have another question, clarification basically for a novice,
the const char * currencyType line,  something has to be declared or is this a declaration statment too?
I understand the rest, I think, I assume that I need to add a currencyType = under each case statement, set equal to Euro, cad dollar, pounds sterling, etc.
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 250 total points
ID: 12050815
Hi Macshawn,

You can restructure the program slightly so that is winds up being shorter and simpler.  Here's a start:

#include<stdio.h>
#include<stdlib.h>

#define EURO 0
#define CANADIAN_DOLLAR 1
#define POUND 2
#define MARK 3
#define YEN 4

/*  Make your currencies global.  */

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

float ERate = 1.20567;
float CRate = 0.769686;
float URate = 1.77701;
float GRate = 0.616420;
float JRate = 0.0090517;

char *CurrencyName[5] = {E, CAD, UP, GM, JY};
float *CurrencyRate[5] = {&ERate, &CRate, &URate, &GRate, &JRate};

void menu(void)
{
     int idx;

     printf("Currency Conversion Program\n");
     printf("Available currencies and equivalence to US$1.00:\n");
     for (idx = 0; idx < 5; idx++)
       printf("1.  %s \t       %f\n", CurrencyName[idx], *CurrencyRate[idx]);
     printf("\n To End Program Press Q\n");
   
}

int main(void)
{
     int menuChoice;
     char again;
     float amount;
     float result;
     float conversion;
     menu ();
     return (0);
}

Now you can do away with all of the case and most of the if() statements.  You simply index into CurrencyName and CurrencyRate to get the name and conversion rate.  (You should perform sanity checks to make sure that your index is within bounds.)


Good Luck,
Kent
0
 

Author Comment

by:macshawn
ID: 12051011
thaks for the help from both of you, I just need a little push in the correct direction and you provided.
cheers
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

Author Comment

by:macshawn
ID: 12051066
Hey, Kent I cannot get what you wrote above to input anything? what am I doing wrong, using a simple miracleC compiler had to add getch() to get it to display menu, but now when I hit enter after the number, nothing happens, just closes, tried adding another getch() but did not work.
Another push in the correct direction please.
shawn

0
 

Author Comment

by:macshawn
ID: 12052424
or anyone that know the answer, thanks
shawn
0
 
LVL 45

Expert Comment

by:Kdo
ID: 12053271
Hi Macshawn,

In the posted code, you getchar() and ignore the input, then input a value that you intend to use.  (See the code snippet below from your original program.)

Your first getchar() should probably read the data that you want to interpret.

Good Luck,
Kent



          if(menuChoice <= 5)
          {
             printf("You choose %d\n" , menuChoice);
             printf("%.2f  " ,amount);
               printf("%.2f\n",conversion, "of currency ", result);
               getchar();
               again = getchar();
          }
0
 

Author Comment

by:macshawn
ID: 12058032
Hi Kent, sorry but you lost me, I'm using the getch() to delay the screen so I can see the output, the only way I know how.  
In the program confused about how to combine your suggestions with my program above, so more assistence is appreciated.
thanks
shawn
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 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

16 Experts available now in Live!

Get 1:1 Help Now