简体   繁体   English

用户输入后程序结束

[英]Program ends after user input

Program that I wrote stops after user input, and I don't have any idea why this is happening. 我编写的程序在用户输入后停止,我不知道为什么会这样。 User should give 6 integers, and after that program should do some math with it which isn't the case right now, because after inserting integers it ends, with no error. 用户应给定6个整数,然后该程序应对其进行一些数学运算,而现在情况并非如此,因为在插入整数后,该运算符将结束且没有错误。

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


 int main(int argc, char *argv[]) {

    int T,i;
    int x[5];

     scanf("%d", &T);
     while(T > 0) {
         for(i = 0; i <= 4; i++) {
             scanf("%d", &x[i]);
          }
          int size_tax = x[0] + x[1] + x[2];
          int tax[size_tax];

          for(i = 0; i <= size_tax; i++) {
              if(i == 0) {
                   tax[i] = x[0];
                   if(x[4] == 1) {
                       printf("%d", tax[0]);
                       break;
                    } 
              } else if(i > 0 && i <= x[1] - 1) {
                  tax[i] = tax[i-1] + 1;
                  if(x[4] == i + 1) {
                      printf("%d", tax[i]);
                      break;
                   }
              } else if(i > x[1] - 1 && i <= x[2] - 1) {
                  tax[i] = tax[i-1] * 2;
                      if(x[4] == i + 1) {
                      printf("%d", tax[i]);
                      break;
                    }
              }
          }

         if(x[4] > x[0] + x[1] + x[2]) {
              int result = x[3] - 1;
              for(i = x[3]; i <= x[4] - 1; i++) {
                  result = result * x[i];

              }

              printf("%d", result);
        }

         T--;
      }
 } 

Here is the problem: 这是问题所在:

int tax[size_tax];

for(i = 0; i <= size_tax; i++) {
     ...
}

The declared size of tax is size_tax , so the loop must go up to size_tax-1 , so it should be 声明的tax大小为size_tax ,因此循环必须达到size_tax-1 ,因此应该为

for(i = 0; i < size_tax; i++) { // Less than, not less than or equal to
     ...
}

If the input your testing is 1 2 3 4 5 6 then that's perfectly normal. 如果您的测试输入是1 2 3 4 5 6,那完全正常。 Just insert an else after the last else if. 只需在最后一个else后面插入一个else即可。 It seems this input won't verify any other condition. 看来此输入不会验证任何其他条件。

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

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