Thread: Menu

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    3

    Menu

    This is my first attempt at writing a menu. I can't get it to return after the if statement for yes, and I can't get it to do the else statement for no if you answer no the 2nd time around. The total doesn't work either. Can someone please give me a heads up? You might want to compile it to see what I'm talking about. Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    char answer[20];
    char answer2[20];
    char choiceone[20];
    char choicetwo[20];
    float total;
    float choice1 = 0.95;
    float choice2 = 1.25;
    float choice3 = 1.00;
    
    printf("May I take your order today?\n\r");
    printf("Menu:\n\r");
    printf("Choice1: Fries .95\n\r");
    printf("Choice2: Cheese burger $1.25\n\r");
    printf("Choice3: Lemonade $1.00\n\r");
    printf("Those are your choices! Choose Choice1, Choice2 or Choice3 please.\n\r", choiceone);
    scanf("%s");
    
    if (strcmp (choiceone, "choice1"))
    {
    	total + choice1;
    }
    if (strcmp(choiceone, "choice2"))
    {
    	total + choice2;
    }
    if (strcmp (choiceone, "choice3"))
    {
    	total + choice3;
    }
    
    printf("You've chosen %s.\n\r", choiceone);
    printf("Would you like something else?\n\r", answer);
    scanf("%s", answer);
    
    if (!strcmp (answer, "yes"))
    {
    	printf("Menu:\n\r");
    	printf("Choice1: Fries .95\n\r");
    	printf("Choice2: Cheese burger $1.25\n\r");
    	printf("Choice3: Lemonade $1.00\n\r");
    	printf("Those are your choices! Choose one of those please.\n\r", choiceone);
    	scanf("%s");
    
    	printf("You've chosen %s.\n\r", choiceone);
    	scanf("%s", choiceone);
    	printf("Anything else for you today?\n\r", answer);
    	scanf("%s");
    	return;
    }
    
    else if (!strcmp (answer, "no"))
    {
    	printf("Your total comes to %f", &total);
    }
    
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    use the function scanf in form correct:
    this correct is:
    scanf("%s",str); //for strings
    scanf("%d",&d); //for integers and floats and doubles

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    char answer[20];
    char answer2[20];
    char choiceone[20];
    char choicetwo[20];
    float total;
    float choice1 = 0.95;
    float choice2 = 1.25;
    float choice3 = 1.00;
    
    printf("May I take your order today?\n\r");
    printf("Menu:\n\r");
    printf("Choice1: Fries .95\n\r");
    printf("Choice2: Cheese burger $1.25\n\r");
    printf("Choice3: Lemonade $1.00\n\r");
    printf("Those are your choices! Choose Choice1, Choice2 or Choice3 please.\n\r", choiceone);
    scanf("%s",choiceone);
    
    if (strcmp (choiceone, "choice1"))
    {
            total + choice1;
    }
    if (strcmp(choiceone, "choice2"))
    {
            total + choice2;
    }
    if (strcmp (choiceone, "choice3"))
    {
            total + choice3;
    }
    
    printf("You've chosen %s.\n\r", choiceone);
    printf("Would you like something else?\n\r", answer);
    scanf("%s", answer);
    
    if (!strcmp (answer, "yes"))
    {
            printf("Menu:\n\r");
            printf("Choice1: Fries .95\n\r");
            printf("Choice2: Cheese burger $1.25\n\r");
            printf("Choice3: Lemonade $1.00\n\r");
            printf("Those are your choices! Choose one of those please.\n\r", choiceone);
            scanf("%s",choiceone);
    
            printf("You've chosen %s.\n\r", choiceone);
            scanf("%s", choiceone);
            printf("Anything else for you today?\n\r", answer);
            scanf("%s",choicetwo);
            return;
    }
    
    else if (!strcmp (answer, "no"))
    {
            printf("Your total comes to %f", total);
    }
    
    return 0;
    }
    /**************Um abraço*************/

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    3
    im pretty sure i got the scanf's right, read my initialized numbers/strings...

    %d is for an integer whole number
    %f is a floating point number
    %c single character
    %s string of chars
    %p a memory address

    But thanks for your time
    Last edited by <3pi; 03-15-2005 at 11:37 AM.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
       char answer[20];
       char answer2[20];
       char choiceone[20];
       char choicetwo[20];
       float total;
       float choice1 = 0.95;
       float choice2 = 1.25;
       float choice3 = 1.00;
    
       printf("May I take your order today?\n\r");
       printf("Menu:\n\r");
       printf("Choice1: Fries .95\n\r");
       printf("Choice2: Cheese burger $1.25\n\r");
       printf("Choice3: Lemonade $1.00\n\r");
       printf("Those are your choices! Choose Choice1, Choice2 or Choice3 please.\n\r", choiceone);
    /*
    Too many arguments for format (1 too many)
    */
       scanf("%s");
    /*
    Too few arguments for format (1 missing)
    */
    
       if ( strcmp (choiceone, "choice1") )
       {
          total + choice1;
    /*
    Code has no effect in function main
    Possible use of 'total' before definition in function main
    */
       }
       if ( strcmp(choiceone, "choice2") )
       {
          total + choice2;
    /*
    Code has no effect in function main
    */
       }
       if ( strcmp (choiceone, "choice3") )
       {
          total + choice3;
    /*
    Code has no effect in function main
    */
       }
    
       printf("You've chosen %s.\n\r", choiceone);
       printf("Would you like something else?\n\r", answer);
    /*
    Too many arguments for format (1 too many)
    */
       scanf("%s", answer);
       if ( !strcmp (answer, "yes") )
       {
          printf("Menu:\n\r");
          printf("Choice1: Fries .95\n\r");
          printf("Choice2: Cheese burger $1.25\n\r");
          printf("Choice3: Lemonade $1.00\n\r");
          printf("Those are your choices! Choose one of those please.\n\r", choiceone);
    /*
    Too many arguments for format (1 too many)
    */
          scanf("%s");
    /*
    Too few arguments for format (1 missing)
    */
    
          printf("You've chosen %s.\n\r", choiceone);
          scanf("%s", choiceone);
          printf("Anything else for you today?\n\r", answer);
    /*
    Too many arguments for format (1 too many)
    */
          scanf("%s");
    /*
    Too few arguments for format (1 missing)
    */
          return;
    /*
    function 'main(void)' should return a value
    */
       }
    
       else if ( !strcmp (answer, "no") )
       {
          printf("Your total comes to %f", &total);
    /*
    Size of argument no. 2 inconsistent with format
    */
       }
    
       return 0;
    }
    /*
    'choicetwo' is declared but never used in function main
    'answer2' is declared but never used in function main
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User dinjas's Avatar
    Join Date
    Feb 2005
    Location
    Vancouver, Washington
    Posts
    40
    Quote Originally Posted by <3pi
    Code:
    printf("Those are your choices! Choose Choice1, Choice2 or Choice3 please.\n\r", choiceone);
    scanf("%s");
    
    if (strcmp (choiceone, "choice1"))
    I believe strcmp is case sensitive, so it might not be a bad idea to prompt your user to enter a choice that begins with a lowercase C since that is what you're checking for. (i.e. Choice != choice)
    Also, you are not using scanf properly. You're scanning in a string, fine. But where are you putting it?
    Oh, the \r's aren't necessary on the printf's either.

    dinjas
    straight off the heap

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM