switch循环,该怎么解决

switch循环
这个程序不会一个字一个字地出现,是为什么,帮忙看一下,谢谢


# include<iostream.h>
int main()
{
int k=0;
while(1)
{
switch(k)
{
case 0:cout<<"欢";break;
case 1:cout<<"迎";break;
case 2:cout<<"使";break;
case 3:cout<<"用";break;
case 4:cout<<"本";break;
case 5:cout<<"程";break;
case 6:cout<<"序";break;
default:break;
}
for(int i=0;i<5e8;i++);
++k;
if(k>=7)break;
}
cout<<endl;
}

------解决思路----------------------
空for循环被优化掉了。
------解决思路----------------------
引用:
Quote: 引用:

你这个是什么意思,拖延时间?


for(int i=0;i<5e8;i++);
++k;


因为要是字一个个输出来,就得有一个空循环来拖延时间


可能被优化了,建议用Sleep函数
------解决思路----------------------
#include <windows.h>
#include <iostream.h>
int main()
{
    int k=0;
    while(1)
    {
        switch(k)
        {
        case 0:cout<<"欢";cout.flush();break;
        case 1:cout<<"迎";cout.flush();break;
        case 2:cout<<"使";cout.flush();break;
        case 3:cout<<"用";cout.flush();break;
        case 4:cout<<"本";cout.flush();break;
        case 5:cout<<"程";cout.flush();break;
        case 6:cout<<"序";cout.flush();break;
        default:break;
        }
        Sleep(500);
        ++k;
        if(k>=7)break;
    }
    cout<<endl;
    return 0;
}