Thread: Warnings, warnings, warnings?

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    Warnings, warnings, warnings?

    I have 5 warnings standing in my way and have tried lots of different things, but no luck. The last suggestion I was given was to try using strcmp functions. When implemented I had even more warnings accompanied by errors. I am not sure what else to try. Could someone fix or suggest other possible ways to fixing these warnings I can't seem to shake?

  2. #2
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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
    You aren't testing your circumstances properly.
    The world is waiting. I must leave you now.

  3. #3
    Me want cookie! Monster's Avatar
    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.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Shadow
    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
    You aren't testing your circumstances properly.

    My functions are prototyped and why the worry about #define clrscr() system("cls") ?

  5. #5
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > 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.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Monster
    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.
    Thx for the help.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Shadow
    > why the worry about #define clrscr() system("cls") ?
    All computers don't have system, or cls.
    Would you recommend removing them. Would that have any effect on my program?

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Shadow
    > why the worry about #define clrscr() system("cls") ?
    All computers don't have system, or cls.
    I am down to 2 warnings but i don't how to initialize the local dta_file*

    C:\My Documents\temp2\clientcode2.c(128) : warning C4700: local variable 'dta_file' used without having been initialized

    ?????

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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);
    This is you general problem area.
    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.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by spentdome
    C:\My Documents\temp2\clientcode2.c(128) : warning C4700: local variable 'dta_file' used without having been initialized
    >void add_record(void)
    >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);
    The highlighted part doesn't do anything. You probably meant to say
    >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]

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    41

    still no cigar

    Originally posted by Hammer


    >void add_record(void)
    >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);
    The highlighted part doesn't do anything. You probably meant to say
    >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?
    I have tried to implement your suggestions and here is the code. No luck. I have no idea what's going on now. Thx for the help.

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >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);
    I presume you're going to add code later to do something inbetween opening and closing the file?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #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);
    }
    }

  14. #14
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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.

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Shadow
    Edit your post and repost your code inside code tags.
    Refer to Mr Hammer's signature on how to use them.

    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); 
    } 
    }
    */

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advice on removing valid (but minor) compiler warnings
    By PaulBlay in forum C Programming
    Replies: 12
    Last Post: 04-20-2009, 12:16 PM
  2. How to solve warnings that appear when building my DLL...
    By starcatcher in forum Windows Programming
    Replies: 6
    Last Post: 12-14-2008, 11:47 AM
  3. Warnings when using vector of vector
    By Boksha in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2008, 01:54 PM
  4. Replies: 9
    Last Post: 03-14-2008, 09:55 AM
  5. Warnings from String manipulation functions.
    By Arker in forum C Programming
    Replies: 4
    Last Post: 10-14-2002, 11:59 PM