5

Making a program that has a % progress bar of sorts, its really spammy though. It's creating 100,000 numbers, and showing how far along in the progress it actually is, with a 1% 2% 3% kind of feel, only problem is, it's making the console flood with those numbers, about a thousand of them per percentage increase.

It's probably irrelevant but heres the code I'm using

int testing() {
    cout << "Open file\n";
    fstream outFile("text.txt", ios::out);
    int number = 100000;
    for (int i = 1; i != number + 1; i++) {
        outFile << i << endl;
        cout << (i / 1000) << endl;
        //cout << clearLastLine();
    }
    outFile.close();
    cout << "File Closed\n";
    return 0;
}

The commented cout is what I want to use (it doesn't exist to my knowledge). Every time the cout << i/1000 << endl; triggers, I want it to remove that number it just put into the console window, so it can be replaced with the next number. Is this possible? If it is, I'd love to know - thanks. -- This picture below, shows why I want it to clear the last line that was outputted. lots of 99% then 100% to close

New code that makes it less spammy, but I still end up with 100 lines of console I do not want, however I still want the newest number to display, just the others not to.

int testing() {
    cout << "Open file\n";
    fstream outFile("text.txt", ios::out);
    int number = 100000;
    int percentifier = number * .01; // added this so I can change number to be anything I want
    int temp2 = 0;
    for (int i = 1; i != number + 1; i++) {
        outFile << i << endl;
        int temp = i / percentifier;
        if (temp != temp2) {
            //cout << clearLastLine();
            cout << (temp) << "%" << endl;
        }
        temp2 = i / percentifier;
    }
    outFile.close();
    cout << "File Closed\n";
    return 0;
}
John Olivas
  • 309
  • 1
  • 2
  • 14
  • 2
    Why not change the condition of the loop so the last iteration never actually happens? If it doesn't happen then it won't be printed. Or just add a condition before writing to `std::cout` so that output never happens? – Some programmer dude Feb 07 '16 at 19:40
  • yea I'm working on that right now, trying to see if that % has changed or not, if not, dont print - but still that's an extra 100 lines of console text I don't want if I do get it working like that. I still want to see how far it is from completion though. – John Olivas Feb 07 '16 at 19:43
  • 1
    Ah I think I know what you mean. It's very simple, just keep a variable containing the last percentage. If the new percentage is equal to the last, don't print, else print and set the variable to the new percentage. – Some programmer dude Feb 07 '16 at 19:44
  • Yep, got it :P http://i.imgur.com/D2cCyeO.jpg - but now, I just need to get rid of those last numbers as soon as the number increases... :L – John Olivas Feb 07 '16 at 19:49
  • 4
    I GOT IT!!! :D `cout << (temp) << "%" << '\r';` the \r makes the line overwrite what is currently there!! – John Olivas Feb 07 '16 at 20:00
  • Possible duplicate of [C++ How to use the same line to print text after delete previous?](https://stackoverflow.com/questions/24515308/c-how-to-use-the-same-line-to-print-text-after-delete-previous) – Fabio A. Correa Apr 22 '19 at 19:23

1 Answers1

10

Well, since I've refined my question more, a question with the answer I was looking for showed up in a list pf possible answers. And low and behold, I found what I wanted.

Using the '\r' modifier, instead of endl; on my cout << (temp) << "%" << endl; deletes the current line as soon as it writes to it, and overwrites it with the next number.

John Olivas
  • 309
  • 1
  • 2
  • 14