如何使它成为一个无限循环?

How to make this an infinite loop?

  1. 我试图让这段代码 "move" 从左到右。我已经成功地将它从右移了。现在的问题是,我如何无限地 运行 呢?好吧,我不熟悉 C++,因为我是初学者。我试图在互联网上搜索如何无限地制作代码 运行,然后我找到了一个 for(;;) 循环,并将其放在第一个 for 循环开始的位置上方。但它不起作用。你能给我提示或任何提示吗?

     #include <iostream>
     #include <cstdlib>
     #include <string>
     #include <ctime>
     #include <iomanip>
     #include <windows.h>
    
     using namespace std;
     int main ()
     {
         string a;
         cout <<"Enter String : ";
         cin >> a;
         cout << '\n' << '\n' << '\n' << '\n';
         for(int x = 0; x <= 20; x++ ) {  
             Sleep(200);
             system("cls");
             cout <<"Enter String : ";
             cout << a;
             cout << '\n' << '\n' << '\n' << '\n';
             cout << setw(x)<< a;
         }
         for(int y = 20; y <= 20; y-- ) {
             Sleep(200);
             system("cls");
             cout <<"Enter String : ";
             cout << a;
             cout << '\n' << '\n' << '\n' << '\n';
             cout << setw(y)<<a;
         }
         return 0;
      } 
    

输出应显示如下:

Enter string: Hello Friend


"Hello Friend" > it will move to the right and after 20 spaces.
                       <  now it move back to the left < "Hello Friend"

我还看到了一个 "Void" 代码,它有什么作用?它与我的代码相关吗?

通常你可以这样写一个无限循环:

while (1){
  //do coding
}

While 循环 运行 while () 内的语句为真。 1 始终为真。 您可以随时使用 "break;" 来停止无限循环。

罪魁祸首是这一行 for(int y = 20; y <= 20; y-- ) 将其编辑为 for(int y = 20; y >= 0; y-- ) 然后将两个 for 循环放入 while(1){ }

完整代码如下:

int main ()
 {
     string a;
     cout <<"Enter String : ";
     cin >> a;
     cout << '\n' << '\n' << '\n' << '\n';

     while(1){ //infinite loop starts

     for(int x = 0; x <= 20; x++ ){  
     Sleep(200);
     system("cls");
     cout <<"Enter String : ";
     cout << a;
     cout << '\n' << '\n' << '\n' << '\n';
     cout << setw(x)<< a;
     }
     for(int y = 20; y >= 0; y-- ){ //make sure this condition
     Sleep(200);
     system("cls");
     cout <<"Enter String : ";
     cout << a;
     cout << '\n' << '\n' << '\n' << '\n';
     cout << setw(y)<<a;
     }

 }
     return 0;
}

"And also I saw a "Void" code what does it do? and is related to my code?": 基本上它意味着 "nothing" 或 "no type".
有 3 种使用 void 的基本方法:

函数参数:int func(void) -- 函数将 nothing.same 作为 int func()

函数return值:void func(int) -- 函数return什么都没有

通用数据指针:void* data -- 'data' 是指向未知类型数据的指针,无法取消引用。
不,它与您的代码无关。