Help with C program

This is a discussion on Help with C program within the C Programming forums, part of the General Programming Boards category; I have this program that I am working on using C. The code compiles and runs I am just a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Help with C program

    I have this program that I am working on using C. The code compiles and runs I am just a little lost with the last portion of it. The purpose of the program is for the user to enter grades for multiple students. When the user has finished with one student they enter -1 at that point the the average is displayed. Then it is suppose to reset and start with student 2 and so on.

    ***This is the part I am having problems with.
    When the user has not more students to enter grades for -1 in the grade 1 to indicate no more students. Then it should produce the average for all students.
    I realize that this isn't the simpliest code to use but I can not use an array which would make this program so much easier.

    Any assistance would be a big help.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
         int studentquantity = 1;
         int gradenumber = 1;
         int grade;
         int studenttotal = 0;
         int totalnumgrades;
         int overallgrade;
         float average;
         float classaverage;
    
    
    printf("To change change students enter -1\n");
    printf("When completed with list enter -1 as first score\n\n");
    
    do { 
         do {
              do {
    
                       printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
                       scanf("%d", &grade );
                    if (grade > 100)
                       printf("Grade is invalid, try again\n");
                    else
                        break;
    
                    } while ( grade != -1 );
    
                if (grade < 0 ) break; 
    
                  gradenumber = gradenumber + 1;
                  studenttotal = studenttotal + grade;
    
    
            } while ( grade != -1 );
                  gradenumber--;
                  average = ( float ) studenttotal / gradenumber;
                  printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber, average);
    
                   totalnumgrades = gradenumber;
                   overallgrade = studenttotal;
                   gradenumber = 1;
                   grade = 0;
                   studentquantity++;
    
    
    
         } while ( ( gradenumber == 1) && (grade == -1) );
    
                if ( ( gradenumber == 1) && (grade == -1) ) 
                    classaverage = (float) overallgrade / totalnumgrades;
                     printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage);
    
    
    system("PAUSE"); 
    return 0;
    }
    Last edited by hsmith1976; 10-03-2009 at 02:28 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is such a thing as indentation.

    What I can say about the question without trying to read that code: there's nothing here that an array would help with, even a little bit, so that's just a red herring. You just need to check, when a -1 is entered, if it is the first value then stop everything. (Maybe use a flag for that or something.)

  3. #3
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    Note, also receiving help here.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    3
    Yes I have this posted on a couple different sites same information on all trying to get assistance. I was not getting help on these other forums so posted here as well to expand the possiblity of getting help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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