4

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?

2
  • 1
    Your while loop is actually a do-while loop. The stuff in brackets after is not part of the loop - it's actually a scope block. This is wrong. Also, your while condition is wrong, the syntax of else if { is invalid, and you should not have a break.
    – paddy
    Aug 2, 2013 at 2:14
  • Note that you ignore students who manage to get a grade of 100 (you should have if (grade <= 100)). This was silently corrected in the accepted answer. Aug 2, 2013 at 3:03

4 Answers 4

3

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
3
  • 1
    Thank you very much! My code is working now. I was thinking I had put the other loops in the while loop. Thanks for correcting me. Aug 2, 2013 at 2:22
  • Note that -2 is a negative number (and should stop the loop according to the specification) but neither the original nor the revised code terminates the loop when -2 is entered. The condition should be while (grade >= 0); Aug 2, 2013 at 2:59
  • From the printf("Enter -1 to end loop"); statement, it looks like -1 is needed to exit loop, which is why I left it there.
    – jh314
    Aug 2, 2013 at 3:15
2
#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();
}
7
  • Here i put your entire condition of logic in the do-while loop. You don't need to use the break statement in this program because there is already a do-while loop.Also , the condition is"while(grade!=1);".
    – KhalidGT
    Aug 2, 2013 at 2:27
  • 1
    Hi Khalid, please consider tabbing your code in and also explaining what you are doing to the OP and put your explanation in the answer itself not in a comment below it Aug 2, 2013 at 2:27
  • 1
    You can also use this codition "while(grade > 0". It is also a very good condition
    – KhalidGT
    Aug 2, 2013 at 2:31
  • Sorry man . I'm new on this site . Next time it won't happen :)
    – KhalidGT
    Aug 2, 2013 at 2:32
  • Thank you very much for all the help! My code is working now :) Aug 2, 2013 at 2:34
0
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.

2
  • 2
    If the OP doesn't know how to write a do or while loop, then I doubt he'd understand what 'refactoring' means. Aug 2, 2013 at 2:22
  • Yeah, I don't know what refactoring means. We're still starting on all the loops and I'm a freshman Computer Science student and this is practically the first time I'm coding. I'm sorry. I'll research on this later. Aug 2, 2013 at 2:32
0

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);
2
  • 1
    Thank you for this! I like how you put the logic in order since it looked easier to do and it's more organized. I will do this next time I make a program. Aug 2, 2013 at 2:31
  • You're welcome, @ThezzaBaluyos. Writing out problems like that is very helpful when writing code. Eventually, you get to a point where you think like that regularly. I'll leave you with an axiom: "Paper is RAM". ;)
    – jrd1
    Aug 2, 2013 at 2:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.