Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++:我的if/else不工作?没有错误?_C++_If Statement - Fatal编程技术网

C++:我的if/else不工作?没有错误?

C++:我的if/else不工作?没有错误?,c++,if-statement,C++,If Statement,我的程序应该拒绝任何超过1000的输入。我的代码和MS Visual Studio中没有任何错误,它允许我编译它,但当我在2000年输入时,它说输入成功,而不是请重试 我知道我还没有加入循环,所以请再试一次将不起作用。然而,到现在为止,我只想输出说,请再试一次,然后转到镍币 // This program prompts the number of coins, and outputs how many cents you have #include <iostream> #in

我的程序应该拒绝任何超过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;
}

您正在输入不同的变量,例如,一角硬币、五分镍币等,但始终检查四分之一硬币的值。只要修复每个if语句以检查刚刚使用cin输入的变量,您就可以了。

缺少大括号{}。使用调试器,您的问题将比发布到StackOverflow要快得多。@RavelaSmyth您的教科书已经解释了作用域块,不是吗?