C ++:我的if / else不起作用?没错误?

时间:2015-09-13 19:50:28

标签: c++ if-statement

我的程序应该拒绝超过1000的任何输入。我的代码和MS Visual Studio中没有任何错误,它让我编译它,但是当我放入2000时,它说“输入成功”而不是“请再试一次”。

我知道我还没有把循环放进去,所以“请再试一次”是行不通的。但是,截至目前,我只想让输出说“请再试一次”然后继续使用镍币。

//  This program prompts the number of coins, and outputs how many cents you have

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


int main() {

    // declaring variables:
    unsigned QUARTERS;
    unsigned DIMES;
    unsigned NICKELS;
    unsigned PENNIES;
    double total;

    cout << "********************************************************" << endl;
    cout << "             Welcome to Crazy Coin Counter!             " << endl;
    cout << "********************************************************" << endl << endl;

    // user input:
    cout << "# QUARTERS: ";
    cin >> QUARTERS;
        if (QUARTERS < 1000)
        cout << "               --> Input Successful!" << endl;
        else if (QUARTERS >= 1000)
        cout << "You cannot put in more than 1000 quarters! Please try again." << endl;
    cout << endl << "# DIMES: ";
    cin >> DIMES;
        if (DIMES < 1000)
        cout << "               --> Input Successful!" << endl;
        else if (DIMES>= 1000)
        cout << "You cannot put in more than 1000 dimes! Please try again." << endl;
    cout << endl << "# NICKLES: ";
    cin >> NICKELS;
        if (NICKELS< 1000)
        cout << "               --> Input Successful!" << endl;
        else if (NICKELS>= 1000)
        cout << "You cannot put in more than 1000 dimes! Please try again." << endl;
    cout << endl << "# PENNIES: ";
    cin >> PENNIES;
        if (PENNIES < 1000)
        cout << "               --> Input Successful!" << endl;
        else if (PENNIES >= 1000)
        cout << "You cannot put in more than 1000 dimes! Please try again." << endl;

    // calculations:

    total = (QUARTERS * 0.25) + (DIMES * 0.1) + (NICKELS * 0.05) + (PENNIES * 0.01);

    // output:

    cout << endl <<endl<< "Congrats! You have       $" << total << "      worth of coins! " << endl << endl << endl;

    return 0;
}

0 个答案:

没有答案