在cout字符串期间在控制台上闪烁光标

时间:2015-05-11 17:50:21

标签: c++

当控制台打开并且程序运行时,我只得到一个闪烁的光标。 有什么我做错了吗?程序运行得太快,我看不到它?

感谢您提前提供任何帮助。

#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

string RandomString(int len)
{
    srand(time(0));
    string str = "01213456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    int pos;
    while (str.size() != len) {
        pos = ((rand() % (str.size() - 1)));
    }

    return str;
}

int main()
{
    string random_str = RandomString(10);
    cout << "random_str : " << random_str << endl;
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:5)

看起来你的while循环永远不会退出:

    while (str.size() != len) {
        pos = ((rand() % (str.size() - 1)));
    }

您只能在循环中更新pos,但不能在pos条件下使用while