Thread: Unknown problem

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Plymouth, U.K.
    Posts
    13

    Unknown problem

    Okay, here's the code:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    void add(char **data, int rows);
    void del(char **data, int rows);
    void display(char **data, int rows);
    void save(char **data, int rows, FILE * fp);
    void menu();
    
    int main()
    {
        int x;
        int q;
        int quit;
        char **data;
        FILE * fp = fopen("list.dat", "r+");
        if(fp == NULL)
        {
              fp = fopen("list.dat", "w+");
        }
        for(x=0;fgets(data[x], 1000, fp) != NULL;x++);
        {
                              fgets(data[x], 1000, fp);
        }
        while(quit != 1){
        menu();
        scanf("%d", q);
        switch(q)
        {
                    case 1:{
                         add(data, x);
                         save(data, x, fp);
                         }
                    case 2:{
                         del(data, x);
                         save(data, x, fp);
                         }
                    case 3:{
                         display(data, x);
                         }
                    case 4:{
                         quit = 1;
                         }
        }
    }
    fclose(fp);
    return(0);
    }                     
          
    void add(char **data, int rows){
         char *add;
         printf("Please enter string to be added to the data file: ");
         scanf("%s", *add);
         strcat(add, "\n");
         strcpy(data[rows+1], add);
         }
         
    void del(char **data, int rows){
         int del;
         display(data, rows);
         printf("Please enter the number of the entry to be deleted: ");
         scanf("%d", del);
         data[del]= "";
         }
    
    void display(char **data, int rows){
         int i;
         for(i=0;i<=rows;i++){
         printf("[%d]\t%s\n",i,data[i]);
         }
         }
         
    void save(char **data, int rows, FILE * fp){
         int i;
         for(i=0;i<=rows; i++){
                          strcat(data[i], "\n");
                          fputs(data[i], fp);
                          }
         }
    
    void menu(){
         printf("Welcome to the menu.\n");
         printf("1) Add data to the file.\n");
         printf("2) Remove data from the file.\n");
         printf("3) Display contents of the file.\n");
         printf("4) Exit the program.\n");
         printf("Please insert the corresponding number for the function you wish to perform: ");
         }
    On compile it has no errors - but when I run it, it crashes before it displays the menu (menu(). I'm not sure why, do any of you have any ideas? Also, feel free to tell me where my code is inefficient

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
        for(x=0;fgets(data[x], 1000, fp) != NULL;x++);
        {
                              fgets(data[x], 1000, fp);
        }
    Can you see it now? Also

    Code:
        FILE * fp = fopen("list.dat", "r+");
        if(fp == NULL)
        {
              fp = fopen("list.dat", "w+");
        }
    That's kind of silly. Edit: And finally, scanf accepts a pointer

    Code:
    scanf("%d", q);
    scanf("%d", &q);

  3. #3
    Registered User
    Join Date
    Apr 2006
    Location
    Plymouth, U.K.
    Posts
    13
    Quote Originally Posted by Tonto
    Code:
        for(x=0;fgets(data[x], 1000, fp) != NULL;x++);
        {
                              fgets(data[x], 1000, fp);
        }
    Can you see it now? Also

    Code:
        FILE * fp = fopen("list.dat", "r+");
        if(fp == NULL)
        {
              fp = fopen("list.dat", "w+");
        }
    That's kind of silly. Edit: And finally, scanf accepts a pointer

    Code:
    scanf("%d", q);
    scanf("%d", &q);
    I was told to do the for loop like that by a friend after my code didn't work the first time. What's wrong with the fopen part? And finally, oops at forgetting the &

  4. #4
    Registered User ortegac's Avatar
    Join Date
    Mar 2006
    Location
    In front of the computer screen
    Posts
    15

    Talking See if this help

    Code:
    --------------------Configuration: ass5pract - Win32 Debug--------------------
    Compiling...
    ass5pract.c
    C:\Documents and Settings\Famili@\My Documents\aaron\CM22\ass5pract.c(21) : warning C4700: local variable 'data' used without having been initialized
    C:\Documents and Settings\Famili@\My Documents\aaron\CM22\ass5pract.c(27) : warning C4700: local variable 'q' used without having been initialized
    C:\Documents and Settings\Famili@\My Documents\aaron\CM22\ass5pract.c(53) : warning C4700: local variable 'add' used without having been initialized
    C:\Documents and Settings\Famili@\My Documents\aaron\CM22\ass5pract.c(62) : warning C4700: local variable 'del' used without having been initialized
    
    ass5pract.obj - 0 error(s), 4 warning(s)
    You get 4 warnings they are not intialized, dont you think that is messing it up?

    Also try to only one with one function at a time. Examples use only the funtion to display, try too break your program in smaller parts. To see where the error is?
    Then if you see the display funtions works, add another one an so on.

    Caleb

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Unknown problem
    By pink_langouste in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 12:36 AM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM