![]() |
| | #1 |
| Registered User Join Date: Apr 2006 Location: Plymouth, U.K.
Posts: 13
| Unknown problem 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: ");
}
. 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 | |
| | #2 |
| Registered User 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);
}
Code: FILE * fp = fopen("list.dat", "r+");
if(fp == NULL)
{
fp = fopen("list.dat", "w+");
}
Code: scanf("%d", q);
scanf("%d", &q);
|
| Tonto is offline | |
| | #3 | |
| Registered User Join Date: Apr 2006 Location: Plymouth, U.K.
Posts: 13
| Quote:
| |
| Mindphuck is offline | |
| | #4 |
| Registered User Join Date: Mar 2006 Location: In front of the computer screen
Posts: 15
| 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) 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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |