Thread: Getting an error message!

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    82

    Getting an error message!

    Hey,
    Code:
    /*
     *Database for storing music cd.
     */
    
    #include<stdio.h>
    
    #define MAX      200 //max characters a user can enter
    
    /*prototypes*/
    void write_record(void);
    int file_exists(void);
    void file_new(void);
    void file_open(void);
    
    /*structure for music cd*/
    typedef struct cd
    {
            char   title[MAX];
            char   artist[MAX];
            int    tracks;
            int    album;
            float   price;
            
            struct cd  *next;
    }cd;
    
    /* Global Variables: */ 
    cd    *head = NULL; // a pointer that points to start of the cd node.
    cd    *curr;         // pointer that point to current cd node
    int   changes = 0;  // boolean: if changes made or not
    char  fname[MAX];   // stores a name of a file.
    
    /*function that shows up on exit*/
    void quit(void)
    {
         printf("\nThank you for using database\n");
         system("pause");
    }
    
    
    /*Function to see if a file exists*/
    int file_exists(void)
    {
        FILE *fptr = fopen(fname,"rb"); //open a file for reading.
        
        if(fptr == NULL)              //if file does not exists return 0.
            return 1;
        else                          //if file exists:
        {
            fclose(fptr);             //close file
            return 0;                 //return 1.
        }
    }
    
    int main(void)
    {
        int  choice;         //variable that will store choice which the user enters
        void (*funcPtr)(void); //a pointer to a function that takes no arguments nor returns anything
        
        atexit(quit); //calls 'quit' function b4 program exits
        
        /*Loop that will only break on exit*/
        for(;;)
        {      
               printf("\nCurrent file open: %s\n",fname); // print current file opened
               
               /*print menu*/
               puts("\nMenu: \n");
               puts("     1. File new");
               puts("     2. File open");
               puts("     3. Write a record");
               puts("     4. Delete a record by number");
               puts("     5. Delete a record by title");
               puts("     6. Save records");
               puts("     7. Save records with a new file name");
               puts("     8. Edit a record");
               puts("     9. Delete a file");
               puts("    10. Exit");
               
               scanf(" %d",&choice); //ask user for choice
               
               switch(choice)
               {
                    case 2:
                         file_open();
                         break;
                    case 10:
                         exit(0);
                         break;
               }
        }
    }
         
    void file_open(void)
    {
         /*check if there are changes and then see if a the user wants to discard the records*/
         if(changes && yesno("Are you sure you want to discard the records?"))
         {
             return;
         }
         
         /*ask the user the file name they would like to open*/
         printf("Enter the file name you would like to open: ");
         scanf (" %s",fname);
         
         /*check if the file exists*/
         if(file_exists == 1)
         {
             printf("File does not exists!\n");
             fname[0] = '\0';
             return;
         }
    }
    I have shorten my code, so that is doesn't consumes much time.

    Why is it giving me an error saying "comparison between a pointer and integer" when I am doing "if(file_exists == 1)" ?

    And its not working.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Because you forgot the brackets, so the compiler thinks you ment a pointer to the 'file_exists' function.

    if(file_exists == 1) should be if(file_exists() == 1)

    BTW why your comments in the file_exists function are very misleading...

    and the file name should not be a global, you should pass the file name into the file_exists function as a param.
    Last edited by novacain; 10-09-2013 at 01:47 AM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by novacain View Post
    Because you forgot the brackets, so the compiler thinks you ment a pointer to the 'file_exists' function.

    if(file_exists == 1) should be if(file_exists() == 1)

    BTW why your comments in the file_exists function are very misleading...

    and the file name should not be a global, you should pass the file name into the file_exists function as a param.
    Ow, didn't even notice that mistake. Thanks man, and I will try and make the comments more useful. And fname is global because many functions are using it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > And fname is global because many functions are using it.
    That's a rubbish reason.
    You barely need one hand to count the number of functions you have.

    Keep this up, and you'll have 100's of functions and 1000's of globals when you start to try (and fail) to write any meaningful program.

    Consider this the time you learn some good programming habits that will be of benefit to you in the future (and not just lazy ones for the moment).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    82
    Quote Originally Posted by Salem View Post
    You barely need one hand to count the number of functions you have.
    Did I mention I have shorten the program? Give me another reason to when to use global variables.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Harith
    Give me another reason to when to use global variables.
    Generally, for now, you have no reason to use global variables. At some point, when you become more skilled and write much larger programs, you might find that there is global state involved that makes sense to judiciously involve global variables. At the moment, your main exposure to this would be standard input and standard output as accessed via stdin and stdout respectively.

    The thing is, for the trivial programs that you're writing now, the state involved is effectively global, because the program is so tiny. But if you go ahead and say "oh, global state, so global variable!", then you'll never develop the habit of reaching for locality first. Thus, when you go on to larger programs, you will still reach for global variables first, upon which the problems with global variables will come into play (e.g., they make it harder for you to reason about your program) and it'll be so much harder for you to program properly.

    Basically, in a sufficiently small program, all the advice concerning good style and such don't matter. But if you don't develop good style by practicing it when writing small programs, you have no hope of having good style when writing large programs, where it does matter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 10-08-2010, 05:35 PM
  2. Error Message
    By tallguy in forum C++ Programming
    Replies: 6
    Last Post: 04-26-2007, 11:00 AM
  3. new error message for me
    By elad in forum C++ Programming
    Replies: 8
    Last Post: 09-22-2004, 02:48 PM
  4. Getting an error message
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 07-30-2002, 07:39 PM
  5. Error message please help
    By jill in forum C++ Programming
    Replies: 2
    Last Post: 12-01-2001, 05:23 PM

Tags for this Thread