Loop not incremententing? (short code)

This is a discussion on Loop not incremententing? (short code) within the C Programming forums, part of the General Programming Boards category; Code: int main(int argc, char*argv[]) { double i=0.0; double j=0.0; int player_number; double batting_average; char batting_record; FILE *fp=fopen(argv[1],"r"); while(fscanf(fp,"%d%c",&player_number,&batting_record)!=EOF) { ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    140

    Loop not incremententing? (short code)

    Code:
    int main(int argc, char*argv[])
       {
               double i=0.0;
               double j=0.0;
               int player_number;
               double batting_average;
              char batting_record;
     
              FILE *fp=fopen(argv[1],"r");
             while(fscanf(fp,"%d%c",&player_number,&batting_record)!=EOF)
          {
              fscanf(fp,"%d",&player_number);
              printf("\nPlayer %d's record: ",player_number);
     
                 while(batting_record!='\n')
              {
     
     
                fscanf(fp,"%c",&batting_record);
                printf("%c",batting_record);
     
                if (batting_record=='H')
                  i++;
     
                if (batting_record=='O')
                  j++;
     
     
                if ((i!=0)&&(j!=0))
     
                batting_average=(i)/(i+j);
     
     
              }
          printf("Player %d's batting average: %.2lf\n",player_number,batting_average);
     
     
            }
     
     
     
     
     
     
      return 0;
     
      }
    problem source lines 30 batting_average

    File:
    Player 12's record: HOOOWSHHOOHPWWHO
    Player 12's batting average: 0.455
    Player 4's record: OSOHHHWWOHOHOOO
    Player 4's batting average: 0.417
    Player 7's record: WPOHOOHWOHHOWOO
    Player 7's batting average: 0.364

    my output: //first output result is correct, but the rest are off...?
    Player 12's record: HOOOWSHHOOHPWWHO
    Player 12's batting average: 0.45 (correct)

    Player 4's record: OSOHHHWWOHOHOOO
    Player 4's batting average: 0.43 (wrong)

    Player 7's record: WPOHOOHWOHHOWOO
    Player 7's batting average: 0.41 (wrong)
    Last edited by tmac619619; 11-10-2012 at 12:00 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Please work on your indentation when you post code.

    You don't reset "i" and "j" when you process a new player.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to exit out of a while loop? (short code)
    By tmac619619 in forum C Programming
    Replies: 2
    Last Post: 11-02-2012, 02:47 AM
  2. Please help! short while loop tracking problem.
    By matthayzon89 in forum C Programming
    Replies: 7
    Last Post: 04-22-2010, 01:29 PM
  3. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-24-2006, 12:46 AM
  4. help with this short code on my quiz..
    By vtlo99 in forum C++ Programming
    Replies: 2
    Last Post: 03-01-2002, 08:17 AM
  5. how will this short c++ code look in pure c?
    By dune911 in forum C Programming
    Replies: 8
    Last Post: 10-19-2001, 12:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21