-2

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;
      }
   }
}
Gaben
  • 345
  • 1
  • 12

1 Answers1

0

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.

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]; for int* sen = new int[num];
and change bool sens[num]; for bool* sens = new bool[num];

Gaben
  • 345
  • 1
  • 12
  • Please explain it like as if I were a 5-year-old, I really don't know very much about C++, I forgot everything but the bare basics, and only got back into it for my project – Santi Maldonado Nov 25 '19 at 01:53
  • also, my lord and savior Gaben, bring a heavy update to TF2 – Santi Maldonado Nov 25 '19 at 01:54
  • You must define arrays in a different fashion. In C++ you can't make array the size of "variable" (the size that changes, or is from input). Hovewer you can get the same utilities with "pointers" (my second block of code). – Gaben Nov 25 '19 at 01:55
  • The code executes two times, or does it fall into "never ending loop"? – Gaben Nov 25 '19 at 23:28