Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,566 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,646 people online right now. Registration is fast and FREE... Join Now!




C Menu help

 
Reply to this topicStart new topic

C Menu help, Need to insert a menu for the user to pick the store they want calcual

duece
25 Nov, 2007 - 09:22 AM
Post #1

New D.I.C Head
*

Joined: 24 Nov, 2007
Posts: 3


My Contributions
CODE
#include <stdio.h>
#include <ctype.h>

int main(void)
{
//entering the variables in float
float totalsale = 0.0;
float DelmarLocation = .0725;
float EncinitasLocation = .075;
float LajollaLocation  = .0775;
char user_input;
char cContinue = 'Y';

do
{
//Program Header
printf("\nKudler Fine Foods Tax Calculator\n");
printf ("\n--------------------------------------\n\n");
printf ("Enter total sale and press enter key:\n");
scanf("%f",& totalsale);
printf ("Is the above price correct:\n");
printf("Continue (Y/N)?\n");

scanf("  %c", &user_input );
user_input = toupper( user_input );
            
if( (user_input == 'Y') )
        {
printf ("\n--------------------------------------\n\n");
printf ("The Delmar Location sales tax: $%.2f\n", DelmarLocation * totalsale);
printf ("Delmar Location total sale: $%.2f\n", DelmarLocation * totalsale + totalsale);
printf ("\n--------------------------------------\n\n");
printf ("The Encinitas Location sales tax: $%.2f\n", EncinitasLocation * totalsale);
printf ("Encinitas Location total sale: $%.2f\n", EncinitasLocation * totalsale + totalsale);
printf ("\n--------------------------------------\n\n");
printf ("The Lajolla Location sales tax: $%.2f\n", LajollaLocation * totalsale);
printf ("Lajolla Location total sale: $%.2f\n", LajollaLocation * totalsale + totalsale);
printf ("\n--------------------------------------\n\n");

printf ("\n");
    

            
getchar(); // eat the last key in the keyboard buffer.
printf( "\n\nRun again (Y/N)? " );
scanf( "%c",&cContinue );
            
cContinue = toupper( cContinue );
}
else
{
printf("\nRe-enter price\n");
}
        
} while ( cContinue == 'Y');

return 0;
}      
//End Main Function

Mod Edit: Code tags added: code.gif
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C Menu Help
25 Nov, 2007 - 09:31 AM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
So what exactly is your question?
User is online!Profile CardPM
+Quote Post

duece
RE: C Menu Help
25 Nov, 2007 - 09:34 AM
Post #3

New D.I.C Head
*

Joined: 24 Nov, 2007
Posts: 3


My Contributions
QUOTE(NickDMax @ 25 Nov, 2007 - 10:31 AM) *

So what exactly is your question?


I have tried and done research, but I can not figure out how to do it.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C Menu Help
25 Nov, 2007 - 09:51 AM
Post #4

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
do what?
User is online!Profile CardPM
+Quote Post

duece
RE: C Menu Help
25 Nov, 2007 - 09:56 AM
Post #5

New D.I.C Head
*

Joined: 24 Nov, 2007
Posts: 3


My Contributions
QUOTE(NickDMax @ 25 Nov, 2007 - 10:51 AM) *

do what?

Have the user select from a menu which store to use for the tax calculation.
User is offlineProfile CardPM
+Quote Post

NickDMax
RE: C Menu Help
25 Nov, 2007 - 11:49 AM
Post #6

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,868



Thanked: 53 times
Dream Kudos: 550
My Contributions
Here is an example of how to create a menu system. Granted mine is a bit fancy but... well... its what I came up with:
CODE
#include <stdio.h>


//-------- Some Menu Prompts that we can use -----------
static char menuPrompts[][64] = {
        "Enter the world of tomarrow",
        "Sell your soul to Microsoft",
        "Become a dot-com billionare",
        "Invest poorly in internet fads",
        "Write a more influential book than Knuth",
        "End it all (exit program)"
};

//an enumeration listing all of the prompts...
enum prompts {
    WORLD_OF_TOMARROW, //==0
    SOUL_TO_MICROSOFT, //==1
    DOTCOM_BILLIONARE, //==2
    POOR_INTERNET_FAD, //==3
    OUT_SELL_KNUTH,    //==4
    EXIT_PROGRAM,      //==5
    NUMBER_OF_MENU_ITEMS //This "counts" how many prompts we made
};

//Define how manu tries the user gets
#define MAX_USER_TRIES 3

//--- function prototypes ----
void displayMenu(char menuItems[][64], int num);
int get_users_input(int max);
int getLine(char *buffer, int length);
void scrollScr();

int main() {
    int choice = 0;
    //Generally menus are done like this...
    do {
        //First you display the menu
        displayMenu(menuPrompts, NUMBER_OF_MENU_ITEMS);
        //Then get the user's choice
        choice = get_users_input(NUMBER_OF_MENU_ITEMS);
        //Use a control structure like a switch or if-statement to control
        // the flow of the program
        switch (choice) {
        case WORLD_OF_TOMARROW:
            {
                //here is where we would do something related to user's choice...
                printf("\nUser wants to: %s\n\n",menuPrompts[WORLD_OF_TOMARROW]);
            } break;
        case SOUL_TO_MICROSOFT:
            {
                printf("\nUser wants to: %s\n\n",menuPrompts[SOUL_TO_MICROSOFT]);
            } break;
        case DOTCOM_BILLIONARE:
            {
                printf("\nUser wants to: %s\n\n",menuPrompts[DOTCOM_BILLIONARE]);
            } break;
        case POOR_INTERNET_FAD:
            {
                printf("\nUser wants to: %s\n\n",menuPrompts[POOR_INTERNET_FAD]);
            } break;
        case OUT_SELL_KNUTH:
            {
                printf("\nUser wants to: %s\n\n",menuPrompts[OUT_SELL_KNUTH]);
            } break;
        case NUMBER_OF_MENU_ITEMS:
            {
                scrollScr();
            } break;
        }
    //The whole thing is put into a loop and we look for the exit condition(s).
    } while (choice!=EXIT_PROGRAM);
    return 0;

}

//This simply prints out a numbered list of strings
void displayMenu(char menuItems[][64], int num) {
    int i;
    for (i = 0; i < num; i++) {
        printf("% 2d. %s\n", i+1, menuItems[i]);
    }
}

//This routine gets the users input: if the user does not enter a number > 0 then
//    the user will be prompted again up to MAX_USER_TRIES times.
int get_users_input(int max) {
    int choice = 0;
    int tries = 0;
    char inBuffer[10];
    while (!choice && tries < MAX_USER_TRIES) {
        printf("\nEnter a choice (1 - %d): ", max);
        getLine(inBuffer, 10); //read in the user's input
        sscanf(inBuffer, "%d", &choice); //see if it is a valid number
        tries++;
    }
    return (tries < MAX_USER_TRIES) ? choice - 1 : max;
}

//An input routine: returns the number of characters read in.
// the routine does have a flaw though... it adds the 0x0A from the
// CRLF on windows systems... so if the user only presses 1 key then enter
// the routine returns 2.
int getLine(char *buffer, int length) {
    int i = 0;
    while(i < length && (buffer[i++] = getchar()) != '\n');
    buffer[i] = 0x00;
    return i-1;
}

//Since all the clear screen methods are platform dependant, this will
//  scroll the screen a bunch so clear away old data
void scrollScr() {
    int i;
    for (i=0; i<25; i++) puts("\n");
}


now you don't have to be so fancy, the basic idea is to print out a menu, get the user's choice, respond to the choice...
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 10:16PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month