EDIT: Title of thread should read "Return statement affected by other return statements"
Hi all, I have a piece of code that resembles the following:
Code:
int locomotives_required (double tons_per_wagon, double train_tons, double gradient)
{
double tons_per_axle_variable = tons_per_axle(tons_per_wagon);
ifstream ifs;
ifs.open ("C:\\C++ Projects\\Coal Logistics\\LoadTables.txt", std::ofstream::out | std::ofstream::app);
string lineread;
getline (ifs, lineread);
char* wordptr = tokenizer(lineread);
int number = atoi(wordptr);
int j;
int locomotive_counter = 0;
if (tons_per_axle_variable < number)
return ERROR_LOW_WAGON_WEIGHT;
for (int i = 1; *wordptr != 'EOF'; i++) //select the correct table
{
wordptr = tokenizer("NULL");
if (tons_per_axle_variable < atof(wordptr))
for (j = 1; j <= 18; j++)
{
getline (ifs, lineread);
if (gradient == atof(tokenizer(lineread)))
for (int k = 0; k <= 6; k++)
{
locomotive_counter++;
if (train_tons <= atof(tokenizer("NULL")))
return 3;
}
}
if (j == 19)
return ERROR_HIGH_GRADIENT;
else
{
getline(ifs,lineread);
while (lineread != "")
getline(ifs,lineread);
getline(ifs,lineread);
wordptr = tokenizer(lineread);
}
}
if (*wordptr == 'EOF')
return ERROR_HIGH_WAGON_WEIGHT;
}
Neglecting all the distractions, please look at line number 26. I have written this piece of code such that this called function always returns at line 26, which should return the number 3. I ran a debugger and checked step by step to make sure the function returns at line 26 and it does.
Note that in theory, the return keywords at lines 30 and 41 should have no bearing on the outcome of returning the value "3" at line 26.
Yet when I run this program and print the outcome I always get a 6-8 digit random number every time like: 767172 and 393620 etc...
To solve this problem I COMMENT OUT lines 30 and 41 and rather make an empty statement like " ;" and finally the function returns the value 3 as it should.
Can anybody explain this???