这里的所有代码我while循环似乎被忽略了我的函数getline
#include <iostream>
#include <string>
#include <windows.h>
#include <stdlib.h>
using namespace std;
void Pdelay(string str)
{
for (char c : str)
{
std::cout << "" << c << "";
Sleep(100);
}
std::cout << '\n';
}
int main()
{
int exit = 1;
while (exit == 1)
{
cout<< "Type something\n:";
string str;
str.clear();
getline(cin,str);
Pdelay(str);
cout << "\n\n[1]Type something again"<< endl;
cout << "[2]Exit\n:";
cin >> exit;
}
return 0;
}
每当我运行它,它正常工作第一次全面,当它循环回它跳过函数getline,并用两个COUT继续声明。
这与此有什么关系? – xaxxon
注释:'int exit = 1;'使用不同的名称,因为它可能与['std :: exit']冲突(http://en.cppreference.com/w/cpp/utility/program/exit) ,特别是在使用命名空间标准后(你应该尽量避免)。 – vsoftco
在已经为空的字符串上调用'clear()'没有多大意义。 – molbdnilo