简体   繁体   中英

C++ program won't end on return

I have a problem. I am unable to exit program when i detect an error.

int main()
{
    int N,N1;

    cout<<("Zadejte pocet prvku mnoziny A:")<<endl;
    cin>>N;
        if (N<=0 || cin.fail()){
            cout<<("Nespravny vstup.")<<endl;
            return 1;
        }
    int *A=(int *)malloc(sizeof(int)*N); /***********/

    cout<<("Zadejte prvky mnoziny A:")<<endl;
    napln(A,N);

    /************************************************/

    cout<<("Zadejte pocet prvku mnoziny B:")<<endl;
    cin>>N1;
        if (N1<=0 || cin.fail()){
            cout<<("Nespravny vstup.")<<endl;
            return 1;
        }
    int *B=(int *)malloc(sizeof(int)*N1); /***********/

    cout<<("Zadejte prvky mnoziny B:")<<endl;
    napln(B,N1);

    int *C=(int *)malloc(sizeof(int)*((N1>N)? N1:N)); /***********/

    vypis(C,porovnej(A,B,C,N,N1));

    free(A);
    free(B);
    free(C);
    return 0;
}

int napln(int *p,int n){

        int prvek,i,j;

        for (i=0;i<n;i++){
            cin>>prvek; //dodelat kontrolu
            if (cin.fail()){
                cout<<("Nespravny vstup.")<<endl;
                free(p);
                return 1;
            }
            else{
                p[i]=prvek;
                for(j=0;j<i;j++){
                    if (p[i]==p[j]){
                        cout<<("Nespravny vstup.")<<endl;
                        free(p);
                        return 1;
                    }
                }
            }
        }
    return 1;
}

The problem is when i call napln function once (tried in separated .cpp file) it works perfectly (detecting doubled values and exit) but when i do the same for another dyn. allocated memory it won't exit and continues (message i written into console window but program flow just continues...). Any ideas how to fix it?

I can't understand the identifier in your program, but maybe the problem is you free a memory twice: first is free(p) in function napln , second is free(A) and free(B) in main . Try not to free the pointer in function napln .

return only ever returns from the function you're currently in. You can use the return value of your napln function to decide, from outside, that the program should end, based on the return value. Eg, you could have napln return, let's say 1 on failure and 0 on success and based on the return value, you could do:

if(!napln(A,N))
   return 1; //return from main

C++ also has exceptions as an alternative to controlling your program flow via return values.

Alternatively, you can use the void exit(int status) function to exit your program, whatever your current context, though it's not a very good practice to structure your code like that.

Anyway, the code's not very well structured. For example, you should try not to repeat yourself and make the input request part of your code a function instead of copying and pasting it and changing A to B.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM