Skip to content
← Back to Community
Continuous Loop - How to End?
Profile icon

Hi there. I'm trying to create a program that calculates a number of students' GPAs based off of the number of classes and grades they provide. It won't calculate the GPA correctly (probably something to do with assigning the grade points in the switch loop) and then it creates an infinite loop. Please let me know what I am doing wrong? Thanks!

Profile icon
Profile icon
Profile icon

There is quite a bit wrong with your code. Here is some feedback:

  • The function to work out the grade point average does not need to take in any values as all the ones it needs is supplied by the user when the are asked for them from inside the function
  • The function returns a value so the function when it is called in main needs a value to assign the return to
  • There needs to be a for loop in main that asks for the student's ID then calls the averaging function which returns the GPA. The number of times this function is called is equal to the number of students in the class. Each student's GPA and ID can be printed out from inside this for loop.
  • When the user enters a grade it can be stored in a single character variable as the grade is a single character and not a string.
  • Each case option needs a break statement after it - look up a reference on how to use a switch in C
  • If you find that an input is being skipped over use the getchar function to clear the newline character from the buffer after each call to scanf. The newline (\n) is automatically put there when the user presses the Enter key.

Reply to this post if you want me to look over your code again. All the best. C is hard isn't it! Python is so much easier to learn.