涡轮 如何使用 C++ 终止具有 3 个点的 "Loading..." 程序?

turbo How to terminate a "Loading..." program with having 3 dots using C++?

本文关键字:Loading 程序 何使用 C++ 终止 涡轮      更新时间:2023-10-16

你可能在很多地方看到过"Loading…"其中3个点(或更多),即"…",首先一个一个地出现,然后在显示所有点后,它们消失,然后再一个一个地出现(这样,总共2次)并退出。

精致的

: -

第1阶段:

加载。

第二阶段:

加载. .

第三阶段:

加载…

然后,它重复第二次,之后它终止!

因此,为此我准备了一个c++程序,其源代码为:

#include <iostream.h>
#include <time.h>
#include<dos.h>
int main()
{
    cout << "Loading";
    cout.flush();
    for (;;) {
        for (int i = 0; i < 3; i++) {
            cout << ".";
            cout.flush();
            sleep(1);
        }
        cout << "bbb   bbb";
    }
    return 0;
}

程序未终止。它不会停止!我如何编辑它使其终止?

请张贴代码支持的Turbo c++编译器,因为我不是太了解ANSI c++ !!: P

注意:这不是一个重复的问题,所以不要把它标记为重复!!

谢谢,提前!:)

重复第二次,之后终止

你不需要在程序的某个地方有一个2来做到这一点吗?

也许在for (;;)的某个地方?与程序中的其他for类似…

几个反问句:

  • 你认为for (;;)会做什么?
  • 为什么你认为把自己限制在Turbo c++编译器是一个好主意?

你写的是for(;;)。为什么会终止呢?这是一个无尽的循环。

如果您希望外部循环只运行2次,为什么不使用for(int j = 0; j < 2; ++j){other loop}

#include <iostream.h>
#include <time.h>
#include<dos.h>
int main()
{
    cout << "Loading";
    cout.flush();
    //The outer "for" didn't stop in your case, use this:
    for (int iterations = 0; iterations < 2; ++iterations) {
        for (int i = 0; i < 3; i++) {
            cout << ".";
            cout.flush();
            sleep(1);
        }
        cout << "bbb   bbb";
    }
    return 0;
}

您至少有两个选择:

  1. for(;;)替换为

    for(int j=0; j < 2; ++j) { /*inner loop here*/ }

  2. 保持for(;;)不变,但执行以下操作:

    int loopcount = 0; for(;;) /* your loop */ { /* add this after inner loop */ ++loopcount; if(loopcount > 1) break; }