【问题标题】:Changing the "Press any key to Continue..." in C++ using Visual Basics使用 Visual Basics 在 C++ 中更改“按任意键继续...”
【发布时间】:2017-10-23 22:54:27
【问题描述】:

到目前为止,我们在课堂上编写的每个程序都以默认的“按任意键继续...”结尾,我该如何更改?我试过使用

cout”无)

但它仍然显示“按任意键继续”,我输入一个键。有什么想法吗?

这是我到目前为止所拥有的(请随时指出我如何在其他领域也有所改进!) `

#include <iostream>
using namespace std;
#include <string>
using namespace std;
int main()
{
    //Declaring my varibles
    double persons = 0;
    double tier1 = 125;
    double tier2 = 100;
    double tier3 = 75;
    double cost = 0;
    string hyphens = "";
    system("cls");
    cout << "-------------------------------------------------- " << endl;
    cout << "Computer Programming Seminar" << endl;
    cout << "-------------------------------------------------- " << endl<<endl;
    cout << "Please enter the number of registrants: ";
    cin >> persons;
    cout << hyphens << endl;
    if (0 < persons && persons < 6)
        cost = persons * tier1;

    else if (5 < persons && persons < 21)
        cost = persons *tier2;

    else if (persons >= 21)
        cost = persons*tier3;
    else
    {
        cost = 0;
        cout << "Invalid Entry" <<endl << endl;
    }

    cout << "Total Amount Owed for the Seminar: $" << cost << endl;
    cout << "-------------------------------------------------- " << endl;
    cout << endl << endl << "Press any key to end the Seminar Program";
    system("pause>nul");
    return 0;

【问题讨论】:

    标签: c++ keyboard


    【解决方案1】:

    我的模式是:

    std::cout << "Paused. Press Enter to continue.";
    std::cin.ignore(100000, '\n');
    

    您可以将提示更改为您想要的任何内容。

    【讨论】:

    • 我只是使用std::cin.get(),通常工作正常。
    【解决方案2】:

    按任意键继续...它是默认值,它永远不会改变。你可以试试#include &lt;conio.h&gt; 和而不是system("pause"); 使用:

    _getch();
    

    【讨论】:

    • 那是 C 的方式。 C++方式为std::cin.get()或相关std::cinI/O
    【解决方案3】:

    您想要避免的消息根本不是来自您的程序。它来自pause 命令。因此,如果您想避免该消息,请不要再拨打system("pause")。在自己的代码中进行自己的 I/O。

    【讨论】:

      【解决方案4】:

      我只是在使用 Console.WriteLine 显示自定义结束消息后将控制台前景和背景颜色更改为黑色

      将光标悬在半空中看起来有点草率,但也超级简单,不会真正引起问题。

      【讨论】:

        猜你喜欢
        • 2011-12-13
        • 2013-11-10
        • 2015-01-23
        • 2020-08-10
        • 1970-01-01
        • 1970-01-01
        • 2010-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多