C Program Limit the integer input range - c

I am a new C programming learner, I am trying to make a program which would calculate the GPA of a student on the basis of grade marks input and credits of a subject.
The problem I am having is I want to limit the number of subjects input from 2 to 6 only.
Another problem is I want to limit the user to input integer from 1 to 100 only, instead of any other keywords, special characters (EOF)
I have put the "###" in comment line where I require these modifications.
#include <stdio.h>
int main(void) {
// input user input -- hopefully a number
// temp used to collect garbage characters
// status did the user enter a number?
// counter for keeping track of loop repetition
// no no. of subjects to be entered by user.
// credits credits per subject
// grades grades acheived in each subject (1 to 100).
// grade_value for holding the value of each subject grade (for ex; 80 to hundred is 4.0)
// grade_points Grade points for each subject (credits * grade_value)
// sum sum of total grade points
int counter = 1, subjects, no, credits, grades, status, temp;
float grade_value, grade_point, sum;
printf("Enter number of subjects you took for current semester: ");
status = scanf("%d", & no);
// ### I want to limit this integer input to be >=2 && <=6.
while (status != 1) {
while ((temp = getchar()) != EOF && temp != '\n');
if ((temp < 2) && (temp > 6));
break;
printf("Invalid input... please enter the number of subject again: ");
status = scanf("%d", & no);
}
// ### I want to be this input to block other character inputs than integer from 1 to hundred.
while (counter <= no) {
printf("\nEnter Subject %d grades separated with credits \n", counter);
scanf("%d %d", & grades, & credits);
if ((grades > 0) && (grades <= 29)) {
grade_value = 0;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 30) && (grades <= 34)) {
grade_value = 0.67;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 35) && (grades <= 39)) {
grade_value = 1;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 40) && (grades <= 44)) {
grade_value = 1.33;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 45) && (grades <= 49)) {
grade_value = 1.67;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf(" \nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 50) && (grades <= 54)) {
grade_value = 2;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 55) && (grades <= 59)) {
grade_value = 2.33;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 60) && (grades <= 64)) {
grade_value = 2.67;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 65) && (grades <= 69)) {
grade_value = 3;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 70) && (grades <= 74)) {
grade_value = 3.33;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 75) && (grades <= 79)) {
grade_value = 3.67;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
} else if ((grades >= 80) && (grades <= 100)) {
grade_value = 4;
printf("Grade value for subject %d is: %.2f", counter, grade_value);
grade_point = credits * grade_value;
printf("\nGrade point for subject %d is: %.2f", counter, grade_point);
sum = sum + grade_point;
++counter;
}
// To print a message if user doesnt enter an integer varying from 1 to 100.
else {
printf("\n Error Grade input, Please Key in Again. (1 to 100 only.)");
}
}
printf("\n");
printf("\n");
printf("\nThe GPA is: %.2f", sum);
if (sum <= 49) {
printf("\nYou can register for 2 subjects for next semester.");
} else if ((sum >= 50) && (sum >= 79)) {
printf("\nYou can register for 5 subjects for next semester.");
} else if ((sum >= 80) && (sum <= 100)) {
printf("\nYou can register for 6 subjects for next semester.");
}
printf("\n");
printf("\n");
printf("\n_______________________________________________________");
printf("\nEnd of program");
return 0;
}

