Thread: Please Help ASAP!!! Calculation error!

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    19

    Please Help ASAP!!! Calculation error!

    Can someone PLEASE help me with fixing this. Something is wrong with how the strikes are being added, it seems to just add 10. The numbers in the file are: 8 1 9 1 7 2 10 8 2 6 2 7 3 10 10 10 8 1. I'm also getting 12 frames instead of 12. The total score is supposed to be 176 and I get 153.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    //prototypes (function announcement)
    
    //function main
    int main ()
    
     {
       // declarations and initializations
            int balls [21]={0};
            int frame [11]={0};
            frame [0]=0;
            int j=1;
            int index=0;
            int Fscore=0;
          //  int TotalScore =0;
    
            FILE * frData;
    
       //prompts and scans
            frData=fopen("CleanLane9.txt", "r");
            if (!frData)
            {
                printf("could not open file\a\n");
                exit (101);
            }
    
        // calculation
            do
            {
                fscanf(frData, "%d", &balls[index]);
               //printf("%d ", balls [index]);
                index+=1;
            }
            while ( (fscanf(frData, "%c", &Fscore))!=EOF);
            index=0;
    
        for (; index<=21;)
           {
            if ((balls[index])==10)
                {
                    frame[j]=frame[j-1]+balls[index]+balls[index+1]+balls[index+2];
                    printf("\nFrame %d: %d\n ", j,frame[j]);
                    index++;
                    j++;
                }
           else if ((balls[index]+balls[index+1])==10)
                {
                frame[j]=frame[j-1]+balls[index]+balls[index+1]+balls[index+2];
                printf("\nFrame %d: %d\n ", j,frame[j]);
                index+=2;
                j++;
                }
    
            else ((balls[index])<10);
                {
                frame[j] = frame[j-1]+balls[index]+balls[index+1];
                printf("\nFrame %d: %d\n ", j,frame[j]);
                index+=2;
                j++;
                }
    
            }
            // output
    
        return 0;
        }
        // more functions

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The obvious problem is in how you're reading the file.

    fscanf() skips over whitespace. You are assuming that %c will retrieve the intervening space characters. It will skip over them, and read the next character.

    I haven't checked your subsequent calculations, as I have no idea what you are trying to achieve.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I don't see how you're getting any results at all, because it shouldn't even compile.

    else ((balls[index])<10);

    There is no condition with the final else.
    Else means "do this when everything else has failed to match"

    > for (; index<=21;)
    And this will step off the end of the array, and you'll process a garbage ball[index]

    > frame[j]=frame[j-1]+balls[index]+balls[index+1]+balls[index+2];
    Again, watch for stepping off the ends of the arrays
    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. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Salem View Post
    I don't see how you're getting any results at all, because it shouldn't even compile.

    else ((balls[index])<10);
    I'm not sure if this is the point you mean that it shouldn't compile, but it should. Think...
    Code:
    if(...)
      ...
    else {
      ((balls[index]) < 10);
    }
    Is perfectly legal, and so is leaving out the braces there. Of course, it's a no-op and definitely not what the OP intended, but it would compile (albeit usually with a warning if warnings are put up high enough).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM