Solved

trying to figure out how to print out menu choices

Posted on 2004-09-13
8
257 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thaks for the help from both of you, I just need a little push in the correct direction and you provided.
cheers
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:macshawn
Comment Utility
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
Comment Utility
or anyone that know the answer, thanks
shawn
0
 
LVL 45

Expert Comment

by:Kdo
Comment Utility
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
Comment Utility
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

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

744 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

19 Experts available now in Live!

Get 1:1 Help Now