Menu Help

This is a discussion on Menu Help within the C Programming forums, part of the General Programming Boards category; Ive got this menu I created... Code: int MainMenu(int c) { printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s", "-MAIN MENU-", "1. Initialize the list by reading ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Menu Help

    Ive got this menu I created...

    Code:
    int MainMenu(int c)
    { 
     printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
     "-MAIN MENU-",
     "1. Initialize the list by reading in strings from the input and adding one by one to the linked list.",
     "2. Print all the strings in the list.",
     "3. Remove a string from a list (interactively).",
     "4. Add a string to the list (interactively).",
     "5. Search for a string in the list.",
     "6. Make a deep copy of the list.",
     "7. Concatenate two lists.",
     "8. Destory the list. A new list can be created by selecting 1.",
     "9. Quit.\n");
     
     scanf("%d", &c);
     
     return c;
    }
    
    
     int main()
    {
     struct Node * st = NULL;
     String string;
     int choice; 
     char yon;
     
     MainMenu(choice);
     
     if (choice == 1)
     {
    	  yon = 'y';
    
    	  if (yon == 'y')
    	  {
    	  printf("\n%s", "Please enter a string: ");
          scanf("%s", &string);
          AddNode(&st, string);
    	  printf("\n%s", "Would you like to enter another, y or n?");
    	  scanf("%c", &yon);
    	  } 
    	  else 
    	  {
    		 MainMenu(choice);
    	  }
      }
      
     else if (choice == 2) 
     {
    	 printf("\n%s", "Your list contains:\n");
         PrintList(st);
    	 MainMenu(choice);
     }
     
     else if (choice == 3) {printf("\n%s", "You picked 3."); }
     
     else if (choice == 4) 
     {
    	 printf("\n%s", "Please enter a string: ");
         scanf("%s", &string);
         AddNode(&st, string);
    	 MainMenu(choice);
     }
     
     else if (choice == 5) {printf("\n%s", "You picked 5."); }
     
     else if (choice == 6) {printf("\n%s", "You picked 6."); }
     
     else if (choice == 7) {printf("\n%s", "You picked 7."); }
     
     else if (choice == 8) 
     {
    	 DestroyList(&st);
    	 printf("\n%s", "Your list has been destroyed. To create another, simply chose choice 1."); 
    	 MainMenu(choice);
     }
     
     else 
     {
     printf("\n%s", "Bye Bye!");
     return 0;
     }
    }
    Now I am having three problems...

    1. When it executes, it runs the menu, but no matter what choice I choose, it always exits. I think the initiation of the variable "choice" is the problem because if I declare it in the main as one of the choices, say "int choice = 1;" then it will run and no matter what choice I chose, it runs choice 1. I dont think it is getting the choice back from the MainMenu class. Please advise.

    2. In choice 1, right after the user enters the string, it asks the user if they want to enter another one. But insted of letting them type a choice, the program exits. Not sure whats up with that. Please advise.

    3. I dont know how, but after a given choice is complete, I need the program to go back to the main menu. I think that the way I have it set up works, but I am not sure, for I havent been able to get that far enough through the execution to see. Please advise.
    Last edited by CJ7Mudrover; 03-08-2004 at 10:56 AM.

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You have set up the MainMenu() function as taking one parameter of type int:

    Code:
    int MainMenu(int c)
    When you actually call the function, you pass it the argument 'choice' like so:

    Code:
    MainMenu(choice);
    Is there any value stored in the variable choice? It looks as though you have declared the variable, but not initialised it. And even if you had initialised it, what is the purpose of passing that value to the function?

    It looks like maybe you wish to assign the return value of the MainMenu() function to the variable choice, something like this:

    Code:
    choice = MainMenu();
    If this is how you wish to use your MainMenu() function, then you can make your parameter list like this:

    Code:
    int MainMenu(void)
    ~/

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Great. Thanks for the reply and input. I was able to incorporate what you said along with a do while loop to get the menu to come back up after each choice. Now, for another question...

    I have a print fuction as shown below...

    Code:
    void PrintList(struct Node * st)
    {
        struct Node * next = st;
     
        while(next != NULL)
        {
            printf("%c\n", next->str);
         
            next = next->next;
        } 
    }
    The program I am writing is modifying a linked list full of strings. When the program calls this print function, insted of printing each string, it prints the first characters of each string stored in the linked list. I know why, and that is becuase of the "%c" which is the character type. But the "%s" for the string type does not work, it makes my program crash. I declared type String with this struct...

    Code:
    typedef struct
    {
        char *str;
        int len;
    } String;
    Is there some special way I need to declare this output type or perhaps parse it somehow?

  4. #4
    Registered User Xzyx987X's Avatar
    Join Date
    Sep 2003
    Posts
    107
    Your strings are null terminated aren't they?

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    A couple o' things that always catch ppl out here is memory allocation for your c-string, and also making sure it's null terminated. Post the whole thing and I'll give it a thorough look and tell you what's wrong where.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Ok, here is the beast...

    Code:
    #include <stdio.h>
    
    typedef struct
    {
        char *str;
        int len;
    } String;
    
    struct Node 
    {
        struct Node * next;
        String str;
    };
    
    
    struct Node * AllocNode(String string)
    {
        struct Node * node = (struct Node *)malloc(sizeof(struct Node));
     
        if(node != NULL)
        { 
             node->next = 0;
                  node->str = string;
        }
        
     return node;    
    }
    
    
    void DestroyList(struct Node ** st)
    {
        struct Node * next, * prev = *st;
     
        while(prev != NULL)
        {
             next = prev->next;
             free(prev);
             prev = next;
        } 
        
     *st = 0;   
    }
    
    
    struct Node * AddNode(struct Node ** st, String string)
    {
        struct Node * next = *st;
        struct Node * node = AllocNode(string);
     
        if(*st != NULL)
        {
            while(next->next != NULL)
            {
                      next = next->next;
            }
            
            next->next = node;     
        } 
        else
        {
            *st = node;
        }  
        
         return node;
    }
    
    
    void PrintList(struct Node * st)
    {
        struct Node * next = st;
     
        while(next != NULL)
        {
            printf("%c\n", next->str);
         
            next = next->next;
        } 
    }
    
    int MainMenu(void)
    { 
     int c = 0;
     printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
     "-MAIN MENU-",
     "1. Initialize the list by reading in strings from the input and adding one by one to the linked list.",
     "2. Print all the strings in the list.",
     "3. Remove a string from a list (interactively).",
     "4. Add a string to the list (interactively).",
     "5. Search for a string in the list.",
     "6. Make a deep copy of the list.",
     "7. Concatenate two lists.",
     "8. Destory the list. A new list can be created by selecting 1.",
     "9. Quit.\n");
     
     scanf("%d", &c);
     
     return c;
    }
    
     int main()
    {
     struct Node * st = NULL;
     String string;
     int choice = 0;
     char yon = 'y';
    
     choice = MainMenu();
     
     do
     {
     if (choice == 1)
     {
    	  printf("%s", "Please enter a string: \n");
    	  scanf("%s", &string);
    	  AddNode(&st, string);
       
        do
    	  {
    	       printf("%s", "Please enter a string: \n");
    	       scanf("%s", &string);
    	       AddNode(&st, string);
    	       printf("%s", "Would you like to enter another string? y or n?\n");
    	       scanf("%c", &yon);
    	  } 
    	  while (yon != 'n');
    	  choice = MainMenu();
      }
      
     else if (choice == 2) 
     {
    	 printf("%s", "Your list contains:\n");
         PrintList(st);
    	 choice = MainMenu();
     }
     
     else if (choice == 3) {printf("\n%s", "You picked 3.\n"); }
     
     else if (choice == 4) 
     {
    	 printf("%s", "Please enter a string: \n");
         scanf("%s", &string);
         AddNode(&st, string);
    	 choice = MainMenu();
     }
     
     else if (choice == 5) {printf("\n%s", "You picked 5.\n"); }
     
     else if (choice == 6) {printf("\n%s", "You picked 6.\n"); }
     
     else if (choice == 7) {printf("\n%s", "You picked 7.\n"); }
     
     else if (choice == 8) 
     {
    	 DestroyList(&st);
    	 printf("%s", "Your list has been destroyed. To create another, simply chose choice 1.\n"); 
    	 choice = MainMenu();
     }
     }
     while (choice != 9);
     return 0;
    }

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >scanf("%s", &string);
    You do this a lot. It suggests that you don't quite understand your code. string is an instance of the String struct, not a char *. You want
    Code:
    scanf("%s", string.str);
    Of course, you still haven't allocated memory to string.str, which is a pointer.

    >node->str = string;
    You're copying by value, but the pointer still points to the original memory from string in main. Your list will not print as you expect it to. You need to make a deep copy of the pointers so that they aren't lost when a new string is entered.

    >struct Node * node = (struct Node *)malloc(sizeof(struct Node));
    Don't cast malloc, it's hiding the fact that you forgot to include stdlib.h.

    The following works for options 1 and 2. I didn't test the others:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct
    {
      char *str;
      int len;
    } String;
    
    struct Node 
    {
      struct Node * next;
      String str;
    };
    
    
    struct Node * AllocNode(String string)
    {
      struct Node * node = (struct Node *)malloc(sizeof(struct Node));
    
      if(node != NULL)
      { 
        node->next = 0;
        node->str.len = strlen ( string.str );
        node->str.str = malloc ( node->str.len + 1 );
        if ( node->str.str != NULL )
          strcpy ( node->str.str, string.str );
      }
    
      return node;    
    }
    
    
    void DestroyList(struct Node ** st)
    {
      struct Node * next, * prev = *st;
    
      while(prev != NULL)
      {
        next = prev->next;
        free(prev);
        prev = next;
      } 
    
      *st = 0;   
    }
    
    
    struct Node * AddNode(struct Node ** st, String string)
    {
      struct Node * next = *st;
      struct Node * node = AllocNode(string);
    
      if(*st != NULL)
      {
        while(next->next != NULL)
        {
          next = next->next;
        }
    
        next->next = node;     
      } 
      else
      {
        *st = node;
      }  
    
      return node;
    }
    
    
    void PrintList(struct Node * st)
    {
      struct Node * next = st;
    
      while(next != NULL)
      {
        printf("%s\n", next->str.str);
    
        next = next->next;
      } 
    }
    
    int MainMenu(void)
    { 
      int c = 0;
      printf("\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",
        "-MAIN MENU-",
        "1. Initialize the list by reading in strings from the input and adding one by one to the linked list.",
        "2. Print all the strings in the list.",
        "3. Remove a string from a list (interactively).",
        "4. Add a string to the list (interactively).",
        "5. Search for a string in the list.",
        "6. Make a deep copy of the list.",
        "7. Concatenate two lists.",
        "8. Destory the list. A new list can be created by selecting 1.",
        "9. Quit.\n");
    
      scanf("%d", &c);
    
      return c;
    }
    
    int main()
    {
      struct Node * st = NULL;
      String string;
      int choice = 0;
      char yon = 'y';
      char buff[BUFSIZ];
    
      string.str = buff;
      choice = MainMenu();
    
      do
      {
        if (choice == 1)
        {
          do
          {
            printf("%s", "Please enter a string: \n");
            scanf("%s", string.str);
            AddNode(&st, string);
            printf("%s", "Would you like to enter another string? y or n?\n");
            scanf(" %c", &yon);
          } 
          while (yon != 'n');
          choice = MainMenu();
        }
    
        else if (choice == 2) 
        {
          printf("%s", "Your list contains:\n");
          PrintList(st);
          choice = MainMenu();
        }
    
        else if (choice == 3) {printf("\n%s", "You picked 3.\n"); }
    
        else if (choice == 4) 
        {
          printf("%s", "Please enter a string: \n");
          scanf("%s", string.str);
          AddNode(&st, string);
          choice = MainMenu();
        }
    
        else if (choice == 5) {printf("\n%s", "You picked 5.\n"); }
    
        else if (choice == 6) {printf("\n%s", "You picked 6.\n"); }
    
        else if (choice == 7) {printf("\n%s", "You picked 7.\n"); }
    
        else if (choice == 8) 
        {
          DestroyList(&st);
          printf("%s", "Your list has been destroyed. To create another, simply chose choice 1.\n"); 
          choice = MainMenu();
        }
      }
      while (choice != 9);
      return 0;
    }
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Prelude...once again. Thanks a bunch. Got it fixed.

    It suggests that you don't quite understand your code.
    Some of this code was given as examples in class. I have been trying my best to modify it for my case. The example code was for linked list of ints, so go figure. If you havent noticed, I am new to programming in C. I can do java ok, but I am pretty much having to teach myself C. I have no idea why but this prof wants us to do both, even know the class that preceeds is a java class. They have some similarities, but many differences. I really do appreciate the help and patience from everyone.

    Will be working on the other methods tomorrow. I will post here if I have any problems.

    -B

    P.S. - I think im doing pretty well with it, speaking for I just started using C for the first time about 6 weeks ago Thanks again everyone!

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, 04: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, 02:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 04:41 AM

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