C++ 我的substr()未按预期工作
例如,我试图让文本在屏幕上滚动;他,他,你好C++ 我的substr()未按预期工作,c++,string,substr,C++,String,Substr,例如,我试图让文本在屏幕上滚动;他,他,你好 #include <iostream> #include <stdlib.h> using namespace std; int main() { string text = "Welcome to the Password Vault!"; int x; for (x = 0; x < text.length(); x++) { cout << text.sub
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
string text = "Welcome to the Password Vault!";
int x;
for (x = 0; x < text.length(); x++) {
cout << text.substr(x,x);
_sleep(0100);
}
return 0;
}
#包括
#包括
使用名称空间std;
int main()
{
string text=“欢迎使用密码库!”;
int x;
对于(x=0;x cout不仅要更改起始位置,还要更改要获取的子字符串的长度,这是第二个参数。如果一次只获取一个字符,则第二个参数应为1
有关更多信息,请参见例如。这是因为substr()函数需要两个参数,即要打印的字符串的起始位置和大小。因此,在第一次迭代中,当执行substr(0,0)时,substr(1,1)在另一次迭代中打印“e”
第三次迭代substr(2,2)打印“lc”
substr(3,3)-“com”
以此类推……您使用的substr是错误的。您应该将第二个参数(x)替换为1。第二个参数是要“获取”的字符数。text.substr(x,1);
您将被设置为:)通常您需要发送一个字符来将光标移回行首,例如:\r
。正如您所知,C++11有一个标准的std::this\u thread::sleep\u for
函数。0100是4的八进制文字,而不是100的值。不\include
?@chris您的意思是ode>0100
是64的八进制吗?:)@JoachimPileborg,是的,4是二进制的。都是0和1,你期待什么?谢谢!我想(x,x)只能做x
elccomome me toe to t to theto the Po the Pas the Passwthe Passworhe Password e Password Va Password VaulPassword Vault!assword Vault!ssword Vault!sword Vault!word Vault!ord Vault!rd Vault!d Vault! Vault!Vault!ault!ult!lt!t!!
Process returned 0 (0x0) execution time : 1.961 s
Press any key to continue.
Welcome to the password vault!