简体   繁体   English

文本出现不止一次

[英]Text appearing more than once

so I am trying to come up with a firefighting system for my science fair project and one of the main things that come with trying to do so is the coding.所以我试图为我的科学博览会项目提出一个消防系统,而尝试这样做的主要事情之一就是编码。 it is still in the early stages but for the final product of what I have so far, the code is unnecessarily repeating itself, and I don't know how to stop it.它仍处于早期阶段,但对于我目前所拥有的最终产品,代码不必要地重复自己,我不知道如何阻止它。 this is the code, so please, if you answer it, paste the fixed code and tell me what is wrong so I don't run into it later.这是代码,所以如果你回答了,请粘贴固定代码并告诉我哪里出了问题,这样我以后就不会遇到它了。 code:代码:

#include <fstream>
using namespace std;
int main() {
   int ave = 0;
   int num;
   //begins ave and basic info being collected
   cout << "how many sensors will there be: " << endl;
   cin >> num;

   int sen[num];
   cout << "enter the heat on the sensors in order: " << endl;
   for( int i=0; i<num; i++ ){
      cin >> sen[i];
      ave = ave + sen[i];
   }

   ave = ave / num;
   cout << ave << endl;
   //end of basic info/average
   bool sens[num];
   for( int i=0; i<num; i++ ){
      if( sen[i] + 60 > ave ){
         sens[i] = 1;
      }
   }

   for( int i=0; i<num; i++ )
      if( sens[i] == 1 ){
         cout << "there is a fire at sensor" << sens[i] << endl;
      }
   }
}

You didn't give us any error logs, we can only guess what went wrong, but I see one thing and I'll explain.你没有给我们任何错误日志,我们只能猜测出了什么问题,但我看到一件事,我会解释。


C++ standard (till C++11) doesn't support variable sized arrays. C++ 标准(直到 C++11)不支持可变大小的数组。

You can't use你不能用

cin >> num;
int sen[num];

You can use:您可以使用:

cin >> num;
int* sen = new int[num];

More info here: link .更多信息在这里: 链接


In your code change int sen[num];在您的代码中更改int sen[num]; for int* sen = new int[num];对于int* sen = new int[num];
and change bool sens[num];并更改bool sens[num]; for bool* sens = new bool[num];对于bool* sens = new bool[num];

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

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