A do-while loop is perfect for loops that need to be called at least once and will get rid of your duplicated code. Reading unstructured input, possibly from the keyboard, (one could call, ./a.out < text.txt,) is actually something that is tricky to get right.
Luckily, the C FAQ has lots of advice, eg, http://c-faq.com/stdio/scanfprobs.html. It's going to be very arduous without functions, though. From limited testing, I'm pretty sure that this is a solid way to read the first variable.
#include <stdio.h> /* fgets sscanf */
#include <stdlib.h> /* EXIT_ printf fprintf */
#include <string.h> /* strlen */
int main(void) {
int no;
/* Input number, no \in [2, 6], and make sure that the read cursor is on
the next line. */
do {
char buffer[80];
size_t len;
printf("Enter number of subjects you took for current semester, [2, 6]: ");
/* On the advice of http://c-faq.com/stdio/scanfprobs.html, this reads
into a buffer first. */
if(!fgets(buffer, sizeof buffer, stdin)) {
if(feof(stdin)) {
fprintf(stderr, "Premature EOF.\n");
} else {
/* On IEEE Std 1003.1-2001-conforming systems, this will print
a meaningful error. */
perror("stdin");
}
/* Can't really do anything interactive once stdin has a read
error. */
return EXIT_FAILURE;
}
/* This is always going to be true, but segfaults if not. Paranoid. */
if(!(len = strlen(buffer))) continue;
/* Normally fgets stores a '\n' at the end; check. */
if(buffer[len - 1] != '\n') {
/* Check if the length of the buffer is big enough to hold input. */
if(len >= sizeof buffer - 1) {
int c;
fprintf(stderr, "Line too long.\n");
/* Flush whole line. http://c-faq.com/stdio/stdinflush2.html */
while((c = getchar()) != '\n') {
if(c != EOF) continue;
if(feof(stdin)) fprintf(stderr, "Premature EOF.\n");
else perror("stdin");
return EXIT_FAILURE;
}
continue;
} else {
/* Non-POSIX lines, eg, file without '\n' terminating. */
fprintf(stderr, "Note: line without line break detected.\n");
}
}
/* Parse buffer for a number that's between [2, 6]. Ignore the rest. */
if(sscanf(buffer, "%d", &no) != 1 || no < 2 || no > 6) {
fprintf(stderr, "Invalid input.\n");
continue;
}
/* Now no \in [2, 6]. */
printf("no: %d\n", no);
break;
} while(1);
return EXIT_SUCCESS;
}
One may be able to get away with a subset of this if your teacher enters only well-formed numbers.

Your code has some semantic mistakes. You must never put a semicolon after a if statement. Also you can write a more legible code by spacing blocks of code. Even better, you could refactor some blocks and encapsulate it into functions. Aside of that, see this solutions:
For the first problem, it can be solved with few lines of code. It's a good practice that the user know the restrictions of inputs before enter his input:
/* ### I want to limit this integer input to be >=2 && <=6. */
#include <stdio.h>
int main()
{
int no;
do {
printf("Enter number of subjects you took for current semester (2~6): ");
scanf("%d", &no);
} while (no < 2 || no > 6);
return 0;
}
The other issue can be solved by using the Standard C Library. Just #include <string.h> and #include <ctype.h>
#include <stdio.h>
#include <string.h> /* strlen() */
#include <ctype.h> /* isdigit() */
int main()
{
int n, invalidInput, status;
char strInput[5];
int i; /* for loop */
do {
invalidInput = 0;
printf("Enter a **number** between 1 and 100: ");
fflush(stdin);
status = scanf("%4s", strInput); /* read at maximum 4 chars from stdin */
if (status > 3) {
continue; /* try again */
}
for (i = 0; i < strlen(strInput); ++i) { /* ensure that all characters are numbers */
if (!isdigit(strInput[i])) {
invalidInput = 1;
break;
}
}
if (!invalidInput) {
n = atoi(strInput); /* now your input is a integer */
if (n < 1 || n > 100) {
printf("Error: the input must be between 1 and 100.\n");
invalidInput = 1;
}
}
} while (invalidInput); /* repeat until all chars are digits and they are in the required interval */
return 0;
}

Related

while loop not breaking even with break C programming

