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
Related
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;
}
I'm trying to work on a program to input a phone number and test if it is the right amount of digits in C, also checking if the first value is not 0 or 1. Scanf is not taking a value when I enter it and I do not understand why. If someone could please point out to me what the issue is it'd be much appreciated.
#include <stdio.h>
int main (void){
int number = 0;
int loop = 0;
while(loop == 0){
printf("Enter a phone number: ");
scanf(" %d ", &number);
printf("%d", number);
int firstDigit;
while(number >= 10){
firstDigit = number/10;
}
if((number/1000000) >= 1 && (number/1000000) < 10){
if(firstDigit == 1){
printf("Invalid central office code: 1");
}
else if(firstDigit == 0){
printf("Invalid central office code: 0");
}
else{
int firstThree = (number/10000);
int lastFour = (number%10000);
printf("%d - %d", firstThree, lastFour);
}
}
else if(number == 0){
printf("Exiting.");
loop = 1;
}
else{
if((number/1000000)>=10){
printf("Invalid phone number: too many digits");
}
else if((number/1000000)<1){
printf("Invalid phone number: too few digits");
}
}
}
return 0;
}
This:
while(number >= 10) {
firstDigit = number/10;
}
is an infinite loop, because you are not modifying number.
What you probably want to do is:
while(number >= 10) {
firstDigit = number/10;
number /= 10;
}
You should avoid scanf() to read input. Better use fgets(), and then sscanf() for parsing.
char input[1024]; // This should be large enough
if (!fgets(input, sizeof input, stdin)) {
printf("Input error\n");
return 1;
}
input[strcsnp(input, "\n")] = '\0'; // Because fgets reads \n so remove it
if (sscanf(input, "%d", &number) != 1) {
printf("Parsing error\n");
return 1;
}
// Use number...
Also, scanf() and sscanf() "ignore" the first 0s you type as part of the phone number, so your solution might not be correct. The best way to represent a phone number is either by storing it as a string (as mentioned in a comment), or by defining a phone number structure.
Int size in c is 4 bytes, which can hold values only -2,147,483,648 to 2,147,483,647.
It can't store all 10 digit values.
you can use long
long number = 0;
scanf("%ld", &number);
printf("%ld", number);
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;
}
I'm taking an intro to C programming course, and I'm trying to understand pointers, and how exactly they work. I tried to pass just the variable counter through gameRuntime();, but counter wasn't being returned towards after the while loop. So, I opted to use a pointer. And now I'm in a bigger mess than I started.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int *gameRuntime(int *counter)
{
srand(time(NULL));
int random = rand() % 100 + 1;
printf("> %d <\n", random);
int guess;
*counter = 1;
int local_counter = (*counter);
while ((guess != random) && (local_counter <= 10))
{
printf("\n-----------------------\n");
printf("What is your guess?\n-----------------------\n> ");
scanf("%d", &guess);
if ((guess > 100) || (guess < 1))
{
printf("Please choose a number between 1 and 100\n");
}
else if (guess < random)
{
printf("My number is larger than %d!\n", guess);
local_counter++;
printf("%d guesses left!\n", (11 - local_counter));
}
else if (guess > random)
{
printf("My number is less than %d!\n", guess);
local_counter++;
printf("%d guesses left!\n", (11 - local_counter));
}
else if (guess == random)
{
printf("You guessed it! My number was %d!\n", random);
printf("You found it with just %d guess(es)!\n", local_counter);
}
}
(*counter) = local_counter;
return counter;
}
int main()
{
char quit;
int counter;
int random;
printf("Welcome to the Number Guess Game!\n");
printf("I chose a number between 1 and 100 and you have only 10 chances to guess it!\n\n");
printf("Continue? [Y\\N]\n\n> ");
scanf("%c", &quit);
if ((quit == 'n') || (quit == 'N'))
{
printf("Exiting....");
return 0;
}
else
{
printf("\n=*=+=*=+=*=+=*==*=+=*=+=*=+=*==*=+=*=+=*=+=*==*=+=*=+=+*\n");
printf(" ~ ~ ~ Ready? ~ ~ ~ \n");
printf("=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*=+=*\n");
printf("\n");
printf("\n-----------------------");
printf("\nOk, I've made up my mind!\n");
printf("-----------------------\n");
}
gameRuntime(counter);
printf("\n---%d---\n", counter);
char continueGame;
while ((continueGame != 'N') || (continueGame != 'n'))
{
printf("\n---%d---\n", counter);
if (counter >= 10)
{
printf("SORRY! You could not find my number with 10 guesses!\n");
printf("My number was %d\n", random);
printf("Maybe next time!\n");
printf("\nTry again? [Y\\N]\n");
scanf(" %c", &continueGame);
if ((continueGame == 'Y') || (continueGame == 'y'))
{
gameRuntime(counter);
}
else
{
printf("Thanks for playing! See you later!");
return 0;
}
}
else
{
printf("Play again? [Y\\N]\n> ");
scanf(" %c", &continueGame);
if ((continueGame == 'Y') || (continueGame == 'y'))
{
gameRuntime(counter);
}
else
{
printf("\nThanks for playing! See you later!");
return 0;
}
}
}
}
Any help would be much appreciated, TIA
Change
gameRuntime(counter);
to
gameRuntime(&counter);
You are passing the value of counter, and then you used it like if it was the address of something (you dereferenced the pointer). It does compile because pointers and ints are interchangeable in c, but that doesn't mean that the behavior is defined. It should however, generate a warning about pointer to int conversion. You can also, convert all warnings to errors to prevent compilation in case of a warning.
Warnings can be ignored in very rare cases, so as a rule of thumb make your compiler warn about everything it can and if possible, let it treat warnings as errors.
By applying the suggested fix, you pass the address of counter, which is a pointer holding the location of counter in memory, thus you can then affect the value at that location by dereferencing the pointer.
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);