#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Run-Time Check Failure #3


    Good day, i am new to this forum but i need some help, i ran this program in visual c++ and got this error: "Run-Time Check Failure #3 - The variable 'new file' is being used without being initialized," i ran this same program in borland 5.02 and it ran perfectly but in visual c++ it runs but it doesn't rite to the file, could someone please help me out? thanks in advance, here is the code:

    #include<conio.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<time.h>


    #define TRUE 0
    #define FALSE 1

    typedef struct
    {
    int attempts;
    int higher ;
    int lower;
    }score;

    void play(int,int,int[],score,FILE *, score,FILE *);
    int main()
    {
    FILE *newfile, *read;
    score newscore;
    score oldscore;
    int allscores[3]={0};
    int number, guess;
    int correct;

    correct =1;


    srand(time(NULL));
    number = rand()%10+1;//%32767 + 1;





    printf("Welcome to the guessing game\n\n");

    if(read = fopen("gameinfo.txt","r")) //read the previous scores from file
    {

    fscanf(read,"%d %d %d", &oldscore.attempts, &oldscore.higher, &oldscore.lower);
    printf("\n\nPrevious high score.\n\n");
    printf("Attempts\t\t%d\n\nToo High\t\t%d\n\nToo Low\t\t\t%d",oldscore.attempts,oldscore.higher,oldscore.lower);

    }

    play(guess,number, allscores, newscore, read,oldscore,newfile); //checks if number entered by user is higher or lower than the current number and also checks if the
    //newscore is higher than the old score

    getch();



    return 0;
    }

    void play(int guess,int number,int allscores[],score newscore,FILE * read, score oldscores,FILE * newfile) //function call
    {

    do
    {


    printf("Please enter your number\n"); //outputs previous score by the user
    scanf("%d",&guess);
    system ("cls");
    printf("\n\n");



    allscores[0]++;
    if(guess < number)
    {
    printf("this is too low. Please retry\n\n"); //increments each time the number is lower than the computer's number

    allscores[1]++;

    }
    if(guess>number)
    {
    printf("this is too high. Please retry\n\n"); //increments each time the number is higher than the computer's number

    allscores[2]++;
    }
    }while(guess!=number);
    if(guess==number)
    {

    newscore.attempts = allscores[0];
    newscore.lower = allscores[1]; //assigns all the current scores to the newscore variables
    newscore.higher = allscores[2];
    printf("congrats you got it right\n\n");
    printf("Thank you for playing here is your score.\n\n");
    printf("Attempts\t\t%d\n\nToo High\t\t%d\n\nToo Low\t\t\t%d",newscore.attempts,newscore.higher,newscore.lower); //outputs the newscores
    }



    if(allscores[0]<=oldscores.attempts)
    {


    fclose(read);
    newfile=fopen("gameinfo.txt","w"); //writes the newscores to file


    fprintf(newfile,"%d ",newscore.attempts);
    fprintf(newfile,"%d ",newscore.higher);
    fprintf(newfile,"%d \n",newscore.lower);
    }

    }
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    Next time, post your code in [code][/code] tags, so it looks like this
    Code:
    int main()
    {
        FILE *newfile, *read;
        score newscore;
        score oldscore;

    Next, why declare newfile in main, when the fopen is in play?
    Code:
    main ( ) {
    FILE *newfile, *read;
    ...
    play(guess,number, allscores, newscore, read,oldscore,newfile); //checks if 
    ...
    }
    
    void play(int guess,int number,int allscores[],score newscore,FILE * read, score oldscores,FILE * newfile) //function call
    {
    ...
    newfile=fopen("gameinfo.txt","w"); //writes the newscores to file
    > i ran this program in visual c++ and got this error: "Run-Time Check Failure #3
    As far as main is concerned, you're passing garbage into play.
    Now you and I know that play doesn't give a crap about what newfile points to initially, because it is going to do an fopen before anything else. This is why it works on borland.

    Make newfile a local variable of play and stop passing it as a garbage parameter.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    solution


    could u correct it and give me your solution?




    Originally Posted by salem
    Next time, post your code in [code][/code] tags, so it looks like this
    Code:
    int main()
    {
        FILE *newfile, *read;
        score newscore;
        score oldscore;

    Next, why declare newfile in main, when the fopen is in play?
    Code:
    main ( ) {
    FILE *newfile, *read;
    ...
    play(guess,number, allscores, newscore, read,oldscore,newfile); //checks if 
    ...
    }
    
    void play(int guess,int number,int allscores[],score newscore,FILE * read, score oldscores,FILE * newfile) //function call
    {
    ...
    newfile=fopen("gameinfo.txt","w"); //writes the newscores to file
    > i ran this program in visual c++ and got this error: "Run-Time Check Failure #3
    As far as main is concerned, you're passing garbage into play.
    Now you and I know that play doesn't give a crap about what newfile points to initially, because it is going to do an fopen before anything else. This is why it works on borland.

    Make newfile a local variable of play and stop passing it as a garbage parameter.
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > could u correct it and give me your solution?
    What do you think this is?
    Make newfile a local variable of play and stop passing it as a garbage parameter.
    If you can't edit your program to just change one parameter into a local variable, then do the world a favour and get out of programming.

    But then again, how much of it is really 'your' program?
    It seems like you're a forum wanderer, getting bits of answers from one forum or another, then moving on to somewhere else to make it seem like what you posted was all your own work, where in reality, it's just some horrid mish-mash of random ideas.

    Yes, it might earn you a 'pass' this time around. But you're basically screwed for the next assignment, because you failed to understand any of the reasons for doing the things you (or more accurately, someone else) did.

    Comments on this post

    • ptr2void agrees
    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

IMN logo majestic logo threadwatch logo seochat tools logo