【问题标题】:Progressbar C++进度条 C++
【发布时间】:2016-03-29 09:35:58
【问题描述】:

我正在尝试为在 unix 服务器上运行的程序制作进度条。它位于大 for 循环的末尾,如下所示:

struct CodeBook{
    string Residue;
    vector<string> CodeWords;
};

    int r = 0, d = 2;
    string code;

    vector<CodeBook> CodeBooks(Weight(n+1, 4, d));

    for (r = 0; r < Weight(n+1, 4, d); r++){
            do{
                    getline(Input, code);
                    if (code[0] == 'R') CodeBooks[r].Residue = code;
                    else if (isdigit(code[0]))CodeBooks[r].CodeWords.push_back(code);
            } while (code != ""); //if the line is empty, end the loop and move to the next

            if (r%256==0) {fflush(stdout); cout << (r / Weight(n+1, 4, d)) << "\r";} //the problem
    }

它所做的只是在行首打印一个永不改变的 0,并将光标放在它上面。 我做错了什么?

【问题讨论】:

  • 可能是骗人的,例如stackoverflow.com/questions/1337529/… 另外:ncurses
  • 好吧,你没有告诉我们它实际上在做什么,那么我们应该如何猜测实际出了什么问题?
  • @BaummitAugen 抱歉,我确实看过,但我发现的所有问题都是针对 C# 的:/ 我会尝试第一个答案。虽然我很漂亮,但那是我的第一个实现(带有回车的 printf)
  • @Jeff 一如既往,如果您对现有代码有任何疑问,请发帖minimal reproducible example
  • 这只会打印 0 或 256 的倍数。根据您的解释,我假设您永远不会达到这样的值,只会得到零。我真的不明白这首先与进度条有什么关系。

标签: c++ return progress-bar cout


【解决方案1】:

表达式(r / Weight(n+1, 4, d)) 将始终为0,因为r 是一个整数,Weight(n+1, 4, d) 也是整数(我认为)并且r 总是小于Weight(n+1, 4, d)(-> for 循环)。 如果您想要百分比输出(0..100),则在除以前乘以 100:

if (r%256==0) {fflush(stdout); cout << (r * 100 / Weight(n+1, 4, d)) << "\r";}

如果您想从 0..1 打印值,也可以转换为 floatdouble

if (r%256==0) {fflush(stdout); cout << (static_cast<double>(r) / static_cast<double>(Weight(n+1, 4, d))) << "\r";}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多