Thread: how to process multiple students in academic system

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    31

    how to process multiple students in academic system

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void welcome();
    void processOneStudent();
    void returnStudentData(int* studentId, int* class, int* cumulativeHours, float* cumulativeGPA);
    void reportSummary(int* numStudents, int* numInGoodStanding);
    
    int main () {
    
      int numStudents = 0, numInGoodStanding = 0;
      char moreStudents;
      
      welcome();
      
      printf("more students (Y or N)? ");
      scanf("&#37;c", &moreStudents);
    
      if ((moreStudents == 'Y')||(moreStudents == 'y')) {
        numStudents = numStudents + 1;
        processOneStudent();
      }
      
      if ((moreStudents == 'N')||(moreStudents == 'n')) {
        reportSummary(&numStudents, &numInGoodStanding);
      }
      
        return (0);
    
    }
    
    void welcome() {
      printf("*************************************************\n");
      printf("*    Welcome to the Academic Standing System    *\n");
      printf("*           Developed by Hugh Saddler           *\n");
      printf("*************************************************\n\n");
    }
    
    void processOneStudent() {
    
      int studentId, class, cumulativeHours, credit, hour;
      float cumulativeGPA;
    
      printf("Enter student id, class, cum. hours, cum. GPA: ");
      scanf("%d%d%d%f", &studentId, &class, &cumulativeHours, &cumulativeGPA);
    
      do {
      printf("Hours and grade (0 0 to end)? ");
      scanf("%d%d", &credit, &hour);
      cumulativeHours = cumulativeHours + hour;
    }
    
      while ((credit > 0)||(hour > 0));
    
      if((credit == 0)||(hour == 0)) {
        returnStudentData(&studentId, &class, &cumulativeHours, &cumulativeGPA);
      }
    }
    
    void returnStudentData(int* studentId, int* class, int* cumulativeHours, float* cumulativeGPA) {
      printf("\n");
      printf("Student Id: %d\n", *studentId);
      printf("Class: %d\n", *class);
      printf("New cumulative hours: %d\n", *cumulativeHours);
      printf("New cumulative GPA: %.2f\n", *cumulativeGPA);
    }
    
    void reportSummary(int* numStudents, int* numInGoodStanding) {
      printf("\n");
      printf("Number of students = %d\n", *numStudents);
      printf("Number in good standing = %d\n", *numInGoodStanding);
      printf("\n");
      printf("Thank you for using the Academic Standing System. Bye bye!\n");
    }
    I want to be able to process multiple students, but my program ends after processing the first student. How do I tell the program to re-run the "more students (Y or N)?" code and keep doing so until there are no more students left and the user enters "N"?

    Note: I haven't programmed to output the correct GPA, the number of students processed, and the number of students in good standing, so they may appear to be incorrect while reviewing my code. Right now, I just need help with getting the program to process multiple students instead of exiting unexpectedly. Thanks!

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218

    Post

    A while loop would be good for this. See if you can get one working for this. You could put it around this bit of code:
    Code:
      printf("more students (Y or N)? ");
      scanf("%c", &moreStudents);
    
      if ((moreStudents == 'Y')||(moreStudents == 'y')) {
        numStudents = numStudents + 1;
        processOneStudent();
      }
    Then run your report summary at the end without the if statement.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    Ok.

    What parameters would I use for the while loop? That's where I get confused because I thought about using a while loop a few hours ago.

    I'm not sure what you mean by putting the report summary function call at the end without the if statement. I tried doing this and it doesn't respond well when a user enters 'N' or 'n'.

  4. #4
    Registered User
    Join Date
    May 2006
    Location
    WV
    Posts
    11
    Why do you need the second if statement? If they don't say yes, why would you want to continue? Just put while before the first printf, and declare morestudents = 'Y'. Then leave the report summary out of the while loop and out of an if or the while, and you got what you want.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    I'm still not understanding how to write the while loop that everyone is suggesting. My objective is to repeat the "more students (Y or N)?" script and continue processing more students until the user enters 'N' or 'n'. I tried rewriting my program to fit your description, but it's not allowing me to do what I want it to do. If you could post an example of the changes you're referring to, I would greatly appreciate it.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    This is what I get with my original code:

    more students (Y or N)? y
    Enter student id, class, cum. hours, cum. GPA: 1001 2005 10 3.30
    Hours and grade (0 0 to end)? 4 4
    Hours and grade (0 0 to end)? 3 3
    Hours and grade (0 0 to end)? 0 0

    Student Id: 1001
    Class: 2005
    New cumulative hours: 17
    New cumulative GPA: 3.30

    Academic Standing System has exited with status 0.

    What I want the program to do is continue processing more students instead of exiting after the first student has been processed like shown above.
    Last edited by 3MGFX; 10-26-2007 at 06:12 PM.

  7. #7
    Registered User
    Join Date
    May 2006
    Location
    WV
    Posts
    11
    Code:
    moreStudents = 'Y';
    while(moreStudents == 'Y' || moreStudents == 'y'){
    would be my suggestion. Put the brackets where you want it to repeat, then it will exit when the user doesn't press Y or y. If you want to specify that they have to press N or n to exit, you'll need another loop like the one above, but with N and n.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    Ok.

    This is what I have now:
    Code:
    printf("more students (Y or N)? ");
    scanf("&#37;c", &moreStudents);
      
      while ((moreStudents == 'Y')||(moreStudents == 'y')) {
        numStudents = numStudents + 1;
        processOneStudent();
    }
      
        reportSummary(&numStudents, &numInGoodStanding);
      
        return (0);
    It allows me to input information for another student now, but I want it to give me the option of asking the user whether or not there are more students to process. With this version of my program, it automatically assumes that there are more students and skips ahead to the processOneStudent() function. How do I get the program to prompt the user before calling that function?

    And, of course, I want it to prompt the user with the original message: more students (Y or N)?

    I do want to take the time to say that I appreciate it all the help that you've been giving me. Thanks!

    - Hugh

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    I also realized that I don't need to declare " moreStudents = 'Y' " since it returns the same result, which is why I chose not to include it in my revised code.

    Note: Actually, this declaration causes the program to run incorrectly.
    Last edited by 3MGFX; 10-26-2007 at 07:08 PM.

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by 3MGFX View Post
    Code:
      while ((moreStudents == 'Y')||(moreStudents == 'y')) {
        numStudents = numStudents + 1;
        processOneStudent();
    }
    infinite loop.
    Code:
    while (1){  
        processOneStudent();
        printf("more students (Y or N)? ");
      scanf("%c", &moreStudents);
      if (!((moreStudents == 'Y')||(moreStudents == 'y'))) {
            break;
      }
    }
    Last edited by robwhit; 10-26-2007 at 07:34 PM.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    Can you explain how to use this code? And what is an infinite loop?

  12. #12
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    Code:
    printf("more students (Y or N)? ");
    scanf("&#37;c", &moreStudents);
      
      while ((moreStudents == 'Y')||(moreStudents == 'y')) {
        numStudents = numStudents + 1;
        processOneStudent();
        printf("more students (Y or N)? ");
        scanf("%c", &moreStudents);
          if (!((moreStudents == 'Y')||(moreStudents == 'y'))) {
            break;
          }
    }
      
      if ((moreStudents == 'N')||(moreStudents == 'n')) {
        reportSummary(&numStudents, &numInGoodStanding);
    }
      
        return (0);
    yields..

    more students (Y or N)? y
    Enter student id, class, cum. hours, cum. GPA: 1001 2005 10 3.30
    Hours and grade (0 0 to end)? 4 4
    Hours and grade (0 0 to end)? 3 3
    Hours and grade (0 0 to end)? 0 0

    Student Id: 1001
    Class: 2005
    New cumulative hours: 17
    New cumulative GPA: 3.30

    more students (Y or N)?
    Academic Standing System has exited with status 0.

    ..almost there, but it exits before allowing the user input for the next student.

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by 3MGFX View Post
    Can you explain how to use this code? And what is an infinite loop?
    it does the function until you enter in something that it doesn't like.

    it's a loop that doesn't end.
    Last edited by robwhit; 10-26-2007 at 08:32 PM.

  14. #14
    Registered User
    Join Date
    Oct 2007
    Posts
    31
    Fair enough.

    Now how do I incorporate that into my program so that it works properly? I posted my attempt above, and it's pretty close to what I want, but then it exits unexpectedly again..

    any suggestions?

    - Hugh

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    hint: if you do it correctly, you should only need one writing of processOneStudent().

    edit: no need for:
    Code:
    if ((moreStudents == 'N')||(moreStudents == 'n')) {
    if you got that far, the user already wants out.
    Last edited by robwhit; 10-26-2007 at 08:51 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Round Robin Scheduling using c.
    By eclipt in forum C Programming
    Replies: 8
    Last Post: 12-28-2005, 04:58 PM
  2. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 02:41 AM
  3. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  4. Almost finished with Round Robin algorithm
    By Shinobi-wan in forum C++ Programming
    Replies: 2
    Last Post: 12-19-2004, 03:00 PM
  5. Multiple System Commands
    By Denethor2000 in forum C++ Programming
    Replies: 2
    Last Post: 02-21-2004, 08:35 PM