在C ++中一次输出一个字母?

时间:2015-11-23 23:34:19

标签: c++ output

我已经看到了这个问题的几个解决方案,但我仍然遇到了问题。我已经尝试过我在本网站上看到过的几种解决方案,但我仍然遇到同样的问题。我不确定如何完成这项工作,而且我也不确定它为什么不起作用,因为我对C ++很陌生。

我正在尝试做的事情的简要说明:我在我的学校为一个视频游戏设计俱乐部写了一个简单的老式的基于文本的故事/游戏,并且在几个方面我让用户做了决定,或输入诸如名称之类的东西,然后在输出行中使用,因为它会被多次引用。使用像这样简单的东西,我没有遇到任何问题:

#include <iostream>
#include <string>
#include <limits>
#include <windows.h>
using namespace std;


int main(){

   string name, place ;
   cout << "???: What is your name? ";
   getline (cin, name);
   cout << "???: Hello, " << name << "!\n" << "\n";
}

我的问题是,我希望文本一次只显示一个字符,比如对话,但每当我尝试使用其他人写过的内容时,它就不会似乎非常喜欢它。我尝试过的唯一一个我现在可以找到的是:

#include <iostream>
#include <unistd.h> // include <windows.h> on windows

// function to output as if it was being typed
void type_text(const std::string& text)
{
    // loop through each character in the text
    for (std::size_t i = 0; i < text.size(); ++i)
    {
        // output one character
        // flush to make sure the output is not delayed
        std::cout << text[i] << std::flush;

        // sleep 60 milliseconds
        usleep(60000); // use Sleep on windows
    }
}
int main()
{
    type_text("Hej hej hallå!");
}

当我尝试将该代码与我所写的内容一起使用时,我尝试将名称输回给用户显然存在某种冲突。我不确定问题是什么,因为我对C ++这么陌生,有人可以帮助我吗?

0 个答案:

没有答案