Home > OS >  How to stop program after putting negative value in C
How to stop program after putting negative value in C

Time:10-11

I am little bit lost over here. I did a program in C which do grading but im not sure how to keep the program open until I put a negative value (-4 for example). I am not sure if I have to change int to char. But to me its not it. I just don't know how to keep the program up until the negative value. Thats the program I wrote:

#include <stdio.h>



int main()

{

int a;

while(a --> 13)
{  

scanf("%d", &a);

if(a<0)
{
    break;
}
else if(a > 100)
{
    printf("NA \n");
    continue;
}
else if(a >=86 && a <=100)
{
    printf("1 \n");
    continue;
}
else if(a >= 61 && a <=85)
{
    printf("2 \n");
    continue;
}
else if(a >= 51 && a <= 60)
{
    printf("3 \n");
    continue;
}
else if(a >= 0 && a<= 50)
{
    printf("4 \n");
    continue;
    
}
scanf("%d \n", &a);
return 0;

}
}

I change it like this but it still doesn't work like it should be..:

#include <stdio.h>
int main()
{
int a;

while (a)
{

if(a>=86 && a<=100)
{
    printf("1 \n");
}
else if(a >= 61&& a <=85)
{
    printf("2 \n");
}
else if(a >= 51&& a <=60)
{
    printf("3 \n");
}
else if(a >= 0 && a<= 50)
{
    printf("4 \n");
}
else if (a>100)
{
    printf("NA \n");
}
else if (a<0)
{
    break;
}
scanf("%d",&a);
}

}

neither like this:

#include <stdio.h>
int main()
{
int a;

while (a<0)
{
  break;
}
if(a>=86 && a<=100)
{
    printf("1 \n");
}
else if(a >= 61&& a <=85)
{
    printf("2 \n");
}
else if(a >= 51&& a <=60)
{
    printf("3 \n");
}
else if(a >= 0 && a<= 50)
{
    printf("4 \n");
}
else if (a>100)
{
    printf("NA \n");
}

scanf("%d",&a);


}

In this case. The program doesn't stop but works only for first input, then it does nothing

int main()

{
int a;
do
{
scanf("%d",&a);
if(a>=86 && a<=100)
{
    printf("1 \n");
}
else if(a >= 61 && a <=85)
{
    printf("2 \n");
}
else if(a >= 51 && a <=60)
{
    printf("3 \n");
}
else if(a >= 0 && a<= 50)
{
    printf("4 \n");
}
else if (a>100)
{
    printf("NA \n");
}

scanf("%d",&a);

} while (a>0);

}

CodePudding user response:

You could use a do...while cycle, for example:

do {
    /* read a and do stuff */

} while (a > 0);

Or a while cycle

/* read a */
while(a > 0) {
    /* do stuff */
    /* read a */
}

EDIT: fix of your code:

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

int main()
{
    int a;
   
    /* read a before the cycle */
    if(scanf("%d",&a) != 1) {
        fprintf(stderr, "scanf fail!\n");
        exit(EXIT_FAILURE);
    }

    while (a > 0)
    {
        if(a>=86 && a<=100)
        {
            printf("1 \n");
        }
        else if(a >= 61 && a <=85)
        {
            printf("2 \n");
        }
        else if(a >= 51 && a <=60)
        {
            printf("3 \n");
        }
        else if(a >= 0 && a<= 50)
        {
            printf("4 \n");
        }
        else if (a>100)
        {
            printf("NA \n");
        }

        /* update the vaule of a */
        if(scanf("%d",&a) != 1) {
            fprintf(stderr, "scanf fail!\n");
            exit(EXIT_FAILURE);
        }
    }
}

do..while solution:

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

int main()
{
    int a;      

    do
    {
        if(scanf("%d",&a) != 1) {
            fprintf(stderr, "scanf fail!\n");
            exit(EXIT_FAILURE);
        }

        if(a>=86 && a<=100)
        {
            printf("1 \n");
        }
        else if(a >= 61 && a <=85)
        {
            printf("2 \n");
        }
        else if(a >= 51 && a <=60)
        {
            printf("3 \n");
        }
        else if(a >= 0 && a<= 50)
        {
            printf("4 \n");
        }
        else if (a>100)
        {
            printf("NA \n");
        }
    } while (a > 0);
}
  •  Tags:  
  • c
  • Related