a bug in my program

if i use execute program it output fine, but if i use command prompt it
came out error and stop at "You have"

double* getMile(int expectDrive, int year) {
bool flag = false;
int index = 0;
char getMi[10];
double* mi = new double, total = 0;

cout << "Enter the miles will drive each years\n";

while(index < year) {
cout << "Year " << index + 1 << "/" << year << ": ";
cin.getline(getMi, 30);

//-------------------------------------------//
//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
Hey,

The problem is simple.
You wrote:
cin.getline(getMi, 30);
But getMi isn't so big ):
He is just 10 bytes big:
char getMi[10];

Why do you don't use std::string (#include <string>) ? It's the C++-way (:

€dit: oh, I didn't see the fricky delete. You have to give delete over return. delete isn't reachable :/ -> Memory Leak.

bye and have fun (:
Last edited on
thank you for letting me know
but it is not the problem

i think the problem is when i use cmd window to open it

it does not go over 10/10 i got error response at here

any way to fix it?
Ahhh, I got it.

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'.

So:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
double* getMile(int expectDrive, int year)
{
    bool flag = false;
    int index = 0;
    char getMi[30];
    double* mi = new double, 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 = new double ;

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 )?

Hmmm, I correct your code a bit... *correct*
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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!


bye and have fun (:
Thank You man
i got it still need to work with pointer ;p
Topic archived. No new replies allowed.