![]() |
| | #1 |
| Registered User Join Date: Apr 2002
Posts: 41
| Warnings, warnings, warnings? |
| spentdome is offline | |
| | #2 |
| Unleashed Join Date: Sep 2001
Posts: 1,755
| Once again, > #define clrscr() system("cls") I worry about that. > void main() That is wrong. Also, after changing it, place a return 0; right before your program's closing brace. When I compiled this the first time I got 2 warnings because of void main, and main not returning a value. Just to be safe, and proper, I would suggest prototyping your functions. > I have 5 warnings standing in my way I only had 3: Code: tmp.c: In function `add_record': tmp.c:114: warning: comparison between pointer and integer tmp.c: In function `delete_record': tmp.c:171: warning: comparison between pointer and integer tmp.c: In function `search': tmp.c:213: warning: comparison between pointer and integer
__________________ The world is waiting. I must leave you now. |
| Shadow is offline | |
| | #3 |
| Me want cookie! Join Date: Dec 2001
Posts: 680
| Waring 1, 2 and 3: You are comparing a character with a character pointer. This is wrong, change char clientname to char clientname[20] (in function add_record, delete_record and search). Also do this for street_address, city, pros, country and postalorzip_code (and don't use the & in the scanf function when passing a character pointer/array). B.t.w. you can't compare 2 strings with the == operator. Use the strcmp function instead. Warning 4 and 5: dta_file is a filepointer. You must open the file before reading/writing. |
| Monster is offline | |
| | #4 | |
| Registered User Join Date: Apr 2002
Posts: 41
| Quote:
My functions are prototyped and why the worry about #define clrscr() system("cls") ? | |
| spentdome is offline | |
| | #5 |
| Unleashed Join Date: Sep 2001
Posts: 1,755
| > why the worry about #define clrscr() system("cls") ? All computers don't have system, or cls.
__________________ The world is waiting. I must leave you now. |
| Shadow is offline | |
| | #6 | |
| Registered User Join Date: Apr 2002
Posts: 41
| Quote:
| |
| spentdome is offline | |
| | #7 | |
| Registered User Join Date: Apr 2002
Posts: 41
| Quote:
| |
| spentdome is offline | |
| | #8 | |
| Registered User Join Date: Apr 2002
Posts: 41
| Quote:
C:\My Documents\temp2\clientcode2.c(128) : warning C4700: local variable 'dta_file' used without having been initialized ????? | |
| spentdome is offline | |
| | #9 |
| Unleashed Join Date: Sep 2001
Posts: 1,755
| I'm done with this one. - sorry> I am down to 2 warnings I still get 0. Code: printf("Client name: ");
scanf("%s",&clientname);
fprintf(dta_file,"%s\n",clientname);
fflush(stdin);
printf("Street address: ");
scanf("%s",&street_address);
fprintf(dta_file,"%s\n",street_address);
fflush(stdin);
printf("City: ");
scanf("%s",&city);
fprintf(dta_file,"%s\n",city);
fflush(stdin);
printf("Province or State: ");
scanf("%s",&pros);
fprintf(dta_file,"%s\n",pros);
fflush(stdin);
printf("Country: ");
scanf("%s",&country);
fprintf(dta_file,"%s\n",country);
fflush(stdin);
printf("Postal or Zip code: ");
scanf("%s",&postalorzip_code);
fprintf(dta_file,"%s\n",postalorzip_code);
fflush(stdin);
I noticed things in that source every few moments that made me go, "whaaaa?". If it were my choice, I would recommend rewritting the source. Use proper indentation, and neater comments. Comment things that only need to be commented on. You're accepting input, sending it to a file, then, waiting for the user to do some other routines. This isn't hard. Use indentation. Do not use void main. Always return a value. Always check that value too. "Brush those dangling function bottoms" with dis-infenctant. Provide return status', and always check them Even if they aren't your return status' check them. And make sure you're using the syntax correctly for what you're doing. All in all, I get no errors or warnings on compliation of the code, but when I run it, all I get fully functional is the menu. The .dat file is created, though nothing ever gets written to it. I try to enter a record for a new client, and the thing crashes. The program's quit option doesn't work at all. What compiler are you using? What OS are you on? ...and where are you learning this from?
__________________ The world is waiting. I must leave you now. |
| Shadow is offline | |
| | #10 | |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| Quote:
>FILE *dta_file; >fprintf(dta_file,"%s\n",clientname); See here... in function add_record() yuo have declared dta_file as a FILE * variable, then the first time it's used in the fprintf when you try and write to it. You need to open a file with dta_file first (fopen() calls like you have in main). Also, this is wrong in your latest post: Code: dta_file = fopen("client.dat", "r+b"); //opening in binary mode for read/write
(dta_file == NULL);
>if (dta_file == NULL) And this is wrong too: >void main() use int main(void), and given main() a return code. And this is even wronger ![]() >fflush(stdin); dont do this, it's undefined behaviour. And why do you fopen() a file, then fclose() it almost immediatetly?
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] | |
| Hammer is offline | |
| | #11 | |
| Registered User Join Date: Apr 2002
Posts: 41
| still no cigar Quote:
| |
| spentdome is offline | |
| | #12 |
| End Of Line Join Date: Apr 2002
Posts: 6,240
| >if (dta_file == NULL); This has an extra semi-colon on it. Therefore the code that follows this statement will be executed regardless of the result of this test. Remove the semi-colon. >dta_file = fopen("client.dat"); You forgot the second parameter to this function, telling it what mode to open the file with. If you reading the file, do this >dta_file = fopen("client.dat", "r"); But, you must check that dta_file is not NULL before using it, else your app will probably crash. This line of code also needs to be moved, as you are not allowed to have function calls in the middle of declarations. Move it to after >struct fclient *valid; I'm still curious what you're trying to achieve here Code: dta_file = fopen("client.dat", "wb");
fclose(dta_file);
__________________ When all else fails, read the instructions. If you're posting code, use code tags: [code] /* insert code here */ [/code] |
| Hammer is offline | |
| | #13 |
| Registered User Join Date: Apr 2002
Posts: 41
| I appreciate the help Hammer. Learning out of a book can prove quite difficult at times. I have made some chages and I think I am real close to a working function. In my add func() I need a way to read the contents of client.dat to compare against the user input. I have changed to int format using a code to simplify my app. I was wondering if you could take a look at it and see if you know of a way to complete the function. I'm using a temp address to hold the user input then I need to read the file to make sure the code is not already used. If it isn't then the user may continue to input the rest of the required information. Thx. #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define clrscr() system("cls") //************************************************ //structure for writing to and reading from file** struct fclient{ int client_code; char clientname; char street_address; char city; char pros; char country; char postalorzip_code; int deleted; //used as a boolean (0=false, 1=true) }; //************************************************ /* FUNCTION PROTOTYPES*********************/ char menu(void); void add_record(void); void delete_record(void); void search(void); char print_label(void); void key_wait (void); /* END OF FUNCTION PROTOTYPES**************/ int main(void) { FILE *dta_file; char choice; clrscr(); dta_file = fopen("client.dat", "r+b"); if (dta_file == NULL) { printf("\n\nFile does not exist: Creating file"); dta_file = fopen("client.dat", "w+b"); key_wait(); } do { choice = menu(); clrscr(); switch (choice) { case '1': add_record(); break; case '2': delete_record(); break; case '3': search(); break; case '4': display(); break; case '5': print_label(); break; } } while (choice != '6'); fclose(dta_file); return 0; } /************************************************** **********/ char menu (void) /* Task: display a menu and return the choice of the user */ /************************************************** **********/ { char choice; clrscr(); printf("\t\t\tClient Inventory Management \n\n\n"); printf("\t1. Add a Client\n\n"); printf("\t2. Delete a Client\n\n"); printf("\t3. Search for a Client\n\n"); printf("\t4. Display all Clients\n\n"); printf("\t5. Print shipping label\n\n"); printf("\t6. Exit the program\n\n\n"); printf("\tPlease enter your selection: "); do { choice = getch(); } while (choice < '1' || choice > '7'); return (choice); } /************************************************** **********/ void add_record(void) /* Task: Read from keyboard and add the record in the file */ /************************************************** **********/ { FILE *dta_file; int client_code; char clientname; char street_address; char city; char pros; char country; char postalorzip_code; int clientunique=1;//flag for client code validation int client_code2; if (dta_file!=NULL) { dta_file = fopen("client.dat","r+b"); } // Validate the client code************************ while(!clientunique) { clientunique=1; printf("Client code: "); scanf("%d", &client_code2); while(dta_file != NULL) { ******//fread( ???....this is where i don't know what to do***** if (client_code2 != client_code) { clientunique = 0; } dta_file=dta_file; fprintf(dta_file,"%d",client_code); printf("Client name: "); scanf("%s",&clientname); fprintf(dta_file,"%s",clientname); printf("\n"); printf("Street address: "); scanf("%s",&street_address); fprintf(dta_file,"%s",street_address); printf("\n"); printf("City: "); scanf("%s",&city); fprintf(dta_file,"%s",city); printf("\n"); printf("Province or State: "); scanf("%s",&pros); fprintf(dta_file,"%s",pros); printf("\n"); printf("Country: "); scanf("%s",&country); fprintf(dta_file,"%s",country); printf("\n"); printf("Postal or Zip code: "); scanf("%s",&postalorzip_code); fprintf(dta_file,"%s",postalorzip_code); if (!clientunique) { printf("This client name is taken. Please enter a different name...Press a key\n"); key_wait(); } fclose(dta_file); } } |
| spentdome is offline | |
| | #14 |
| Unleashed Join Date: Sep 2001
Posts: 1,755
| Edit your post and repost your code inside code tags. Refer to Mr Hammer's signature on how to use them.
__________________ The world is waiting. I must leave you now. |
| Shadow is offline | |
| | #15 | |
| Registered User Join Date: Apr 2002
Posts: 41
| Quote:
I appreciate the help Hammer. Learning out of a book can prove quite difficult at times. I have made some chages and I think I am real close to a working function. In my add func() I need a way to read the contents of client.dat to compare against the user input. I have changed to int format using a code to simplify my app. I was wondering if you could take a look at it and see if you know of a way to complete the function. I'm using a temp address to hold the user input then I need to read the file to make sure the code is not already used. If it isn't then the user may continue to input the rest of the required information. Thx. Code: /*
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//************************************************
//structure for writing to and reading from file**
struct fclient{
int client_code;
char clientname;
char street_address;
char city;
char pros;
char country;
char postalorzip_code;
int deleted; //used as a boolean (0=false, 1=true)
};
//************************************************
/* FUNCTION PROTOTYPES*********************/
char menu(void);
void add_record(void);
void delete_record(void);
void search(void);
char print_label(void);
void key_wait (void);
/* END OF FUNCTION PROTOTYPES**************/
int main(void)
{
FILE *dta_file;
char choice;
dta_file = fopen("client.dat", "r+b");
if (dta_file == NULL)
{
printf("\n\nFile does not exist: Creating file");
dta_file = fopen("client.dat", "w+b");
key_wait();
}
do
{
choice = menu();
switch (choice)
{
case '1': add_record(); break;
case '2': delete_record(); break;
case '3': search(); break;
case '4': display(); break;
case '5': print_label(); break;
}
}
while (choice != '6');
fclose(dta_file);
return 0;
}
/ **************************************************
**********/
char menu (void)
/*
Task: display a menu and return the choice of the user
*/
/ **************************************************
**********/
{
char choice;
printf("\t\t\tClient Inventory Management \n\n\n");
printf("\t1. Add a Client\n\n");
printf("\t2. Delete a Client\n\n");
printf("\t3. Search for a Client\n\n");
printf("\t4. Display all Clients\n\n");
printf("\t5. Print shipping label\n\n");
printf("\t6. Exit the program\n\n\n");
printf("\tPlease enter your selection: ");
do
{
choice = getch();
} while (choice < '1' || choice > '7');
return (choice);
}
/ **************************************************
**********/
void add_record(void)
/*
Task: Read from keyboard and add the record in the file
*/
/ **************************************************
**********/
{
FILE *dta_file;
int client_code;
char clientname;
char street_address;
char city;
char pros;
char country;
char postalorzip_code;
int clientunique=1;//flag for client code validation
int client_code2;
if (dta_file!=NULL)
{
dta_file = fopen("client.dat","r+b");
}
// Validate the client code************************
while(!clientunique)
{
clientunique=1;
printf("Client code: ");
scanf("%d", &client_code2);
while(dta_file != NULL)
{
******//fread( ???....this is where i don't know what to do*****
if (client_code2 != client_code)
{
clientunique = 0;
}
dta_file=dta_file;
fprintf(dta_file,"%d",client_code);
printf("Client name: ");
scanf("%s",&clientname);
fprintf(dta_file,"%s",clientname);
printf("\n");
printf("Street address: ");
scanf("%s",&street_address);
fprintf(dta_file,"%s",street_address);
printf("\n");
printf("City: ");
scanf("%s",&city);
fprintf(dta_file,"%s",city);
printf("\n");
printf("Province or State: ");
scanf("%s",&pros);
fprintf(dta_file,"%s",pros);
printf("\n");
printf("Country: ");
scanf("%s",&country);
fprintf(dta_file,"%s",country);
printf("\n");
printf("Postal or Zip code: ");
scanf("%s",&postalorzip_code);
fprintf(dta_file,"%s",postalorzip_code);
if (!clientunique)
{
printf("This client name is taken. Please enter a different name...Press a key\n");
key_wait();
}
fclose(dta_file);
}
}
*/
| |
| spentdome is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Advice on removing valid (but minor) compiler warnings | PaulBlay | C Programming | 12 | 04-20-2009 12:16 PM |
| How to solve warnings that appear when building my DLL... | starcatcher | Windows Programming | 6 | 12-14-2008 11:47 AM |
| Warnings when using vector of vector | Boksha | C++ Programming | 5 | 03-29-2008 01:54 PM |
| Change what you get warnings for in Visual studio | h3ro | Tech Board | 9 | 03-14-2008 09:55 AM |
| Warnings from String manipulation functions. | Arker | C Programming | 4 | 10-14-2002 11:59 PM |