-11

I'm trying to make a progressbar for my program that's running on a unix server. It's at the end of a large for loop and looks like this:

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
    }

All it does is print a never-changing 0 at the beginning of the line with the cursor over it. What am I doing wrong?

Jeff
  • 695
  • 1
  • 8
  • 19
  • Probably dupe, e.g. https://stackoverflow.com/questions/1337529/how-to-update-a-printed-message-in-terminal-without-reprinting-linux Also: ncurses – Baum mit Augen Dec 23 '15 at 01:59
  • 1
    Well you didn't tell us what it is actually doing so how are we supposed to guess what is actually wrong? – takendarkk Dec 23 '15 at 02:00
  • @BaummitAugen sorry, I did look, but all questions I found were for C# :/ I will try the first answer. Though I'm pretty that was my first implementation (printf with carriage return) – Jeff Dec 23 '15 at 02:35
  • @Jeff As always, if you have a problem with your existing code, please post a [mcve]. – Baum mit Augen Dec 23 '15 at 02:36
  • This is only going to ever print 0 or multiples of 256. Based on your explanation, I assume you never reach such a value, and only get zero. What I'm really not understanding is how this would relate to a progress bar in the first place. –  Dec 23 '15 at 08:55
  • @WilliamKappler This is the entire loop. Weight(int,int,int) is an int that's typically in the thousands/millions and soon billions if my partner can get his program working (in which case I'll have to make some vars long long ints). Does this extra bit of code help? – Jeff Dec 23 '15 at 18:00
  • @WilliamKappler Ok, not a bar, but it should tell me how far along the program is in terms of reading the input file. – Jeff Dec 23 '15 at 18:05
  • It's more descriptive, but I don't see anything obviously wrong. Perhaps you should break at the loop start and see what is actually going on. –  Dec 24 '15 at 03:34

1 Answers1

1

The expression (r / Weight(n+1, 4, d)) will always be 0 because r is an integer, Weight(n+1, 4, d) is integer too (i think) and r is always less than Weight(n+1, 4, d) (-> for loop). Multiply with 100 before dividing if you want percentage output (0..100):

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

or cast to float or double if you want values printed from 0..1:

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