I'm trying to terminate the program once the user inputs 1 into the decision part, but it still keeps on asking for input even after the user inputs a 1. What did I do wrong or missed in the code? Please help, I don't seem to get what's wrong with it.
#include <stdio.h>
int main()
{
int H, N, mark, s, n, last;
/*Student Marks Input, Grade Output/Loop*/
do
{
printf("Please enter your marks:");
scanf("%i", &mark);
if(mark>100)
{
printf("Invalid Input\n");
printf("Re-enter your marks:");
scanf("%i",&mark);
}
if(mark>=80)
{ H++;
printf("You got a H\n");
}
else
if(mark>=70)
{
printf("You got a D\n");
}
else
if(mark>=60)
{
printf("You got a C\n");
}
else
if(mark>=50)
{
printf("You got a P\n");
}
else
if(mark<=49)
{
N++;
printf("You got an N\n");
}
/*Decisions*/
printf("Are you the last student?(Y=1/N=0):");
scanf("%i", &last);
if(last==0)
{
n++;
}
else if (last==1)
{
s++;
}
}
while(s>0);
/*Results*/
if(H>N)
printf("Good Results");
else
printf("Bad Results");
return 0;
}
To begin with, you have undefined behavior in your code, as you do operations on an uninitialized variable.
Uninitialized local variables, like for example s
, have an indeterminate value, and doing eg s++
will lead to undefined behavior. The variable s
is not the only one you don't initialize and then perform operations on.
Then when you initialize s
, remember that the loop keeps on iterating while (s > 0)
, so if you initialize s
to zero, then do s++
that means that s
will be larger than zero and the loop continues.
You should initialize (I recommend) s
to zero, and then loop while (s == 0)
.
Or, you know, just break
out of the loop:
if (last == 1)
break;
// No else, no special loop condition needed, loop can be infinite
Your indentation made the do ... while loop look like it was an infinite loop. You also had an extra close bracket after the scores that removed the while from the do ... while. That actually made it an infinite loop.
#include <stdio.h>
int main()
{
int H, N, mark, s, n=0, last;
/*Student Marks Input, Grade Output/Loop*/
do
{
// processing
if(last==0)
{
n++;
}
else if (last==1)
{
s++;
}
} // This converts the do ... while into an infinite loop
while(s>0); // This is an invalid while since it never gets here
Change this to a while at the beginning
#include <stdio.h>
int main()
{
int H, N, mark, last;
int s = 0;
int mark = 0;
/*Student Marks Input, Grade Output/Loop*/
while (s < 1) // First loop runs sinc s is initialized to 0.
{
// Get the entry for the next pass through the loop.
printf("Please enter your marks:");
scanf("%d", &mark);
// Perform your processing
/*Decisions*/
printf("Are you the last student?(Y=1/N=0):");
scanf("%i", &last);
if(last==0)
{
n++;
}
else if (last==1)
{
s++;
}
// This is the end of the while loop
}
/*Results*/
if(H>N)
printf("Good Results");
else
printf("Bad Results");
return 0;
}
Now it will exit the loop as you intended when the last student enters the mark
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.