使用 Visual Basic 更改C++中的"Press any key to Continue..."



对于我们到目前为止在课堂上编写的每个程序,它都以默认的"按任意键继续..."结尾。如何更改此设置?我试过使用

cout<<"按任意键结束程序";系统("暂停>"nul)

但它仍然显示"按任意键继续"我输入的键。有什么想法吗?

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

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

我的模式是:

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

您可以将提示更改为所需的任何内容。

按任意键继续... 这是默认值,永远不会更改。您可以尝试#include <conio.h>而不是system("pause");使用:

_getch();

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

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

让光标悬在半空中看起来有点草率,但它也超级容易,不会真正引起问题。

最新更新