c menu

This is a discussion on c menu within the C Programming forums, part of the General Programming Boards category; in c i need to have a menu. pretty much blah blah blah then after all that executes you prompt ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    19

    c menu

    in c i need to have a menu.

    pretty much

    blah blah blah
    then after all that executes you prompt for: Go again (y,n) then repeat the above steps.

    I want to do it in a DO function btw. I have tried while with strcmp but it only works once then the program quits.

    any help?


    I can do it with numbers but i cannot get it working for chars.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ok.. well do is a control statement not a function, first of all. And secondly, a little code goes a long way. Put what you have and I will correct it.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Code is below


    char choice[2], choice1[2];

    choice1[2]='y';

    ........................


    printf("Another? (y/n)");
    scanf("%s", choice);

    while(strncmp(choice,choice1,1)==0)
    { .......................

    printf("Another? (y/n)");
    scanf("%s", choice);
    }

    system("PAUSE");
    return 0;



    ;

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Cornedbee would probably break a candy dish over your head for this...

    Errors in red
    Code:
    char choice[2], choice1[2];
    
    choice1[2]='y'; /* Buffer overflow here. */
    
    ........................
    
    
    printf("Another? (y/n)");
    scanf("%s", choice); /* This isn't wrong, but  its not customarily how one would do it */
    
    while(strncmp(choice,choice1,1)==0) /* Odd choice */
    { .......................
    
    printf("Another? (y/n)");
    scanf("%s", choice);
    }
    
    system("PAUSE");
    return 0;
    
    
    
    ;
    So lets see why I am breaking out the red. When you make an array your arrays are going to be filled with undefined data at first, and they are also going to start at index 0. So they go from 0 to n-1. So if you declare an array like this:

    Example:
    Code:
    char array[50];
    Then you can access from 0 to 49 (which is 50 - 1). If you are only comparing one char of string a and one char of string b, why not just directly compare them?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void)
    {
      char c;
    
      fputs("I am a program that doesn't do very much.\n", stdout);
    
      do
      {
        fputs("Do you wish to continue? (Y/N)", stdout);
        c = toupper(getchar());
    
        switch(c)
        {
           case 'Y':
             continue;
           case 'N':
             c = EOF;
             break;
           default:
             printf("I said Y/N not &#37;c! You will be punished for your insubordination!\n", c);
             while(1) { }
        }
      } while(c != EOF);
    
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    question. Is it a way to do a char to char compare.

    so say that i have

    scanf("&#37;c", &a)

    scanf("%c",&b)

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char a, b;
    
      scanf("&#37;c%c", &a, &b);
    
      if(a == b)
        puts("True");
      else
        puts("False");
    
      return 0;
    }

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    see i had that down and it wasnt working. still trying here though

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    ok why is it that the below works with the space in the scanf. In my program without the space it skipped the scanf and continued to loop.

    Code:
    do
    {
    ............
    
    scanf(" &#37;c", &a);
    }while(a!='n'
    Last edited by Salem; 10-06-2008 at 11:16 PM. Reason: Use [code] tags for code, not [quote] tags

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because space is a perfectly good character that can be read in, unless you tell scanf not to by adding a space to the format string.

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    &#37;c is one of the trickier to use scanf() flags. That is why you may have noticed that in my code I did not use it. Though mine would still suffer from the fact that it would accept ' ' as a the character the user typed.

    You can always do something like:

    Code:
    char a[2];
    
    scanf("%1s", a);
    Which should be cool about not picking up leading spaces.

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    philippiines
    Posts
    10

    Try this one, ihope this will help much!!!

    Code:
    #include<stdio.h>
    main()
    {
    char b;//variable for loop to be test
    int a;//variable for choice
    do
    {
    statement seq;//write your menus here!!
    printf("\n\nenter choice:");//ask for the user to choose one of your menu
    scanf("%d",&a);
    switch(a)
    {
    case 1:
    {
    statement seq;
    break;
    }
    case 2:
    {
    statement seq;
    break;
    }
    defeult:
    {
    printf("\n\ninvalid choice!!!");//tells the user that they enter invalid choice from the menu
    }
    }//end of switch
    printf("\nDo you want to try another?\ninput y if yes:");//ask the user if he/she wants to try another
    scanf("%s",&b);//addressing variable to be test, i used %s because string is much better to use than the character
    }while(b=='y'||b=='Y');
    return 0;
    }//end of main block!!

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, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21