Looping for checking input integer

Joined
Feb 13, 2023
Messages
4
Reaction score
0
I want to write a code for evaluating average result of exam marks. In my program, I want to include one function which will repeatedly ask for the correct range of marks defined by the principles rather the impossible. In this case, marks mustn't be smaller than zero or greater than 100. However if a user types a character accidentally, the code will keep on going. That is why I try to fix mine to actively respond when a letter or whatever is entered. But, the looping is somewhere at one point must be wrong, since I keep getting the result of infinite loop. That is why I am here asking for some help in guiding me. Thank you so much for your help. I use C language.
looping1.png
 
Last edited:
Joined
Jan 30, 2023
Messages
107
Reaction score
13
I want to write a code for evaluating average result of exam marks. In my program, I want to include one function which will repeatedly ask for the correct range of marks defined by the principles rather the impossible. In this case, marks mustn't be smaller than zero or greater than 100. However if a user types a character accidentally, the code will keep on going. That is why I try to fix mine to actively respond when a letter or whatever is entered. But, the looping is somewhere at one point must be wrong, since I keep getting the result of infinite loop. That is why I am here asking for some help in guiding me. Thank you so much for your help. I use C language.View attachment 2421
It sounds like you are trying to create a program in C that calculates the average of exam marks and ensures that the marks are within the acceptable range. To do this, you will need to use a loop that repeatedly asks the user for a mark until a valid input is entered.

To detect and handle invalid inputs, you can use the scanf() function to read user input, and then check whether the input matches the expected format. If the input is not valid, you can clear the input buffer using fflush() and ask the user to enter a valid input again.

Here is an example code snippet that demonstrates how you could accomplish this:

Code:
#include <stdio.h>

int main() {
  int num_students, i, mark;
  float total_marks = 0, average_mark;
 
  printf("Enter the number of students: ");
  scanf("%d", &num_students);
 
  for (i = 0; i < num_students; i++) {
    printf("Enter the mark for student %d: ", i+1);
    while (scanf("%d", &mark) != 1 || mark < 0 || mark > 100) {
      fflush(stdin);
      printf("Invalid input, please enter a mark between 0 and 100: ");
    }
    total_marks += mark;
  }
 
  average_mark = total_marks / num_students;
  printf("The average mark is: %.2f\n", average_mark);
 
  return 0;
}
 
Joined
Feb 13, 2023
Messages
4
Reaction score
0
It sounds like you are trying to create a program in C that calculates the average of exam marks and ensures that the marks are within the acceptable range. To do this, you will need to use a loop that repeatedly asks the user for a mark until a valid input is entered.

To detect and handle invalid inputs, you can use the scanf() function to read user input, and then check whether the input matches the expected format. If the input is not valid, you can clear the input buffer using fflush() and ask the user to enter a valid input again.

Here is an example code snippet that demonstrates how you could accomplish this:

Code:
#include <stdio.h>

int main() {
  int num_students, i, mark;
  float total_marks = 0, average_mark;
 
  printf("Enter the number of students: ");
  scanf("%d", &num_students);
 
  for (i = 0; i < num_students; i++) {
    printf("Enter the mark for student %d: ", i+1);
    while (scanf("%d", &mark) != 1 || mark < 0 || mark > 100) {
      fflush(stdin);
      printf("Invalid input, please enter a mark between 0 and 100: ");
    }
    total_marks += mark;
  }
 
  average_mark = total_marks / num_students;
  printf("The average mark is: %.2f\n", average_mark);
 
  return 0;
}

It sounds like you are trying to create a program in C that calculates the average of exam marks and ensures that the marks are within the acceptable range. To do this, you will need to use a loop that repeatedly asks the user for a mark until a valid input is entered.

To detect and handle invalid inputs, you can use the scanf() function to read user input, and then check whether the input matches the expected format. If the input is not valid, you can clear the input buffer using fflush() and ask the user to enter a valid input again.

Here is an example code snippet that demonstrates how you could accomplish this:

Code:
#include <stdio.h>

int main() {
  int num_students, i, mark;
  float total_marks = 0, average_mark;
 
  printf("Enter the number of students: ");
  scanf("%d", &num_students);
 
  for (i = 0; i < num_students; i++) {
    printf("Enter the mark for student %d: ", i+1);
    while (scanf("%d", &mark) != 1 || mark < 0 || mark > 100) {
      fflush(stdin);
      printf("Invalid input, please enter a mark between 0 and 100: ");
    }
    total_marks += mark;
  }
 
  average_mark = total_marks / num_students;
  printf("The average mark is: %.2f\n", average_mark);
 
  return 0;
}
Thank you so much. Now, I got an idea how to solve it. Thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top