简体   繁体   English

如何终止程序?

[英]How to terminate the program?

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?一旦用户将 1 输入到决策部分,我就试图终止程序,但即使在用户输入 1 之后它仍然继续要求输入。我在代码中做错了什么或遗漏了什么? 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.未初始化的局部变量,例如s ,具有不确定的值,并且执行例如s++将导致未定义的行为。 The variable s is not the only one you don't initialize and then perform operations on.变量s并不是您未初始化然后对其执行操作的唯一变量。

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.然后当您初始化s ,请记住循环会继续迭代while (s > 0) ,因此如果您将s初始化为零,则执行s++ ,这意味着s大于零并且循环继续。

You should initialize (I recommend) s to zero, and then loop while (s == 0) .您应该将(我推荐) s初始化为零,然后循环while (s == 0)

Or, you know, just break out of the loop:或者,你知道,只是break循环了:

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.您的缩进使 do ... while 循环看起来像是一个无限循环。 You also had an extra close bracket after the scores that removed the while from the do ... while.在从 do ... while 中删除 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一开始把这个改成while

#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现在,当最后一个学生进入标记时,它将按照您的预期退出循环

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM