【发布时间】:2013-03-22 03:48:28
【问题描述】:
多年来,我一直在努力寻找这个问题,但我似乎无法理解它。 当输入大于 2 的数字时,程序只是连续输出 ' ' 或 '*' 而不会终止,并给出正确的输出。
谁能看到我做错了什么?这是我的代码:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int xx;
cout << "Enter the Height (odd positive numbers only): " << endl;
cin >> xx;
for(float y; y < xx; y++)
{
for(float x; x < xx; x++)
{
x = abs( x - ( xx / 2 ) );
y = abs( y - ( xx / 2 ) );
if( ( x + y ) <= ( xx / 2 ) )
cout << '*';
else
cout << ' ';
}
cout << endl;
}
return 0;
}
【问题讨论】:
-
UB。 UB。 UB。 UB。表示未定义的行为。
x和y未初始化,您正在阅读它们。 -
for(float y; y < xx; y++)这里的初始 y 是什么? -
对循环索引使用
float类型变量很奇怪。
标签: c++ if-statement for-loop