Abnormal termination of program? [C]

Jul 22, 2013 at 4:43pm
Hi guys, quick question. I'm getting the "abnormal termination of program" when I'm trying to terminate my C program. I'm not sure why this is happening though (sorry, still a beginner when it comes to programming...)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
main() {
        ...	
	while(opt!=4){
		opt = menu();
		switch(opt){
			case 4:
				clrscr();
				printf("Exiting application...");
				exit(0);
				getch();
				break;
        ...
		}
	}
}

void exit(){
	return;
}
Jul 22, 2013 at 5:00pm
So I made a few changes to the program:

1
2
3
4
5
6
7
8
9
10
...
			case 4:
				clrscr();
				printf("Exiting application...");
				getch();
				exit();
...
void exit(){
	return 0;
}


It's now exiting properly, but the compiler says that a void function may not return a value. Did I do this correctly, having the exit() return 0?
Jul 22, 2013 at 5:06pm
you cannot obviously return a value in a void function as the compiler says , why dont you try to make it an integer return type function. what is your aim in this program exactly?
Jul 22, 2013 at 5:17pm
Did you try the <cstdlib> exit function, rather than writing your own?
http://www.cplusplus.com/reference/cstdlib/exit/
Jul 23, 2013 at 12:33pm
@c0defisher: That's actually just a part of the whole program. Basically, what happens is, when the user selects the 4th option, the application closes.

@Chervil: I'll try looking into that, thanks. If ever, this will be the first time that I'll be using that library.
Jul 23, 2013 at 12:37pm
Thanks guys, the exit() from cstdlib fixed the warning message. Sorry for the rather basic question, I'm still brushing up on my programming! :)
Jul 23, 2013 at 12:54pm
Looking at the logic of the program, the while loop will terminate when opt == 4, so there may not be any need to do anything, other than output the message.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    while (opt!=4)
    {
        opt = menu();
        switch(opt)
        {
            case 4:
                clrscr();
                printf("Exiting application...");
                getch();
                break;
                
            case 5:
               // etc.
               
            
        }
    }


When the break is executed at line 10, that ends the switch block. Then the while condition (opt!=4) is tested. Since it is no longer true, the loop terminates and the program will then exit normally.
Thus, there is no need to use any sort of exit function here.
Last edited on Jul 23, 2013 at 12:55pm
Jul 28, 2013 at 12:44pm
I hadn't realized that until you pointed that out. Thanks!
Topic archived. No new replies allowed.