class DatingSim
{
public:
string userName;
int userAge;
int day1Place;
string places[4] = { "Coffee Shop", " Duck Pond", "Club" , "Class"};
string dayOneA = "W E L C O M E T O D A T I N G G A M E";
void slowPrint(string str, int time)
{
for (size_t i = 0; i != str.size(); ++i)
{
cout << str[i];
Sleep(time);
}
}
void dayOne();
void DatingSim::dayOne()
{
slowPrint(dayOneA, 250);
cout << endl;
... other code (just cout stuff shouldn't be a problem)
}
int main()
{
DatingSim NEWGAME;
NEWGAME.dayOne();
return 0;
}
因此,我以前不是使用慢速打印功能参数的字符串,而是使用字符串数组,但是它无法正常工作,因此我只切换到了字符串,因此无法正常工作。我测试了它,并且当它不在一个类中时,它就可以工作。我不应该上课吗?我正在创建一个小游戏,我希望能够使用一个类。当我尝试运行时,没有错误消息只会显示失败。
根据建议,您需要在每个字母后刷新输出流,否则,您会看到整个字符串都打印在末尾。
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
using namespace std;
class DatingSim {
public:
string userName;
int userAge;
int day1Place;
string places[4] = { "Coffee Shop", " Duck Pond", "Club" , "Class"};
string dayOneA = "W E L C O M E T O D A T I N G G A M E";
void slowPrint(string str, int time)
{
for (size_t i = 0; i != str.size(); ++i)
{
cout << str[i] << flush;
this_thread::sleep_for(chrono::milliseconds(100));
}
}
void dayOne()
{
slowPrint(dayOneA, 250);
cout << endl;
}
};
int main()
{
DatingSim NEWGAME;
NEWGAME.dayOne();
return 0;
}