【问题标题】:C++ Being able to exit from a function at any given timeC++ 能够在任何给定时间退出函数
【发布时间】:2016-01-29 04:00:42
【问题描述】:

这个例子是一个非常基础和简单的程序。

我希望能够在用户想要的时候退出函数。

在 add() 函数中,我可以在

上退出
cin>>NumA;

输入 9999 ,它会返回菜单。但是我现在只能退出,所以如果我想随时退出,我必须添加

if (NumA == 9999)
{
    return;
} 

贯穿整个程序。我希望能够在函数内部随时退出,如果可以通过按键来完成,比如退格键,那就更好了。 我想有更好的方法来实现这一点,我该怎么做?也许另一个功能:^)

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

}

【问题讨论】:

  • 我不是专家,但我认为这将进入多线程领域。或者一种非常粗略的方法是调用一个函数,该函数在每行之后监听按键。因此,当您按下某个键时,它可能会注意到并停止执行。但这是一个可怕的解决方法。
  • 你可以在这里使用 SIGINT。当您按下 CTRL + C 时,键盘会创建 SIGINT。请参阅 (stackoverflow.com/questions/482702/using-sigint) 中的第二个答案,并尝试定义您的自定义 SIGINT 处理程序,有点像void siginthandler(int param)
  • @badola 我尝试使用您链接的第二个答案中的代码,每次我按 ctrl+c 时它都会抛出异常并加载 kernel32 符号说源不可用。我在 Visual Studio 2015 上。我使用了完全相同的代码。
  • 好吧,我在我的 MacOS 上尝试了一些东西,它已经使用 g++ 编译器进行了编译。评论太长了,让我把它作为答案。

标签: c++ visual-studio function visual-c++ return


【解决方案1】:

最简单的选择是在其中一个中处理退出点 switch case 语句如下。

*** 另外不要忘记在每个 case 语句之后添加 break 语句

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

    case 0:
        return 0 ;
    break ;

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

谢谢

【讨论】:

  • 情况 0:将结束程序,但我只想从 add() 中退出,而不是从 switch 中退出。是的,我忘了添加休息时间;陈述。谢谢。
【解决方案2】:

好吧,我尝试按照您的描述编写一个小示例程序。我不确定它是否适用于 Visual Studio。我使用 g++ 在 MacOS 上编译它。下面的代码可能不是编写程序的好方法,但它有点工作,并且可能对您的事业有所帮助。

我修改了您的代码以制作更简单的程序以便更好地理解 -

#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);
}

它确实在 g++ 上编译,所以我不确定它是否适用于您的情况。

下面是我执行程序得到的输出:

Abhinavs-MacBook-Pro:测试 abhinav$ ./a.out 主要的 1) 添加 2) 另一个函数调用 0,退出 1 添加 输入数字:34 [添加] 到目前为止的总和 => 34 主要的 1) 添加 2) 另一个函数调用 0,退出 1 添加 输入数字:56 [添加] 到目前为止的总和 => 90 主要的 1) 添加 2) 另一个函数调用 0,退出 ^C[INTERRUPT] 到目前为止的总和 => 90 主要的 1) 添加 2) 另一个函数调用 0,退出 0 [MAIN] 到目前为止的总和 => 90 Abhinavs-MacBook-Pro:测试 abhinav$

我希望上面的代码有帮助。愿原力与你同在..!! :)

【讨论】:

  • 它让我回到了预期的切换菜单。它引发异常错误,但我可以从这里工作。谢谢!
  • 酷。很高兴它有帮助。 :)
猜你喜欢
  • 2023-03-24
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 1970-01-01
相关资源
最近更新 更多