简体   繁体   English

对于循环不停止

[英]For Loop Not Stopping

I have a for loop in a function that won't stop. 我在一个不会停止的函数中有一个for循环。 Here is the entire function which is used for finding a person's grades, adding them together, then displaying the average onscreen: 这是整个函数,用于查找人的成绩,将它们添加到一起,然后在屏幕上显示平均值:

float Student::average() { 
   cout << "How many grades would you like to enter? (Up to ten)\n";
   float x;
   cin >> x;

   cout << "What is your first grade?";
   cin >> grade[0];
   int i = 1;
   for (i; i = x; i++) {
      cout << "What is the next number?\n";
      cin >> grade[i];
   }
   averageGrade = std::accumulate(grade, grade+10, 0.0);
   averageGrade = averageGrade / 10;

   return averageGrade;
}

And here is the for loop on its own: 这是for循环:

for (i; i = x; i++) {
   cout << "What is the next number?\n";
   cin >> grade[i];
}

An error also outputs (but still allows the program to run) saying: 错误也输出(但仍然允许程序运行)说:

1>c:\\users\\hastudent\\documents\\visual studio 2008\\projects\\weapons\\weapons\\weapon.cpp(25) : warning C4244: '=' : conversion from 'float' to 'int', possible loss of data 1> c:\\ users \\ hastudent \\ documents \\ visual studio 2008 \\ projects \\ weapon \\ weapon \\ weapon.cpp(25):警告C4244:'=':从'float'转换为'int',可能会丢失数据

1>c:\\users\\hastudent\\documents\\visual studio 2008\\projects\\weapons\\weapons\\weapon.cpp(30) : warning C4244: '=' : conversion from 'double' to 'float', possible loss of data 1> c:\\ users \\ hastudent \\ documents \\ visual studio 2008 \\ projects \\ weapon \\ weapon \\ weapon.cpp(30):警告C4244:'=':从'double'转换为'float',可能会丢失数据

The program gets up to the point where it ask for the next number. 程序达到要求下一个数字的程度。 Then you input the number and it keeps asking you that forever. 然后你输入数字,它一直在问你这个。

The for loop should read: for循环应为:

for (int i = 1; i < x; i++)

What you have now ( i = x ) is the assignment of x to i , whereas you probably intended a comparison . 你现在拥有的( i = x )是xi赋值 ,而你可能想要进行比较 In this case, the correct comparison to use is "less-than" ( i < x ). 在这种情况下,使用的正确比较是“小于”( i < x )。

Your for loop ran forever because: 你的for循环永远运行,因为:

  • i = x is an assignment, rather than an equality test. i = x是一个赋值,而不是一个相等的测试。
  • its value is the value of the left-hand-side 它的值是左侧的值
  • you gave it non-zero values 你给它非零值
  • and a non-zero value is true in C++. 并且在C ++中为非零值。

If you want an equality test, use ==. 如果要进行相等性测试,请使用==。 However, in this case, you want a < thest. 但是,在这种情况下,你想要一个<thest。

Have you tried printing the value of i in the for loop? 你试过在for循环中打印i的值吗? You start at x, and keep incrementing. 你从x开始,并继续递增。

That should probably be: 这应该是:

for(int i=0; i<x; i++) { /* do something */ }

Usually you start counting from 0, not 1. 通常你从0开始计数,而不是1。

The main issue is that where you do i=x you want to put instead the condition that must hold at each iteration of the loop (that is called the loop invariant ), that is i < x if you start with i=0, or i==x as others have suggested, if you start counting from i=1. 主要的问题是你在哪里做i = x你想要放置在循环的每次迭代中必须保持的条件(称为循环不变量 ),即如果你以i = 0开始,则i <x,或者如果你从i = 1开始计算,i == x正如其他人所建议的那样。

The second part of the for loop is the "When to continue" condition, not "when to stop". for循环的第二部分是“何时继续”条件,而不是“何时停止”。

Since you start with index 1 (unconventional but not unheard of in C++) you should iterate up to the number in question: 既然你从索引1开始(非常规但在C ++中没有闻所未闻),你应该迭代到有问题的数字:

for(i = 1; i <= x; ++i)

Note that even if the condition were "when to quit" = is assignment in C++ while == is equality comparison. 请注意,即使条件是“何时退出” =是C ++中的赋值,而==是相等比较。

在FOR循环中使用!=(比较运算符)而不是=(赋值运算符)。

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

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