confusedcat
confusedcat

Reputation: 79

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?

Upvotes: 4

Views: 27158

Answers (4)

jh314
jh314

Reputation: 27812

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

Upvotes: 3

KhalidGT
KhalidGT

Reputation: 51

#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();
}

Upvotes: 2

jrd1
jrd1

Reputation: 10726

The logic for your problem is:

  1. Keep running while the input is not -1. If input is -1, break/exit execution and display output.
    1. Enter grade.
    2. If the grade is less than or equal to 100 or greater than or equal to 0 perform pass/fail checks:
      1. If the grade is greater than or equal to 50, that person has passed. Increment the number of passes.
      2. 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);

Upvotes: 0

John3136
John3136

Reputation: 29266

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.

Upvotes: 0

Related Questions