【问题标题】:How do I replace this with a variable?如何用变量替换它?
【发布时间】:2023-03-22 01:43:01
【问题描述】:

我试图在 C++ 中创建一个函数,它会像视频游戏一样缓慢地将每个字母输入控制台。 现在我遇到的问题是创建一个字符串变量,它将取代“Hello World”的工作。

 void typein(//input goes here)

{

    char str[] = { "Hello World" //input goes here};

        int len = strlen(str);

    for (int i = 0; i<len; i++) {
            putchar(str[i]);
            Sleep(80);

    }
    Sleep(100);
    cout << endl;

}

【问题讨论】:

标签: c++ visual-c++ visual-studio-2013


【解决方案1】:

只需使用std::string

void typein( const std::string& str )
{

   std::size_type len = str.size( );

   // ...

}

【讨论】:

    【解决方案2】:

    你可以使用指向str数组的指针

    void typein(char* str, int length){
        <your code goes here>
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-20
      • 1970-01-01
      • 2014-01-28
      • 2021-05-03
      • 1970-01-01
      • 2018-09-09
      • 2016-06-05
      • 1970-01-01
      • 2022-10-04
      相关资源
      最近更新 更多