繁体   English   中英

如何保持程序运行直到用户类型停止?

[英]How to keep program running until user types stop?

我构建了我的第一个 C++ 软件,它要求用户输入价格和折扣,然后程序显示折扣后的含税总额。

我想让程序一直运行,直到用户键入stop或我编写的任何特定内容。 但我不希望程序询问用户是否要再次运行该程序。 它应该一直运行,直到用户键入特定命令。

#include <iostream>
#include <cmath>
using namespace std;

int main() {
    // declaring integers
    int bill; 
    int discount;
    cout << "\n\t**************************************************************\n";
    cout << "\t\t    D i s c o u n t  &  T a x   C a l c u l a t o r\n";
    cout << "\t**************************************************************\n";

    cout << "Enter the Amount $";
    //asking user to put in the amount
    cin >> bill;
    cout << "Enter the Discount(%) ";
    //asking user to put in discount percent
    cin >> discount;
    int NewPrice = round(bill-(bill*discount/100));
    cout << "Your dicounted price is $" << NewPrice <<endl;
    cout << "Your Total Bill is " << round(NewPrice * 8.875) / (100) + (NewPrice);
    
    //shows the total value 
    cout << round(bill + discount);
    
    cout << "\n\t**************************************************************\n";
    cout << "\t\t               T H A N K  Y O U\n";
    cout << "\t**************************************************************\n"; 
    return 0;
    system("pause");
}

您可以通过以下两种方式之一获得无限循环:

while (true) {
    ...
}

或者:

for (;;) {
    ...
}

在这两种情况下,测试总是通过,所以这不会停止循环。

然后,您可以在循环中使用条件语句,然后breakreturn中断控制流并退出循环。

while (true) {
    if (cond) {
        break;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM