Student classes do not work

I want to create a program that asks the user for 10 ratings and then filters them to pass and fail, then prints the number of passes and fails. I made a program but the result is wrong.

int pass,fail,grade,studentcounter;

pass=0;
fail=0;
grade=0;
studentcounter=10;

while (studentcounter!=0)
{

    printf("enter the next grade\n");
    scanf("%d",grade);
    student--;
}

switch (grade)
{
case 1:
    if (grade >= 50)
        pass++;
    break;
case 2:
    if (grade <= 49)
        fail++;
    break;
}

}
printf("the number of fail is %d",fail);
printf("the number of pass is %d",pass);

}

      

The problem is that the program asks for ten evaluations, but at the end it will print the rejection number and the pass number as zero. Why?

+1


source to share


7 replies


You enter the class number. The switch statement checks that the score is relative to the cases, and I'm pretty sure the scores are not 1 or 2 percent. An if statement would be a more logical choice in this situation.

Second, you have a block of code that is never used. First you set studentcounter to zero, then you say "Only execute this block when studentcounter is NOT zero" ...

studentcounter=0;

while (studentcounter!=0) {

printf("enter the next grade\n");

scanf("%d",grade);

student--;

}

      

The third problem is that you were wrong.



you can rewrite your code like this:

#include <stdio.h>
#include <stdlib.h>

int main()
{
int pass,fail,grade,studentcounter;

pass=0;
fail=0;
grade=0;
studentcounter=0;

while (studentcounter < 10) {

printf("enter the next grade:\n");

scanf("%d",&grade);

if (grade >= 50) {
    pass++;
} else {
    fail++;
}

studentcounter++;
}


printf("the number of fail is: %d \n",fail);
printf("the number of pass is: %d \n",pass);
return 0;
}

      

Sorry if I missed something; I don't have time to throw it into my editor: P

amuses

+4


source


you need to keep the pass / fail counter inside the while loop, the class variable will be overwritten on every input.



edit: also, don't use switch statement.

0


source


Ok, the code presented is a bunch of crap. I'll leave this as an exercise for you to figure out what I've fixed for you:

#include <stdio.h>

int main(int argc, char *argv[])
{
    int pass = 0;
    int fail = 0;
    int grade= 0;
    int studentcounter;

    for (studentcounter=1; studentcounter<=10; studentcounter++ )
    {
        printf("Enter grade for student #%-2d  :", studentcounter);
        scanf("%d",&grade);

        if(grade >=50)
            pass++;

        if(grade<=49)
            fail++;
    }
    printf("the number of fail is %d\n",fail);
    printf("the number of pass is %d\n",pass);
}

      

PS. If this is homework, please give credit when you turn it on. You can learn a lot by looking at other people's code, but you never pretend to create something that you don't have.

0


source


Operators

C 'cases don't work the way you expect.

They work with "constant expressions", which means that in order to accomplish the task of "Determine if a class passed or failed," a collapsed switch statement of the form

  switch (grade)
  {
    case 100:
    case 99:
    case 98:
    case 97:
    case 96:
    case 95:
    case 94:
    case 93:
    case 92:
    case 91:
    case 90:
    case 89:
    case 88:
    case 87:
    case 86:
    case 85:
    case 84:
    case 83:
    case 82:
    case 81:
    case 80:
    case 79:
    case 78:
    case 77:
    case 76:
    case 75:
    case 74:
    case 73:
    case 72:
    case 71:
    case 70:
    case 69:
    case 68:
    case 67:
    case 66:
    case 65:
    case 64:
    case 63:
    case 62:
    case 61:
    case 60:
    case 59:
    case 58:
    case 57:
    case 56:
    case 55:
    case 54:
    case 53:
    case 52:
    case 51:
    case 50:
      pass ++;    
      break;
    default:
      fail ++;    
      break;
  }

This will probably give you the correct answer, however you can get "bad luck" in your homework for writing such a monster.

I would try to think of another problem that has "while, switch and if" statements, or perhaps you could change your current program so that instead of accepting 10 evaluations, it asks the user if they want to enter another class and only accepted the answer "Y", "y" or "N", "n", so the "switch" to this answer continues.

0


source


By the way, there is a solution that uses the "switch" in a smart way. I won't discuss the advantages / pitfalls of this approach or the arcane nature of the "C" language, but just to prove it, here it is:

#include & ltstdio.h & gt

int main (void)
{   
    int pass, fail, grade, answer, studentcounter;

    pass = 0;
    fail = 0;
    grade = 0;
    studentcounter = 10;

    while (studentcounter--)
    { 

        printf ("enter the next grade \ n");    
        scanf ("% d", & grade);
        answer = (grade> 49);
        switch (answer)
        {
            case 1:
                pass ++;
                break;
            default:
                fail ++;
                break;
        }

    }

    printf ("the number of fail is% d \ n", fail);
    printf ("the number of pass is% d \ n", pass);
}
0


source


hmm, since you have to use swicth, I suggest this: Store a variable that checks if the class is over 50 or not, then use that variable inside a radio button:


char pass_fail = (grade >= 50)? y : n;
switch( pass_fail )
{
case 'y': 
     // print pass
     break;
case 'n':
    // print fail
    break;
}

      


hope this helps you

0


source


2 errors:

1: The while loop must end after the switch statement.
2: Your variable name is "studentcounter", but you are decreasing "student".
Check out the following code. This should work.

void main()
{
    int pass,fail,grade,studentcounter;

    pass=0;
    fail=0;
    grade=0;
    studentcounter=10;

    while (studentcounter!=0)
    {
        printf("enter the next grade\n");
        scanf("%d",grade);
        studentcounter--;

        switch (grade)
        {
            case 1:
                if (grade >= 50)
                    pass++;
                break;
            case 2:
                if (grade <= 49)
                    fail++;
                break;
        }

    }
    printf("the number of fail is %d",fail);
    printf("the number of pass is %d",pass);

}

      

0


source







All Articles