while里面的值不因为循环里的cin而改变吗?

这个程序为什么star>20后还要循环啊

#include
using namespace std;
int main()
{
int space, star = 0;

while (star < 20)
{
cout << "输入第一行*个数:";
cin >> star;
for (space = 0; star > 0; star -= 2, space++)
{
for (int i = 1; i <= space; i++)

cout << ' ';
for (int i = 1; i <= star; i++)
cout << '*';

        cout << "\n";
    }
}
system("pause");
return 0;

}
图片说明

但是多定义n作为while(n<20)

在 读取了star后 令 n=star 运行又正常了

#include
using namespace std;
int main()
{
int space, n = 0,star;

while (n < 20)
{
cout << "输入第一行*个数:";
cin >> star;
n = star;

你的star-=2在for循环进行的时候已经变了,你可以用例外一个变量进行-=2运算

#include<iostream>
using namespace std;
int main()
{
    int space, star = 0,temp=0;
    while (star < 20)
    {
        cout << "输入第一行*个数:";
        cin >> star;
        temp = star;
        for (space = 0; temp > 0; temp -= 2, space++)
        {
            for (int i = 1; i <= space; i++)
                cout << ' ';
            for (int i = 1; i <= star; i++)
                cout << '*';
            cout << "\n";
        }
    }
    system("pause");
    return 0;
}
#include<iostream>
using namespace std;
int main()
{
    int   space,  star = 0; 
    while (star <= 20)
    {
    cin >> star;
    }
    cout << ":star = " << star;
    }

while (star < 20)
{
cout << "输入第一行*个数:";
cin >> star;
if (star >= 20) break;
...

这样写看看

因为for循环里面,把star-=2修改了啊,执行完while循环体,进行star判断时,star值已经变为0了。