variable is being used without being initialized in for loop



The error I get is "The variable 'time' is being used without being initialized."

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;


int main()
{

//commands
int altitude, beam_stregth, currentfuel, time;

double fuel_required;

char AnswerToRerun;

//there's quite a bit of code here but it runs well


for (int i = 0; fuel_required>(currentfuel); i++)
{ //start of for loop

time++;
altitude = altitude-beam_stregth*time*time;
fuel_required = (1-altitude/200000) * beam_stregth;
currentfuel = currentfuel + 10;
cout << time << " min " << " " << altitude << " " <<currentfuel << " " << fuel_required << endl;
cout << "I recommend starting the engine at that time." << endl;

}
well i think time is a reserved keyword, not sure but try to change the variable "time" to "time1" and try again.
it didn't work :(
naming it time should be fine.

The error is exactly what it says it is. Until you initialize a variable, its contents are unknown. So you must not try to "read" from it until you set it to something meaningful.

Bad (what you're doing):
1
2
3
4
int time;  // <- time is not initialized

time++;  // increments time... but it's not initialized, so incrementing it makes
   // no sense! 


Good (what you should be doing):
1
2
3
int time = 0;  // initialize it.  Now you know time==0

time++;  // increments it.  Now it's meaningful because we know time now == 1 
thank you! it worked!
Topic archived. No new replies allowed.