Like Tree1Likes

Help in dynamic and Linked Lists

This is a discussion on Help in dynamic and Linked Lists within the C Programming forums, part of the General Programming Boards category; hi Im trying to create a program that would be able to carry out a certain number of tasks any ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    22

    Help in dynamic and Linked Lists

    hi
    Im trying to create a program that would be able to carry out a certain number of tasks any number of times undefined by the user! I was told to use linked list or dynamic arrays but i dont know how to use them propers

    any ideas?
    help please!

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Can you give us some more information about what you are trying to do?
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by TheBigH View Post
    Can you give us some more information about what you are trying to do?

    Its basically a database for a sports day! I start with a menu to either, as mentioned before, input, output, sort and searching. I have just started and i only know the basics! So i tried to put some of my knowledge together.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct
    {
            char name[50];
            char fname[50];
            char lname[70];
            int age;
            char gender[6];
            int group;        
            int position;
    } Individual;
    
    int main()
    {
        Individual I[1];
        void menu(Individual I[]);
        menu(I);
        void input(Individual I[]);
        void output(Individual I[]);
        void sort(Individual I[]);
        void s_age(Individual I[]);
        void search();    
    }
         
    int count =0;
    int *in
    void menu(Individual I[])
    {
         int opt;
         do{  
               printf("Choose what you want to do\n\n");
               printf("1.) Enter information\n2.) View information\n");
               printf("3.) Sort information\n4.) Search information\n");
               printf("5.) Exit Program\n\n\n");
               printf("Enter option: ");
               scanf("%d", &opt);
               system("CLS");
               switch(opt)
               {
                          case 1:
                               input(I);
                               break;
                          
                          case 2:
                               output(I);
                               break;
                          
                          case 3:
                               sort(I);
                               break;
                          
                          case 4:
                               search();
                               break;
                               
                          case 5:
                               exit(0);
                               break;
                          
                          default:
                                  printf("INVALID OPTION...PLEASE RE-ENTER...");
                                  printf("\n\n"); 
                                  printf("Press any key to re-enter...");
                                  getch();     
                                  system("CLS");                        
               }
         }while(opt !=5);
    }  
    
    
    int input(Individual I[])
    {   
        int x, i, choice;
        for(x=0; x<1; x++)
        {
          printf("Enter FIRST name: ");
          scanf("%s", I[x].fname);      
          printf("\nEnter LAST name: ");
          scanf("%s", I[x].lname);       
          printf("\nEnter age: ");
          scanf("%d", &I[x].age);      
          printf("\nEnter gender[male/female]: ");
          scanf("%s", I[x].gender);
          printf("\nEnter group[1,2,3,4]: ");
          scanf("%d", &I[x].group);
          printf("\nEnter position[1,2,3]: ");
          scanf("%d", &I[x].position);
          system("PAUSE");
          system("CLS");
          
          for (x=0;x<1;x++)
          {
              printf("Name: %s %s\n\n",I[x].fname,I[x].lname);
              printf("Age: %d\n\n", I[x].age);
              printf("Gender: %s\n\n", I[x].gender);
              printf("Group: %d\n\n", I[x].group);
              printf("Position: %d\n\n\n",I[x].position);
              
              }
          printf("\nIs the data correct?\n\n");
          printf("1.]Yes\n2.]No\n\nChoice: ");
          scanf("%d", &choice);
          system("CLS");
          if(choice == 1){
                     count++; 
                     printf("%d", count); 
                     in = &count                               
                    void files(Individual I[]);
                    files(I);  
                                                        
                    }
          else{
               printf("Please re-enter data, correctly...\n\n");
               input(I);
               }                 
    }
    
    void files(Individual I[])
    {
         int x;
         FILE*ptr=fopen("Reg.txt", "a");
         for(x=0;x<1;x++)
         {
          fprintf(ptr, "%s\n", I[x].fname);
          fprintf(ptr, "%s\n", I[x].lname);
          fprintf(ptr, "%d\n", I[x].age);
          fprintf(ptr, "%s\n", I[x].gender);
          fprintf(ptr, "%d\n", I[x].group);
          fprintf(ptr, "%d\n\n", I[x].position);
          
         }
             fclose(ptr);
    }
    
    int output(Individual I[])
    {
        FILE*ptr=fopen("Reg.txt", "r");
        int a;
        for(a=0;a<*in; a++)
        {
            fscanf(ptr,"%s",I[a].fname);
            fscanf(ptr,"%s",I[a].lname);
            fscanf(ptr,"%d",&I[a].age);
            fscanf(ptr,"%s",I[a].gender);
            fscanf(ptr,"%d",&I[a].group);
            fscanf(ptr,"%d",&I[a].position);
            
            }
        for (a=0; a<*in; a++)
        {
                 printf("%s %s\n\n", I[a].fname, I[a].lname);
                 printf("%d\n\n",I[a].age);
                 printf("%s\n\n", I[a].gender);
                  printf("%d\n\n",I[a].group);
                  printf("%d\n",I[a].position);
            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
            }
        system("PAUSE");
        system("CLS");                            
    }
    It's not advanced.
    I want the user to be able to do anything at any time.
    Im writing to a file and reading as well. Im not very good in pointers. i tried to use them!
    its only allowing me to view 2 sets of entered data, if i add more than 2, the output function crashes.
    My friend advised me to use linked lists but im unsure as to how!
    any ideas?

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    i just put the input output and files functions

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    Code:
    void menu(Individual I[]);
    void input(Individual I[]);
    void output(Individual I[]);
    void sort(Individual I[]);
    void s_age(Individual I[]);
    void search();    
    
    
    
    int main()
    {
        Individual I[1];
        menu(I);
    }
    Your function prototypes should be above main.

    On line 28, you have forgotten a semicolon
    Fact - Beethoven wrote his first symphony in C

  6. #6
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Firstly, do not use system calls like clr or pause as they can introduce security concerns and make your code not interchangeable between OS's. Why system() is evil - C++ Forum That link is from a C++ website, but the ideas are the same, so don't hold the language against them! (I kid). Secondly, all you need is a while loop inside your main that prompts the user with your menu of options and loops until the user inputs the "exit" option.

    Code:
    #define     EXIT        '?'
    while( ( option = getoption() ) != EXIT )
    {
    //do stuff here
    }

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    You have written 162 lines but the program doesn't compile because of several errors. That's not a good method to develop a program. You should work step by step and compile/test often to catch errors as soon as possible.

    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by jwroblewski44 View Post
    Firstly, do not use system calls like clr or pause as they can introduce security concerns and make your code not interchangeable between OS's. Why system() is evil - C++ Forum That link is from a C++ website, but the ideas are the same, so don't hold the language against them! (I kid). Secondly, all you need is a while loop inside your main that prompts the user with your menu of options and loops until the user inputs the "exit" option.

    Code:
    #define     EXIT        '?'
    while( ( option = getoption() ) != EXIT )
    {
    //do stuff here
    }
    the menu function i am using works fine..ok its like this. When i enter 2 sets of info (input function) the outputs works fine show the 2 sets of info! however if and add another set of info (now 3 sets) the output function crashes! I have no idea why! do i need my struct count to keep incrementing and how?

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by AndiPersti View Post
    You have written 162 lines but the program doesn't compile because of several errors. That's not a good method to develop a program. You should work step by step and compile/test often to catch errors as soon as possible.

    Bye, Andreas
    yes i realized :/ thanks but i rushed to put the code online so i didnt check it over properly :/

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by joeymaharaj View Post
    the menu function i am using works fine..ok its like this. When i enter 2 sets of info (input function) the outputs works fine show the 2 sets of info! however if and add another set of info (now 3 sets) the output function crashes! I have no idea why!
    And I have no idea how you are even able to run the program because your compiler shouldn't accept it:
    Code:
    $ make foo
    cc -ggdb3 -Wall -Wextra   -c -o foo.o foo.c
    foo.c:29:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘void’
    foo.c:73:5: error: conflicting types for ‘input’
    foo.c:20:10: note: previous declaration of ‘input’ was here
    foo.c: In function ‘input’:
    foo.c:109:18: error: ‘in’ undeclared (first use in this function)
    foo.c:109:18: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:110:17: error: expected ‘;’ before ‘void’
    foo.c:111:17: warning: implicit declaration of function ‘files’ [-Wimplicit-function-declaration]
    foo.c: In function ‘output’:
    foo.c:162:1: warning: no return statement in function returning non-void [-Wreturn-type]
    foo.c: In function ‘input’:
    foo.c:162:1: error: expected declaration or statement at end of input
    foo.c:75:12: warning: unused variable ‘i’ [-Wunused-variable]
    foo.c:162:1: warning: control reaches end of non-void function [-Wreturn-type]
    foo.c: In function ‘main’:
    foo.c:25:1: warning: control reaches end of non-void function [-Wreturn-type]
    make: *** [foo.o] Error 1
    I think it's time to think about getting better tools.

    Bye, Andreas

  11. #11
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    You have your function prototypes inside of your main. Incorrect, just like Click_here stated above. And when they are pulled out and placed in the correct position, you have nothing in your main. Then, according to my previous post, you need a while loop, continually getting options from the user, manipulating the data as needed, until the option accepted is the one that signals a program exit.

    Post your revised code for us to help further.

  12. #12
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by jwroblewski44 View Post
    You have your function prototypes inside of your main. Incorrect, just like Click_here stated above. And when they are pulled out and placed in the correct position, you have nothing in your main. Then, according to my previous post, you need a while loop, continually getting options from the user, manipulating the data as needed, until the option accepted is the one that signals a program exit.

    Post your revised code for us to help further.
    ok i put the function prototypes outside! and it seem to be running properly for 2 sets of data ONLY :/ not more!
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    typedef struct
    {
            char name[50];
            char fname[50];
            char lname[70];
            int age;
            char gender[6];
            int group;        
            int position;
    } Individual;
    
    void menu(Individual I[]);
    void input(Individual I[]);
    void output(Individual I[]);
    
    int main()
    {
        Individual I[1];
        menu(I);       
    }
       
    int count =0;
    
    void menu(Individual I[])
    {
         int opt;
         do{  
               printf("Choose what you want to do\n\n");
               printf("1.) Enter information\n2.) View information\n");;
               printf("3.) Exit Program\n\n\n");
               printf("Enter option: ");
               scanf("%d", &opt);
               system("CLS");
               switch(opt)
               {
                          case 1:
                               input(I);
                               break;
                          
                          case 2:
                               output(I);
                               break;
                               
                          case 3:
                               exit(0);
                               break;
                          
                          default:
                                  printf("INVALID OPTION...PLEASE RE-ENTER...");
                                  printf("\n\n"); 
                                  printf("Press any key to re-enter...");
                                  getch();                             
               }
         }while(opt !=3);
    }  
    
    
    void input(Individual I[])
    {   
        int x, i, choice;
        for(x=0; x<1; x++)
        {
          printf("Enter FIRST name: ");
          scanf("%s", I[x].fname);      
          printf("\nEnter LAST name: ");
          scanf("%s", I[x].lname);       
          printf("\nEnter age: ");
          scanf("%d", &I[x].age);      
          printf("\nEnter gender[male/female]: ");
          scanf("%s", I[x].gender);
          printf("\nEnter group[1,2,3,4]: ");
          scanf("%d", &I[x].group);
          printf("\nEnter position[1,2,3]: ");
          scanf("%d", &I[x].position);
          printf("DONE Press Enter\n\n");
          getch();
          }
          
          for (x=0;x<1;x++)
          {
              printf("Name: %s %s\n\n",I[x].fname,I[x].lname);
              printf("Age: %d\n\n", I[x].age);
              printf("Gender: %s\n\n", I[x].gender);
              printf("Group: %d\n\n", I[x].group);
              printf("Position: %d\n\n\n",I[x].position);
              
              }
          printf("\nIs the data correct?\n\n");
          printf("1.]Yes\n2.]No\n\nChoice: ");
          scanf("%d", &choice);
          system("CLS");
          if(choice == 1){
                     count++; 
                     printf("%d", count);                                
                    void files(Individual I[]);
                    files(I);  
                                                        
                    }
          else{
               printf("Please re-enter data, correctly...\n\n");
               input(I);
               }                 
    }
    
    void files(Individual I[])
    {
         int x;
         FILE*ptr=fopen("Reg.txt", "a");
         for(x=0;x<1;x++)
         {
          fprintf(ptr, "%s\n", I[x].fname);
          fprintf(ptr, "%s\n", I[x].lname);
          fprintf(ptr, "%d\n", I[x].age);
          fprintf(ptr, "%s\n", I[x].gender);
          fprintf(ptr, "%d\n", I[x].group);
          fprintf(ptr, "%d\n\n", I[x].position);
          
         }
             fclose(ptr);
    }
    
    void output(Individual I[])
    {
        FILE*ptr=fopen("Reg.txt", "r");
        int a;
        for(a=0;a<count; a++)
        {
            fscanf(ptr,"%s",I[a].fname);
            fscanf(ptr,"%s",I[a].lname);
            fscanf(ptr,"%d",&I[a].age);
            fscanf(ptr,"%s",I[a].gender);
            fscanf(ptr,"%d",&I[a].group);
            fscanf(ptr,"%d",&I[a].position);
            
            }
        for (a=0; a<count; a++)
        {
                 printf("%s %s\n\n", I[a].fname, I[a].lname);
                 printf("%d\n\n",I[a].age);
                 printf("%s\n\n", I[a].gender);
                  printf("%d\n\n",I[a].group);
                  printf("%d\n",I[a].position);
            printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
            }
        getch();                           
    }

  13. #13
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    You have a loop inside your menu function that only accepts valid options, which is good. But your menu function should not contain calls to your input or output functions. Your menu function should return the option entered by the user, which is then used inside your main function. It should contain the menu portion, and nothing else. This way will be less misleading as per the names of the functions and what they actually do. On a side note, I am still unable to compile your code. How are you running it?

    You also still have system calls inside your code.

    What I would do is, (yep!) start over. Like AndiPersti said, focus on individual segments of the program like inputting the data into the data structure and successfully reading it back out to stdout instead of a file. You said you would like to be able to enter an undefined amount of "records", this implies the use of linked lists. This seems a little out of your reach at this time, so focus on using your static sized array. Most importantly, you need to think about code stability. As in, not writing out of bounds in arrays, valid type of data, so on...

    Avoid global variables unless absolutely necessary.
    http://c2.com/cgi/wiki?GlobalVariablesAreBad
    Last edited by jwroblewski44; 02-26-2013 at 05:15 PM.

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,500
    Line 98 is a function prototype, not a function call.
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    Feb 2013
    Posts
    22
    Quote Originally Posted by jwroblewski44 View Post
    You have a loop inside your menu function that only accepts valid options, which is good. But your menu function should not contain calls to your input or output functions. Your menu function should return the option entered by the user, which is then used inside your main function. It should contain the menu portion, and nothing else. This way will be less misleading as per the names of the functions and what they actually do. On a side note, I am still unable to compile your code. How are you running it?

    You also still have system calls inside your code.

    What I would do is, (yep!) start over. Like AndiPersti said, focus on individual segments of the program like inputting the data into the data structure and successfully reading it back out to stdout instead of a file. You said you would like to be able to enter an undefined amount of "records", this implies the use of linked lists. This seems a little out of your reach at this time, so focus on using your static sized array. Most importantly, you need to think about code stability. As in, not writing out of bounds in arrays, valid type of data, so on...

    Avoid global variables unless absolutely necessary.
    Global Variables Are Bad

    im using Dev C++ on win7 i did the program before and it worked with static arrays! i trying it again and show you! maybe you can help me from there?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double Linked Dynamic Lists Vs Unrolled Linked Lists
    By lantzvillian in forum C Programming
    Replies: 6
    Last Post: 02-14-2012, 01:07 PM
  2. From Python's Linked Lists to Dynamic Arrays
    By hexbox in forum C Programming
    Replies: 3
    Last Post: 01-26-2005, 03:14 PM
  3. question on linked lists(stack with linked lists)
    By dionys in forum C Programming
    Replies: 1
    Last Post: 06-02-2004, 12:08 PM
  4. Dynamic number of linked lists
    By foniks munkee in forum C Programming
    Replies: 2
    Last Post: 02-24-2002, 09:30 PM
  5. dynamic memory + linked lists
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-10-2002, 04:50 PM

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