C++ C+中的输入验证+;

C++ C+中的输入验证+;,c++,validation,if-statement,C++,Validation,If Statement,我正在编写一个需要输入验证的程序,如果单位小于或等于0,程序将不会运行,但我会不断获取包含总数的字符串,但如果值为0,我不会尝试运行该程序 //Write a program that asks for the numbers of units sold and computes the total cost of purchase //Make sure to use input validation that the number of units is greater tha

我正在编写一个需要输入验证的程序,如果单位小于或等于0,程序将不会运行,但我会不断获取包含总数的字符串,但如果值为0,我不会尝试运行该程序

//Write a program that asks for the numbers of units sold and computes the total cost of purchase
//Make sure to use input validation that the number of units is greater        than 0
#include <iostream>
using namespace std;

int main ()
{
    double discount, discountTotal, units, total;
    double package = 99;

    cout << "What is the number of units sold? ";
    cin >> units;
    if(units <=0){
        cout << "Units must be greater than 0" << endl;
    }
    if(units > 0 && units < 10)
        discount = .00;
    else if(units >=10 && units <= 19)
        discount = .20;
    else if(units >=20 && units <= 49)
        discount = .30;
    else if(units >=50 && units <= 99)
        discount = .40;
    else if(units >=100, .50)
        discount = .50;

    discountTotal = package * discount;
    total = package - discountTotal;
    cout << "Your total is: " << total << endl;

    return 0;
}
//编写一个程序,询问售出的单位数量并计算购买的总成本
//确保使用输入验证,使单位数大于0
#包括
使用名称空间std;
int main()
{
双重折扣,折扣总额,单位,总额;
双包装=99;
cout>单位;

如果(单位如果输入不正确,您可以
立即返回

if(units <=0){
    cout << "Units must be greater than 0" << endl;        
    return -1; // if the input 0 or negative, the program will end here
}

if(unitsMmm…我认为这样更好:

#include <iostream>
using namespace std;

int main ()
{

    double discount, discountTotal, units, total;
    double package = 99;

    cout << "What is the number of units sold? ";
    cin >> units;

    if(units <=0)
    {
        cout << "Units must be greater than 0" << endl;
    }
    else
    {
        if(units > 0 && units < 10)
            discount = .00;
        else if(units >=10 && units <= 19)
            discount = .20;
        else if(units >=20 && units <= 49)
            discount = .30;
        else if(units >=50 && units <= 99)
            discount = .40;
        else if(units >=100, .50)
            discount = .50;

        discountTotal = package * discount;
        total = package - discountTotal;
        cout << "Your total is: " << total << endl;
    }
    return 0;
}
#包括
使用名称空间std;
int main()
{
双重折扣,折扣总额,单位,总额;
双包装=99;
cout>单位;
如果(单位)“…但如果值为0,我不会尝试运行它。`“不,你不会,没有条件阻止运行计算并输出总数。
#include <iostream>
using namespace std;

int main ()
{

    double discount, discountTotal, units, total;
    double package = 99;

    cout << "What is the number of units sold? ";
    cin >> units;

    if(units <=0)
    {
        cout << "Units must be greater than 0" << endl;
    }
    else
    {
        if(units > 0 && units < 10)
            discount = .00;
        else if(units >=10 && units <= 19)
            discount = .20;
        else if(units >=20 && units <= 49)
            discount = .30;
        else if(units >=50 && units <= 99)
            discount = .40;
        else if(units >=100, .50)
            discount = .50;

        discountTotal = package * discount;
        total = package - discountTotal;
        cout << "Your total is: " << total << endl;
    }
    return 0;
}