Trying to make a GPA calculator.
Here is my code:
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
float fgpa, grade, n;
char reply;
n = 1;
grade = 1;
printf("--- GPA Calculator ---");
while(fgpa < 1 || fgpa > 4)
{
printf("\n\nEnter your current GPA: ");
scanf("%f", &fgpa);
if(fgpa < 1 || fgpa > 4)
printf("Invalid Input! Please re-enter value.");
}
printf("\nUsing the following table to convert grade to points\n\nA+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\nC = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
while(grade > 0, n++)
{
printf("\nEnter points for module %.0f, enter \"0\" if there are no more additional modules: ", n);
scanf("%f", &grade);
printf("%f", grade);
fgpa = (fgpa + grade) / n;
}
fgpa = fgpa* n / (n-1);
n--;
printf("\n\nNumber of modules taken: %.0f\nFinal GPA: %.1f", n, fgpa);
return 0;
}
I've tried using if(grade = 0) break; but its still not breaking the loop even when the grade is correctly read 0.
picture of 0 being read correctly but loop still continuing
There are multiple problems in the code:
fgpa is uninitialized so the first test in the loop has undefined behavior.
you should also test the return value of scanf() to detect invalid or missing input.
while (grade > 0, n++) is incorrect too: you should instead always read the next grade and test its value and break from the loop before incrementing n.
Your averaging method seems incorrect too: you do not give the same weight to every module.
It seems more appropriate for your purpose to use for ever loops (for (;;)), unconditionally read input, check for scanf() success and test the input values explicitly before proceeding with the computations.
Here is a modified version:
#include <stdio.h>
// flush the rest of the input line, return EOF at end of file
int flush(void) {
int c;
while ((c = getchar()) != EOF && c != \n')
continue;
return c;
}
int main() {
float fgpa;
float grade;
int n = 1;
char reply;
printf("--- GPA Calculator ---");
for (;;) {
printf("\n\nEnter your current GPA: ");
if (scanf("%f", &fgpa) == 1) {
if (fgpa >= 1 && fgpa <= 4)
break;
}
} else {
if (flush() == EOF) {
fprintf(stderr, "unexpected end of file\n");
return 1;
}
}
printf("Invalid Input! Please re-enter value.\n");
}
printf("\nUsing the following table to convert grade to points\n\n"
"A+ = 4.0\nA = 4.0\nB+ = 3.5\nB = 3\nC+ = 2.5\n"
"C = 2.0\nD+ = 1.5\nD = 1\nF = 0\n");
for (;;) {
printf("\nEnter points for module %d, enter \"0\" if there are no more additional modules: ", n);
if (scanf("%f", &grade) == 1) {
if (grade <= 0)
break;
printf("%f\n", grade);
fgpa = fgpa + grade;
n = n + 1;
} else {
if (flush() == EOF)
break;
printf("Invalid Input! Please re-enter value.\n");
}
}
fgpa = fgpa / n;
printf("\n\nNumber of modules taken: %d\nFinal GPA: %.3f\n", n, fgpa);
return 0;
}

Percentage calculation problems in C

I am making a program that counts how many passing grades there are based on the conditions. However, when I try calculating the percentage of passing scores, that is, if (passGrade >= 0) { printf("Percent of passing grades are %d! \n", (inputGrade/passGrade)*100); return 0; the program does not show the result, and instead shows up a zero, I did debug the program, and the passGrade, which is the number of total grades I entered in this line, shows up its value printf("You have entered %d passing grades! \n", passGrade); but when it comes to the next line, it just shows up a -1. I did look online for the problem, and nothing shows up.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main()
{
int inputGrade, passGrade, percentGradePass;
inputGrade = 0;
passGrade = 0;
/* printf("Please type a grade (-1) to exit: ");
scanf("%i", &inputGrade);
printf("You typed: %i \n", inputGrade); */
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
scanf("%d", &inputGrade);
printf("You typed: %d \n", inputGrade);
if (inputGrade == -1)
{
inputGrade = -1;
}
else if (inputGrade >= 70 && inputGrade <= 100) {
passGrade = passGrade + 1;
}
else {
inputGrade = 0;
}
}
printf("You have entered %d passing grades! \n", passGrade);
if (passGrade >= 0) {
printf("Percent of passing grades are %d! \n", (inputGrade/passGrade)*100);
return 0;
}
}
Help would be much appreciated.
There are a number of issues with your code. Some issues are critical while others rather represents "strange/unnecessary" code.
Example with comments inline:
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
scanf("%d", &inputGrade); // ALWAYS, ALWAYS... check the return value of scanf
printf("You typed: %d \n", inputGrade);
if (inputGrade == -1)
{
inputGrade = -1; // Unnecessary! inputGrade is already -1 so no need to
// assign it again
}
else if (inputGrade >= 70 && inputGrade <= 100) {
passGrade = passGrade + 1;
}
else {
inputGrade = 0; // Unnecessary! inputGrade will get a new value from scanf
// in the next loop
}
}
So your loop could simply be:
while (inputGrade != -1)
{
printf("Please type a grade (-1) to exit: ");
if (scanf("%d", &inputGrade) != 1)
{
// Invalid input, terminate program
exit(1);
}
printf("You typed: %d \n", inputGrade);
if (inputGrade >= 70 && inputGrade <= 100)
{
passGrade = passGrade + 1;
}
}
Another potential issue is this:
(inputGrade/passGrade)*100
Since both variable have the type int the result of the division will be truncated to nearest integer, e.g. 6/10 will result in 0 instead of 0.6 and the whole expression will consequently become 0. To avoid that you can do the multiplication first (i.e. (100 *inputGrade)/passGrade) or use a floating point type instead of integer types.
That said, it seems that there is a basic misunderstanding in your algorithm. You say:
I try calculating the percentage of passing scores
To calculate the percentage you want
100 * NumberOfPassingGrades / TotalNumberOfGrades
but that is not what you are doing. Instead of using inputGrade in the calculation, you need to count the number of grades and use that in the calculation.
Something like:
int totalGrade = 0;
while (1)
{
printf("Please type a grade (-1) to exit: ");
if (scanf("%d", &inputGrade) != 1)
{
// Invalid input, terminate program
exit(1);
}
if (inputGrade != -1)
{
// Stop the loop
break;
}
printf("You typed: %d \n", inputGrade);
if (inputGrade >= 70 && inputGrade <= 100)
{
passGrade = passGrade + 1;
}
totalGrade = totalGrade + 1;
}
printf("You have entered %d passing grades! \n", passGrade);
printf("You have entered %d grades in total \n", totalGrade);
if (totalGrade > 0)
{
printf("Percent of passing grades are %d! \n", (100 * passGrade)/totalGrade);
}
what does 'the program does not show the result' means?
when your program reaches '(inputGrade/passGrade)*100)'
1/ you should check if 'passGrade' is 0 or not because if it is '0' there will be a run-time error.
2/ and also, since while loop is finished when 'inputGrade == -1', so you need to convert it to 'plus' before divide it
3/ lastly, since 'scanf("%d", &inputGrade);' means inputGrade = 'input' so, you dont need to write 'inputGrade = -1;' in your 'if' case

Average of an arbitrary amount of numbers

I'm currently trying to create a C program for a class assignment that takes the average of an arbitrary amount of test scores. However, I've run into some problems.
My professor has provided an outline to help get started. I also can only add code where indicated, so no extra variables and such.
This is what I have so far:
#include <stdio.h>
void calculateAverage()
{
int grade, count, sum;
double average;
/* add code to input grades, calculate average, and print it */
/* --> between here */
printf("Enter the amount of test scores.\n");
scanf("%d", &count);
grade = 0;
sum = 0;
while (grade != -1 && grade <= 100 && grade >= 0)
{
printf("Enter the grade. Enter -1 when you are done entering grades.\n");
scanf("%d", &grade);
if (grade != -1 && grade <= 100 && grade >= 0)
{
sum = sum + grade;
}
else
{
average = (sum / count);
printf("average is %.2lf \n", &average);
}
}
/* --> and here */
}
int main(void)
{
while (1)
calculateAverage();
return 0;
}
So the problem I've ran into is that with what I have so far, the average will always be calculated as 0. Why exactly is this happening, and how would I fix it so it gives me the correct average?
UPDATE
So I tried casting the average to double so I can avoid a type mismatch, which did get rid of my compiler warnings but the average is still coming out to 0 for all inputed values.
#include <stdio.h>
void calculateAverage()
{
int grade, count, sum;
double average;
/* add code to input grades, calculate average, and print it */
/* --> between here */
printf("Enter the amount of test scores.\n");
scanf("%d", &count);
grade = 0;
sum = 0;
while (grade != -1 && grade <= 100 && grade >= 0)
{
printf("Enter the grade. Enter -1 when you are done entering grades.\n");
scanf("%d", &grade);
if (grade != -1 && grade <= 100 && grade >= 0)
{
sum = sum + grade;
}
else
{
average = (double)(sum / count);
printf("average is %.2f \n", &average);
}
}
/* --> and here */
}
int main(void)
{
while (1)
calculateAverage();
return 0;
}
#include <stdio.h>
void calculateAverage()
{
int grade, count, sum;
double average;
/* add code to input grades, calculate average, and print it */
/* --> between here */
printf("Enter the amount of test scores.\n");
scanf("%d", &count);
grade = 0;
sum = 0;
while (grade != -1 && grade <= 100 && grade >= 0)
{
printf("Enter the grade. Enter -1 when you are done entering grades.\n");
scanf("%d", &grade);
if (grade != -1 && grade <= 100 && grade >= 0)
{
sum = sum + grade;
}
else
{
average = (double)(sum / count);
printf("average is %.2lf \n", average); # <---- Please fix this!
}
}
/* --> and here */
}
int main(void)
{
while (1)
calculateAverage();
return 0;
}
There are a few problems in your code:
One is you are printing the address of average using & operator.
Another thing you can change is to use the correct format specifier when printing double.

GPA scores program

