• C

trying to figure out how to print out menu choices

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;
}
macshawnAsked:
Who is Participating?
 
CmdrRickHunterConnect With a Mentor Commented:
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
 
macshawnAuthor Commented:
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
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
macshawnAuthor Commented:
thaks for the help from both of you, I just need a little push in the correct direction and you provided.
cheers
0
 
macshawnAuthor Commented:
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
 
macshawnAuthor Commented:
or anyone that know the answer, thanks
shawn
0
 
Kent OlsenData Warehouse Architect / DBACommented:
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
 
macshawnAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.