0

This example is a very basic and simple program.

I want to be able to exit from a function whenever the user wants to.

In the add() function i'm able to exit on the

cin>>NumA;

line by typing 9999 and it will return me to the menu. However i would only be able to exit at this point, so if i wanted to exit at any time i'd have to add the

if (NumA == 9999)
{
    return;
} 

throught all of the program. I want to be able to exit at any time inside of the function, even better if it can be done by pressing a key, like backspace. I suppose there is a better way to implement this, how can i do this? Maybe another function :^)

void add()
{
cout << "Addition" << endl;
cout << "Number A: ";
int NumA;
cin>>NumA;
if (NumA == 9999)
{
    return;
}
cout << "Number B: ";
int NumB;
cin>>NumB;

int Result = NumA + NumB;
cout << NumA << " + " << NumB << " = " << Result<<endl;
}

int main()
{
int Op;
do
{
    cout << "Main" << endl;
    cout << "1) Add" << endl;
    cout << "2) Another function call" << endl;
    cout << "3) Yet another function call" << endl;
    cout << "n) ..." << endl;
    cout << "0, to exit" << endl;
    cin>>Op;
    switch (Op)
    {
    case 1:
    {
        add();
    }
    default:
    {
        break;
    }
    }
}
while (Op != 0);

}
SwodniwS
  • 103
  • 1
  • 5
  • I'm no expert, but I think this will go into the domain of multithreading. Or a very crude way to do this would be to call a function which listens for a key press after every line. So when you press a key, there's a chance it might notice and stop execution. But this is a HORRIBLE workaround. – therainmaker Oct 29 '15 at 06:05
  • You can sort of use SIGINT here. Sigint is created by the keyboard when you press CTRL + C. See the second answer in (http://stackoverflow.com/questions/482702/using-sigint), and try to define your custom SIGINT handler, somewhat like `void siginthandler(int param)` – badola Oct 29 '15 at 06:30
  • @badola I tried using the code from the second answer that you linked and every time i press ctrl+c it throws and exception and it loads kernel32 symbols saying source not available. I'm on visual studio 2015. I used the exact same code. – SwodniwS Oct 29 '15 at 17:02
  • Well, I tried something on my MacOS and it has been compiled using a g++ compiler. It is too long for a comment, so let me put it as an answer. – badola Oct 29 '15 at 19:07

2 Answers2

0

Most easiest option would be to handle exit point in one of the Switch case statement as follows.

*** Also don't forget to add break statement after every case statement

switch (Op)
{
    case 1:
        add();
    break ;

    case 0:
        return 0 ;
    break ;

    default:
        cout << "Invalid option" << endl;
    break;
}

Thanks

User420
  • 50
  • 7
  • The case 0: will end the program but i just want to exit from add() not from the switch. And yeah, i forgot to add the break; statement. Thanks. – SwodniwS Oct 29 '15 at 15:33
0

Well, I tried to write a small sample program as per your description. I am not sure if it will work on Visual Studio. I compiled it on MacOS using g++. The below code may not be a good way of writing a program, but it sort of works, and maybe helpful for your cause.

I modified your code to make a simpler program for better understanding -

#include <signal.h>
#include <stdlib.h>
#include <iostream>

int sum = 0;
int main(); // Prototyping main() is also not recommended

void siginthandler(int param)
{
    std::cout << "[INTERRUPT] Sum so far => " << sum << "\n";
    main(); // main() shouldn't be called here, but rather some menu function
}

void add()
{
    std::cout << "Addition\n";
    std::cout << "Enter a number : ";
    int NumA;
    std::cin >> NumA;
    sum += NumA;
    std::cout << "[ADD] Sum so far => " << sum << "\n";
}

int main()
{
    // Register the SIGINT handler with the main()
    signal(SIGINT, siginthandler);
    int Op; 
    do  
    {   
        std::cout << "Main" << std::endl;
        std::cout << "1) Add" << std::endl;
        std::cout << "2) Another function call" << std::endl;
        std::cout << "0, to exit" << std::endl;
        std::cin>>Op;
        switch (Op)
        {   
            case 1:
            {   
                add(); break;
            }   
            default:
            {
                break;
            }
        }
    }
    while (Op != 0);
    std::cout << "[MAIN] Sum so far => " << sum << "\n";
    exit(0);
}

It does compile on g++, so I am not sure if it will work for your case or not.

Below is the output I got by executing the program :

Abhinavs-MacBook-Pro:test abhinav$ ./a.out
Main
1) Add
2) Another function call
0, to exit
1
Addition
Enter a number : 34
[ADD] Sum so far => 34
Main
1) Add
2) Another function call
0, to exit
1
Addition
Enter a number : 56
[ADD] Sum so far => 90
Main
1) Add
2) Another function call
0, to exit
^C[INTERRUPT] Sum so far => 90
Main
1) Add
2) Another function call
0, to exit
0
[MAIN] Sum so far => 90
Abhinavs-MacBook-Pro:test abhinav$

I hope the above code helps. May the force be with you..!! :)

badola
  • 820
  • 1
  • 13
  • 26