空の if ステートメントを挿入しても、プログラムが整数変数の値 (ゼロ) を生成するのはなぜでしょうか?
3 つの整数を比較して、それらのすべてまたはいずれかが 10 より大きいかどうかを出力する単純なプログラムです。42 行目には、セミコロン付きの空の else ステートメントがあります。ユーザーが z を 6 と入力した場合、プログラムは z1 =0 を返すべきではありません。
#include <iostream> // include iostream class for input and output
#include <string> // for strings operation
#include <sstream> // for stringstream operations
using namespace std; // use defintion from std namespace
int main ()
{
int x, y, z; // define three user-input integers
int x1, y1, z1; // variables to hold bool values
stringstream xyz; // variable to store whole bool value
int XYZ; // variable to condiition print
// prompt user for x, y and z input
cout << "Please enter x" << "\n";
cin >> x;
cout << "Please enter y" << "\n";
cin >> y;
cout << "Please enter z" << "\n";
cin >> z;
// generate bool values of x1, y1, z1
if ( 10/x < 1)
x1 = 1;
else
x1 = 0;
if ( 10/y < 1)
y1 = 1;
else
y1 = 0;
if ( 10/z < 1)
z1 = 1;
else
;
// read into xyz and then XYZ
xyz << x1 << y1 << z1;
xyz >> XYZ;
// generate 8 print statements
if (XYZ == 111)
;
if (XYZ == 000)
cout << "x, y, z < 10" << endl;
if (XYZ == 110)
cout << "x, y > 10 and z < 10" << endl;
if (XYZ == 101)
cout << "x, z > 10 and y < 10" << endl;
if (XYZ == 100)
cout << "y, z < 10 and x > 10" << endl;
if (XYZ == 011)
cout << "y, z > 10 and x < 10" << endl;
if (XYZ == 010)
cout << "x, z < 10 and y > 10" << endl;
if (XYZ == 001)
cout << "x, y < 10 and z > 10" << endl;
} // program main ends
私はこれを調査するのに何時間も費やしましたが、ほとんどの議論は、if ステートメントの直後のセミコロンの構文エラーまたはその他のトピックに関するものです。
(注: 51 行目は、コンソールに何も表示しないように修正されて実行されます)。
Mac OSX 10.8.4でこれに遭遇した人はいますか? デフォルトのLLVMコンパイラと関係がありますか?
ありがとう。