C ++给了我2个不同的基本计算答案

时间:2017-06-02 21:25:01

标签: c++

当我运行这个时,我在if语句之外得到了正确的答案,而在if语句中得到了错误的答案。 EX 3和3的长度和宽度给我9但内部条件它给我92 .. 我试过double float int ... 任何帮助都会很棒......

cout << "you entered 2 for Rectangle\n";
cout << "Enter the length of the Rectangle.\n";
cin >> lengthRec;

cout << "Enter the width of the Rectangle.\n";
cin >> widthRec;

areaRec = lengthRec * widthRec;
cout << areaRec;

if ((lengthRec > 0) && (widthRec > 0)) {
    areaRec = lengthRec * widthRec;
    cout << "\nlength is " << lengthRec << "\n"<< "width is " << 
    widthRec << "\n";
    cout << "The area is ";
    cout << areaRec;
}
else {
    cout << "Invalid entry, Please re run with a positive number\n";
}

1 个答案:

答案 0 :(得分:0)

工作正常没有问题 - 其他东西后来篡改了o / p线,因为你没有;没有&#39; \ n&#39;

#include <iostream>
#include <stdio.h>
#include <iostream>

using namespace std;

int main(){
    int lengthRec, widthRec, areaRec;
    cout << "you entered 2 for Rectangle\n";
    cout << "Enter the length of the Rectangle.\n";
    cin >> lengthRec;

    cout << "Enter the width of the Rectangle.\n";
    cin >> widthRec; //CHECK HERE

    areaRec = lengthRec * widthRec;
    cout << areaRec<<"\n";

    if ((lengthRec > 0) && (widthRec > 0)) {

        areaRec = lengthRec * widthRec;
        cout << "\nlength is " << lengthRec << "\n"<< "width is " << 
        widthRec << "\n";
        cout << "The area is ";
        cout << areaRec<<"\n"; //CHECK HERE
    }
    else {
        cout << "Invalid entry, Please re run with a positive number\n";
    }
    return 0;
}

输出: -

you entered 2 for Rectangle
Enter the length of the Rectangle.
Enter the width of the Rectangle.
9
length is 3
width is 3
The area is 9