2017-06-02 24 views
-2

これを実行すると、if文の外で正しい答えが得られ、if文の中で間違った答えが得られます。 EXと3は長さと幅が9です何か他のものは、後でので、O/Pラインを改ざん - 条件の内側にそれは私がC++は基本計算に2つの異なる答えを与えます

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"; 
} 
+4

[再生できません](http://coliru.stacked-crooked.com/a/73101508f1a55aaf)。実際のコードを投稿してください。 'areaRec'の後に改行を書いていないので、後であなたのプログラムの中の何かが「2」を書き出し、2つの数字がお互いに隣に現れる可能性があります。 – cdhowie

+0

'if'ステートメント内のコードが正しく動作しているように見えます。印刷9と2は後でどこかに印刷されます。 'cout << areaRec;'を 'cout << areaRec <<"に変更してみてください。 "Garbage:" ';あなたは私が何を意味するかを見ます。 – KCH

+0

ああ...ありがとうございました。コードの一番下にチェックを入れました。これがこの原因です...ありがとうございました!私の最初の質問と私は緊張していた... –

答えて

0

は罰金は問題ワークスない...すべてのヘルプは素晴らしいことだダブルフロートint型... を試してみました ...私に92を与えますドン;「\ n」

#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 
+0

正しく動作するとは答えられません。デモを見たい場合は、ideone.comやrextester.comのようなサイトへのリンクをコメントに投稿してください。 – Barmar

+0

@Barmar:入手しました! – Zakir

関連する問題