Debugging Issues

This is a discussion on Debugging Issues within the C Programming forums, part of the General Programming Boards category; I am writing a program to read a list of grades from an array, calculates the average and then gives ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    5

    Debugging Issues

    I am writing a program to read a list of grades from an array, calculates the average and then gives a grade based on the average. However, I am having debugging issues that I was wondering if anyone with a little more expertise could determine.

    proj7.c: In function 'main':
    proj7.c:17: error: expected ')' before '}' token
    proj7.c:17: error: expected ';' before '}' token
    proj7.c: At top level:
    proj7.c:18: warning: data definition has no type or storage class
    proj7.c:18: error: 'student_grade' undeclared here (not in a function)
    proj7.c:18: error: 'grade_size' undeclared here (not in a function)
    proj7.c:18: error: void value not ignored as it ought to be
    proj7.c:19: warning: data definition has no type or storage class
    proj7.c:19: warning: parameter names (without types) in function declaration
    proj7.c:19: error: conflicting types for 'compare'
    proj7.c:8: error: previous declaration of 'compare' was here
    proj7.c:23: error: expected identifier or '(' before 'return'
    proj7.c:25: error: expected identifier or '(' before '}' token
    proj7.c: In function 'get_data':
    proj7.c:42: error: 'input' undeclared (first use in this function)
    proj7.c:42: error: (Each undeclared identifier is reported only once
    proj7.c:42: error: for each function it appears in.)
    proj7.c:42: error: expected ')' before 'status'
    proj7.c: In function 'average':
    proj7.c:68: warning: 'return' with a value, in function returning void
    proj7.c: In function 'compare':
    proj7.c:86: error: 'letter' undeclared (first use in this function)
    proj7.c:87: error: expected ')' before '}' token
    proj7.c:87: error: expected expression before '}' token
    proj7.c:89: error: expected expression before 'else'
    proj7.c: At top level:
    proj7.c:96: error: expected identifier or '(' before '}' token


    And heres the code:
    Code:
    #include <stdio.h>
    #define SIZE 15
    
    void get_data(double ar[], int *count);
    
    void average(const double ar[], int count);
    
    void compare(const double ar[], double avg, int count);
    
    int main(void)
    {
      double student_grade[SIZE];
      double student_avg;
      int grade_size;
    
    
      get_data(student_grade, &grade_size};
      student_avg= average(student_grade, grade_size);
      compare(student_grade, grade_size, student_avg);
    
    
    
    return(0);
    
    }
    
    void get_data(double ar[], int *count)
    {
    
      FILE *inp;
      FILE *outp;
    
      int i = 0;
      int input_status;
      double grade_1;
    
      inp = fopen("proj7.dat", "r");
      outp = fopen("proj7.dat", "w");
    
    
      input_status = fscanf(inp, "%lf", &grade_1);
      while(input status == 1 && i <= SIZE)
      {
      ar[i] = grade_1;
    
      i++;
    
      fprintf(outp,"%d"   "%4.2lf",i, &grade_1);
      }
    
    
    *count = i;
    } 
     
    void average(const double ar[], int count)
    {
     double total;
     double avg;
     int i;
      
     for (i= 0; i <= count; i++)
     {
      total += ar[i];
     }
    
    avg= total/i;
      
    return(avg);
       
    }
    
    void compare(const double ar[], double average, int count)
    { 
      char ltr_grade;
      int i;
     
    
    
      printf("\n STUDENT NUMBER          SCORE      GRADE \n");
      printf("\n --------------          -----      -----\n");
    
     
      for(i = 0; i < count;  ++i)
      {
      if(ar[i] <= average-10)
            letter = 'F';
      if(ar[i] <= average+10}   
            letter = 'C';
      else
            letter = 'A';
    
    
      printf("%3d%9lf%9c \n", i+1, ar[i], ltr_grade);
      }
     
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    get_data(student_grade, &grade_size};

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Also:
    - the average() function returns void yet you are trying to return something from it.
    - The get_data() function has: while(input status. You are missing the underscore from the variable name.
    - The compare() function is using a variable named letter that was never declared.
    Code:
    if(ar[i] <= average+10}

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    fprintf(outp,"%d"   "%4.2lf",i, &grade_1);
    %lf takes a double, not a double*.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What has this got to do with debugging? Debugging is when you have a program that runs, but doesn't do what you want.

    What the original poster is asking for here is help getting rid of compiler errors, and unless they are caused by bugs in the compiler [which I'm 99% sure isn't the case here].

    Compiler errors because of incorrect code is not a BUG - it's "not yet compiling code".

    And the best way to ensure the code compiles is to write a small portion of code (10-20 lines), compile that, if any errors, fix them, repeat until it compiles. Ideally, you then check that the bit of code you've written so far works as expected. Only when that is done, do you add more code - again, in small steps.

    --
    Mats
    Last edited by matsp; 06-05-2009 at 05:01 PM.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    What has this got to do with debugging? Debugging is when you have a program that runs, but doesn't do what you want.
    I think you are being a little bit unwise in saying that trying to make something compile properly is not "debugging", in part because that would imply that "debugging" is something involving a "debugger" which that will be a silly place to live.

    You can debug "code which compiles" WITHOUT a debugger, you can "debug" code which does not compile without a debugger. In fact, there are no other choices.

    And the best way to ensure the code compiles is to write a small portion of code (10-20 lines), compile that, if any errors, fix them, repeat until it compiles. Ideally, you then check that the bit of code you've written so far works as expected. Only when that is done, do you add more code - again, in small steps.
    If I could replace all the "stickys" with a single paragraph, matsp, that would be it.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by matsp View Post
    And the best way to ensure the code compiles is to write a small portion of code (10-20 lines), compile that, if any errors, fix them, repeat until it compiles. Ideally, you then check that the bit of code you've written so far works as expected. Only when that is done, do you add more code - again, in small steps.

    --
    Mats
    QFT!
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dev-C++: Problems with Breakpoint Debugging
    By Thileepan_Bala in forum C Programming
    Replies: 1
    Last Post: 01-17-2008, 09:48 AM
  2. gdb no debugging symbols found
    By Laserve in forum Linux Programming
    Replies: 8
    Last Post: 09-17-2006, 08:56 AM
  3. help needed in organising a debugging contest
    By shrijeetp in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-22-2006, 06:45 AM
  4. Debugging book recommendation
    By dagans in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 09-13-2005, 07:35 PM
  5. debugging directx apps
    By confuted in forum C++ Programming
    Replies: 1
    Last Post: 08-16-2003, 08:56 AM

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