繁体   English   中英

当我使用十进制值时,为什么我的计算器会重复相同的输出?

[英]Why does my cacluator repeat the same output when I use a decimal value?

我写了这段代码。 它运行良好,但我很好奇。 当我在其中一个函数中添加一个十进制数时,为什么我的其余函数会重复相同的代码并完成? 我知道当我将数字输入指定为 double 时它会起作用。 我很好奇为什么它会像这样运行。

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

int num1, num2;

int request(){
    cout << "Type in a number: " << flush;
    cin >> num1;
    cout << "Type in a number: " << flush;
    cin >> num2;
}

int getMin(){
    cout << "Get the minimum of 2 numbers" << endl;
    request();
    if(num1 < num2)
        cout << "The minimumm of " << num1 << " and "
                << num2 << " is " << num1 << endl;
    else
        cout << "The minimumm of " << num1 << " and "
                << num2 << " is " << num2 << endl;
}

int getMax(){
    cout << "Get the maximum of 2 numbers" << endl;
    request();
    if(num1 > num2)
        cout << "The maximum of " << num1 << " and "
                << num2 << " is " << num1 << endl;
    else
        cout << "The maximum of " << num1 << " and "
                << num2 << " is " << num2 << endl;
}

int power(){
    cout << "Get the exponent of 2 numbers" << endl;
    request();
    cout << num1 << " to the power of " << num2 << " is "
         << pow(num1,num2) << endl;
}



int main(){


    getMin();
    cout << endl;
    getMax();
    cout << endl;
    power();
    cout << endl;

}

output
Get the minimum of 2 numbers
Type in a number: 5.5
Type in a number: The minimumm of 5 and 0 is 0

Get the maximum of 2 numbers
Type in a number: Type in a number: The maximum of 5 and 0 is 5

Get the exponent of 2 numbers
Type in a number: Type in a number: 5 to the power of 0 is 1

为什么我的其余函数重复相同的代码并完成?

这是因为当int的提取失败时,使提取失败的字符留在流中(并且每次尝试新提取时都会遇到)并且流被设置为失败状态,以便进一步尝试提取数据从流失败 - 所以你之前为num1num2设置的任何值都将被一遍又一遍地重用。 如果未分配任何值,则您读取未初始化的内存并且您的程序具有未定义的行为。

要从失败的提取中恢复,您可以clear()流的状态标志和ignore()流中的字符,直到行尾。

此外,您的request()函数和其余函数无论如何都有未定义的行为。 它们被声明为返回一个int但不返回任何内容,因此将它们改为void

但是,您可以更改request()以返回bool 如果请求成功则返回true ,否则返回false

#include <limits> // std::numeric_limits

bool request() {
    cout << "Type in a number: ";
    if(cin >> num1) {
        cout << "Type in a number: ";
        if(cin >> num2) return true;     // both numbers extracted successfully
    }

    // something failed
    if(not cin.eof()) { // skip clearing the state if eof() is the reason for the failure
        cin.clear();    // clear the fail state

        // ignore rest of line to remove the erroneous input from the stream
        cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
    return false;
}

您现在可以使用它并确保用户实际上成功输入了这两个值。 例子:

void getMin() {   // made it void since it doesn't return anything
    cout << "Get the minimum of 2 numbers" << endl;
    if(request()) {               // check that extraction succeeded
        if(num1 < num2)
            cout << "The minimumm of " << num1 << " and "
                 << num2 << " is " << num1 << endl;
        else
            cout << "The minimumm of " << num1 << " and "
                 << num2 << " is " << num2 << endl;
    }
}

暂无
暂无

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

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