//-------------------------------------------//
//BUG FIX//
while(!flag) {
for (int i = 0; i < strlen(getMi); i++)
if ( !isdigit(getMi[i]) ) {
cout << "Not a digit please enter again: ";
cin.getline(getMi, 30);
i = 0;
}
flag = true;
}
while (atof(getMi) >= expectDrive) {
cout << "\nInvaild--can not enter over 100,000. Reenter: ";
cin.getline(getMi, 30);
}
total += atof(getMi);
if (total >= expectDrive && index != (year - 1)) {
cout << "\nInvaild entry; cant enter over 100,000 within "
<< year << " years; reenter: \n";
index = 0;
total = 0;
continue;
}
//End BUG FIX//
mi[index] = atof(getMi);
cout << "You have " << total << " miles, be sure don't enter over 100,000 a year.\n";
flag = false;
index++;
}
return mi;
delete mi;
}
also is delete mi reachable? if not where should i put it?
Thank You
You return 'mi' right? -> And you will use it after the function call right? -> but when you free'd ( delete[]) 'mi' and use it after your program will become a crash!
That means:
don't delete 'mi' in the function, you can delete it anytime, anywhere, but you have to know the address of 'mi'.
double* getMile(int expectDrive, int year)
{
bool flag = false;
int index = 0;
char getMi[30];
double* mi = newdouble, total = 0;
cout << "Enter the miles will drive each years\n";
while (index < year)
{
cout << "Year " << index + 1 << "/" << year << ": ";
cin.getline(getMi, 30);
while (!flag)
{
for (int i = 0; i < strlen(getMi); i++)
if ( !isdigit(getMi[i]) )
{
cout << "Not a digit please enter again: ";
cin.getline(getMi, 30);
i = 0;
}
flag = true;
}
while (atof(getMi) >= expectDrive)
{
cout << "\nInvaild--can not enter over 100,000. Reenter: ";
cin.getline(getMi, 30);
}
total += atof(getMi);
if (total >= expectDrive && index != (year - 1))
{
cout << "\nInvaild entry; cant enter over 100,000 within "
<< year << " years; reenter: \n";
index = 0;
total = 0;
continue;
}
*mi = atof(getMi); // oh dear, what have you done. You wrote: mi[index]. Read the reason below
cout << "You have " << total << " miles, be sure don't enter over 100,000 a year.\n";
flag = false;
index++;
}
//delete mi; <-- We don't need this anymore. We call it in ' int main() ', before the program is closing. ( We need the address! )
return mi;
}
Reason:
You allocate !!A!! double with:
double* mi = newdouble ;
So, 'mi' can carry only ONE DOUBLE. But you wanted a whole array my friend.
I don't understand why you are using a heap-variable ( A variable, which is allocated with a pointer )? Why not a stack-variable ( the opposite of a heap variable )?
double getMile(int expectDrive, int year)
{
bool flag = false;
int index = 0;
char getMi[30];
double mi, total = 0; // 'mi' is just a double, so we don't need to free it anymore (:
cout << "Enter the miles will drive each years\n";
while (index < year)
{
cout << "Year " << index + 1 << "/" << year << ": ";
cin.getline(getMi, 30);
while (!flag)
{
for (size_t i = 0; i < strlen(getMi); i++)
if ( !isdigit(getMi[i]) )
{
cout << "Not a digit please enter again: ";
cin.getline(getMi, 30);
i = 0;
}
flag = true;
}
while (atof(getMi) >= expectDrive)
{
cout << "\nInvaild--can not enter over 100,000. Reenter: ";
cin.getline(getMi, 30);
}
total += atof(getMi);
if (total >= expectDrive && index != (year - 1))
{
cout << "\nInvaild entry; cant enter over 100,000 within "
<< year << " years; reenter: \n";
index = 0;
total = 0;
continue;
}
mi = atof(getMi);
cout << "You have " << total << " miles, be sure don't enter over 100,000 a year.\n";
flag = false;
index++;
}
return mi;
}
And in 'int main()' we call it like:
cout << getMile(...)
Soo, and the reason why it is working sometimes is: Undefined Behaviour! A bad thing!