C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-08-2006, 02:25 PM   #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
Mindphuck is offline   Reply With Quote
Old 04-08-2006, 02:28 PM   #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);
Tonto is offline   Reply With Quote
Old 04-08-2006, 02:31 PM   #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 &
Mindphuck is offline   Reply With Quote
Old 04-08-2006, 02:35 PM   #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
ortegac is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:22 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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