int main()
{
    Appointment appt;
    int i;
        string *description;
    
    int *day, *month, *hour, *min, *_hour, *_min;
     char chr, chr1, chr2, chr3;
     
     
    string *command;
    command = new string [sizeOfArray];
    int sizeOfArray;
    cin >> sizeOfArray;

    
    
    for(i= 0; i< sizeOfArray; i++){
    cin >> command[i];
    if (command[i] == "cancel"){ // <---- ERROR OCCURS HERE
    appt.cancel(i,description,day,month,hour,min,_hour,_min,chr,chr1);}
    if (command[i] == "select"){
    appt.select(i,chr,chr1);}
     if (command[i] == "move to"){
    appt.moveTo(i,chr,chr1,chr2,chr3,description);}
     if (command[i] == "add"){
    appt.add(i,chr,chr1,chr2,chr3,command,description,day,month,hour,min,_hour,_min);}
     if (command[i] == "quit"){
    break;               }
     if (command[i] == "list"){
    appt.list(i,chr,chr1,chr2,chr3,command,description,day,month,hour,min,_hour,_min);}
    }


system("PAUSE");


}

this is my main function,
the if statement is to check the command, and call the corresponding function,
the code compile happily, but the program just terminate abnormally after i enter the command.

Recommended Answers

All 4 Replies

It shouldn't have compiled, you declare sizeOfArray after you try to use it. Take that value in before you instantiate a string array.

See what I wrote in your other post too, you have a bunch of uninitialized pointers declared at the top of your code which are going to choke your code if you try to use them.

It shouldn't have compiled, you declare sizeOfArray after you try to use it. Take that value in before you instantiate a string array.

See what I wrote in your other post too, you have a bunch of uninitialized pointers declared at the top of your code which are going to choke your code if you try to use them.

i have initialized the pointers in my function(which i havent show you the code), but not in the main(), is it wrong?
And i have declared sizeOfArray as global variable, i just forgot to remove it in my main()

sO, what's wrong with my code :(

lines 12, 13 and 14 are in the wrong order. Move line 12 down below line 14, when the value of sizeOfArray is known. You have to do things in logical sequence -- first get the value of sizeOfArray from the keyboard and then allocate the character array of that size.

You have a whole bunch of uninitialized pointers at line 7:

int *day, *month, *hour, *min, *_hour, *_min;

Since you send those pointers by value to the function calls that you have, the only way that this program will not crash is if you don't use those parameters at all in the functions (which would be useless). You need to allocate memory somewhere and set those pointers to point to that memory. I understand that you are sending pointers to your functions such that your functions can set values of "min" "hour" etc., in that case, you should declare those as "int" (not int*) and then send them to the function as "&hour" or "&min" etc. (or better yet, use pass-by-reference instead).

commented: He'll get it eventually... +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.