I am supposed to write a program where the user is asked how many students there are in a class. Then, it asks for the GPA of each of those students. In the end, it is supposed to display number of students with each GPA score. So far this is what i have, but it doesn't seem to count properly.
#include <stdio.h>
int main(void){
int cnt1, cnt2, cnt3, cnt4, student, numofsdts, GPA, GPAFreq[4];
printf("Enter number of students: ");
scanf("%d", &numofsdts);
student = 1;
while(student <= numofsdts){
printf("GPA of student number %d: ", student);
scanf("%d", &GPA);
if(GPA < 1 || GPA > 4){
printf("invalid number \n");
}
else{
student++;
}
if(student == numofsdts + 1)
break;
if(GPAFreq[1])
cnt1++;
else if(GPAFreq[2])
cnt2++;
else if(GPAFreq[3])
cnt3++;
else if(GPAFreq[4])
cnt4++;
}
printf("GPA 1: %d students \n", cnt1);
printf("GPA 2: %d students \n", cnt2);
printf("GPA 3: %d students \n", cnt3);
printf("GPA 4: %d students \n", cnt4);
}
Set int cnt1 = 0, cnt2 = 0 etc. (they are not nullified by default, just have some garbage [like a rented room not cleaned explicitly...]).
Also:
if(GPA < 1 || GPA > 4){
printf("invalid number \n");
continue; // skip the rest of the loop body
}
Or a slightly cleaner approach (in full):
#include <stdio.h>
int main(void){
int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0;
int numofsdts, GPA;
printf("Enter number of students: ");
scanf("%d", &numofsdts);
students = 0;
while(students <= numofsdts){
printf("GPA of student number %d: ", students + 1);
scanf("%d", &GPA);
if(GPA < 1 || GPA > 4){
printf("invalid number \n");
continue;
}
if(GPA == 1)
cnt1++;
else if(GPA == 2)
cnt2++;
else if(GPA == 3)
cnt3++;
else if(GPA == 4)
cnt4++;
students++;
}
printf("GPA 1: %d students \n", cnt1);
printf("GPA 2: %d students \n", cnt2);
printf("GPA 3: %d students \n", cnt3);
printf("GPA 4: %d students \n", cnt4);
}
There are multiple errors here. The first is that cnt1-4 have to be initialized prior to being added to. The second is that C uses zero indexing, so GPAFreq[4] is not accessing the fourth element of your array (which would be GPAFreq[3].
The third is that your if statement isn't doing what you think it is. It is evaluating the values inside of your array as boolean variables, i.e. 0 is false, anything else is true. A better approach is to do this:
GPAFreq[GPA - 1] += 1;
This will be counting the frequencies in each of the indices of the array. Then to print them, you just access GPAFreq and no longer need the cnt variables.

C Program that counts how many pass or fail grades and exits when a negative number is inputted

I'm a newbie to C Programming and we're still starting on the loops. For our exercise today, we were tasked to create a do-while program that counts how many pass and fail grades there are but the loop breaks when a negative number is inputted. Also, numbers above 100 is skipped. This is my program:
#include<stdio.h>
#include<conio.h>
int main()
{
int grade, pass, fail;
pass = 0;
fail = 0;
do {
printf("Enter grade:\n");
scanf("%d", &grade);
printf("Enter -1 to end loop");
}
while(grade == -1){
if(grade < 100){
if(grade >= 50){
pass = pass + 1;
}
else if {
fail = fail + 1;
}
}
break;
}
printf("Pass: %d", pass);
printf("Fail: %d", fail);
getch ();
return 0;
}
Can someone please tell me how to improve or where I went wrong?
You need to put all of the code that you loop between the do and the while statements.
do {
printf("Enter -1 to end loop");
printf("Enter grade:\n");
scanf("%d", &grade);
if(grade <= 100 && grade >= 0) {
if(grade >= 50){
pass = pass + 1;
}
else {
fail = fail + 1;
}
}
} while(grade >= 0);
The general structure of a do-while loop is:
do {
// all of the code in the loop goes here
} while (condition);
// <-- everything from here onwards is outside the loop
#include <stdio.h>
#include <conio.h>
int main()
{
int grade, pass, fail;
pass = 0;
fail = 0;
do {
printf("\nEnter grade:\n");
scanf("%d", &grade);
printf("Enter -1 to end loop");
if (grade < 100 && grade >= 50)
pass = pass + 1;
else
fail = fail + 1;
printf("\nPass: %d", pass);
printf("\nFail: %d", fail);
}
while (grade >= 0);
getch();
}
do {
// stuff
}
while {
// more stuff
}
Is mixing 2 concepts together: the while loop and the do while loop - I'd start by refactoring that piece.
The logic for your problem is:
Keep running while the input is not -1. If input is -1, break/exit execution and display output.
Enter grade.
If the grade is less than or equal to 100 or greater than or equal to 0 perform pass/fail checks:
If the grade is greater than or equal to 50, that person has passed. Increment the number of passes.
If the grade is less than 50, that person has failed. Increment the number of failed exams.
jh314's layout logic is correct, but doesn't fix the execution logic:
int grade, pass, fail;
pass = 0;
fail = 0;
do {
printf("Enter -1 to end loop");
printf("Enter grade:\n");
scanf("%d", &grade);
//you want grades that are both less than or equal to 100
//and greater than or equal to 0
if(grade <= 100 && grade >= 0){
if(grade >= 50){
pass = pass + 1;
}
//if the grades are less than 50, that person has failed.
else {
fail = fail + 1;
}
}
} while(grade != -1);
printf("Pass: %d", pass);
printf("Fail: %d", fail);

Resources