为什么我的程序为什么不在Visual Studios 2019之外输出我想要的信息? [英] Why doesn't my program output the information I want in outside of Visual Studios 2019?

查看:50
本文介绍了为什么我的程序为什么不在Visual Studios 2019之外输出我想要的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,向用户提供有关减肥的一些信息,以及在我要求用户输入的一些信息下如何达到特定目标的信息.我现在可以开始实现函数以使主函数更好一些,但是在解决此问题之前,我不希望继续.

I coded a program that gives the user some information about weight loss, and how to reach specific goals given some information I ask the user to input. I'm at the stage where I can start implementing functions to make the main function a bit nicer, but I don't want to proceed until I get this problem solved.

它在Visual Studios中运行得很好,可以完美输出最后一部分,但是如果我尝试运行在build文件夹中找到的.exe用于Debug或Release,则它会在告诉用户之前退出程序他们想要的信息.

It runs just fine in Visual Studios, outputting the last part flawlessly, but if I try to run the .exe found in the build folder for either Debug or Release, it just exits out of the program right before it tells the user the information they want.

不过,我不确定为什么会发生这种情况,因为它不会给我任何构建错误或编译器错误.这是.exe跳过的代码:

I'm unsure as to why this happens, though, because it doesn't give me any build errors or compiler errors. Here's the code the .exe skips over:

cout << "Choose your rate at which you'd like to meet your goal! " <<
        "\n 1. Fast\n 2. Medium\n 3. Slow" << endl;
    int uRate;
    cin >> uRate;

//This bit of code it executes, and the user inputs their rate, which 
//assigns the value to the switch statement for it to determine it's case.

double time = uWeight - uGoalWeight;

    double fastrate = (uWeight * 0.01);
    double mediumrate = (uWeight * 0.0075);
    double slowrate = (uWeight * 0.005);

    int newTime;

    switch (uRate) {
    case 1:
        newTime = time / fastrate;
        cout << "Your time in weeks to reach your goal is : " << newTime << " weeks." << endl;
        cout << "You'd have to lose " << fastrate << " lbs per week to meet this goal!" << endl;
        cout << "Remember, your optimal goal weight would be: "
            << uGoalWeight << " lbs!" << endl;

        break;

    case 2:
        newTime = time / mediumrate;
        cout << "Your time in weeks to reach your goal is : " << newTime << " weeks." << endl;
        cout << "You'd have to lose " << mediumrate << " lbs per week to meet this goal!" << endl;
        cout << "Remember, your optimal goal weight would be: "
            << uGoalWeight << " lbs!" << endl;

        break;

    case 3:
        newTime = time / slowrate;
        cout << "Your time in weeks to reach your goal is : " << newTime << " weeks." << endl;
        cout << "You'd have to lose " << slowrate << " lbs per week to meet this goal!" << endl;
        cout << "Remember, your optimal goal weight would be: "
            << uGoalWeight << " lbs!" << endl;

        break;

基本上,这是释放/调试.exe文件中不会显示的代码.只要已在VS2019中进行调试或运行,其他所有内容都可以正常工作.我需要显示此信息,因为这是应用程序编码的全部原因.

Basically, this is the bit of code that just doesn't display in the release/debug .exe file. Everything else works so long as it's debugged or ran in release from VS2019. I need this info to display, as it's the entire reason the app was coded.

推荐答案

OP已声明

您说对了.我通过在switch语句的末尾添加system("pause")来解决此问题.

You right. I fixed it by adding system("pause") at the end of the switch statement.

和类似的提示大约在链接

and similar hints were given in round about 50 % of answers in the link SO: How to stop C++ console application from exiting immediately? provided by drescherjm.

我相信 system("pause"); 是一个错误的解决方案,仅用于在应用程序结束时等待用户确认.–这可能会使应用程序容易受到攻击.

I believe system("pause"); is a bad solution for just waiting for user confirm at end of application. – It may make the application vulnerable.

使用谷歌搜索,例如 ENV33-C.不要调用状态为(p)的system()

Googling this I found e.g. ENV33-C. Do not call system() which states

使用 system()函数可能会导致可利用的

Use of the system() function can result in exploitable vulnerabilities, in the worst case allowing execution of arbitrary system commands. Situations in which calls to system() have high risk include the following:

  • 传递源自污染源的未经消毒或未经消毒的命令字符串时
  • 如果指定的命令没有路径名,并且攻击者可以使用命令处理器的路径名解析机制
  • 如果指定了可执行文件的相对路径,并且攻击者可以访问当前工作目录的控制权
  • 如果指定的可执行程序可以被攻击者欺骗

请勿通过 system()或等效功能调用命令处理器来执行命令.

Do not invoke a command processor via system() or equivalent functions to execute a command.

通过一些语句和C ++ std库,实际上很容易实现在应用程序结尾处等待用户确认:

Waiting for user confirm at end of an application is actually easy to achieve with a few statements and the C++ std library:

#include <iostream>
#include <string>

void pause()
{
  std::cout << "Press [ENTER] to continue...";
  std::string input; std::getline(std::cin, input);
}

int main()
{
  std::cout << "Hello World.\n";
  pause();
  return 0;
}

输出:

Hello World.
Press [ENTER] to continue...

在大肠杆菌上的实时演示

顺便说一句.该解决方案更加可移植,因为它不依赖于标准库之外的任何内容.

Btw. this solution is even more portable as it doesn't depend on anything then the standard library.

回到过去,在控制台程序中可以看到使用计算机的常用方法

Back the days, where console programs where the usual way to work with computers you could see the

Press any key to continue...

经常.尽管la脚的笑话经常重复出现:

quite often. A frequentially repeated though lame joke was:

程序说我可以按任意键.我多次按 Shift Alt Ctrl ,但没有任何反应."

"The program said I may press any key. I pressed Shift and Alt and Ctrl many times but nothing happened."

这篇关于为什么我的程序为什么不在Visual Studios 2019之外输出我想要的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