I have a simple problem but it seems complicated. I need to keep text from duplicating in each iteration so during, say, a while loop a text, say, "Processing..." would appear once. Example:
int counter = 5;
while (counter > 0)
{
cout << "Processing..." << endl; //or
cout << "Processing...";
counter--;
}
Either prints
Processing...
Processing...
or Processing...Processing...
But I just need to print it once:
Processing...
while the counter gets decremented.
Seems like a rudimentary issue but I just can't seem to find a way to make it work as I need it.
Could somebody help me please?
Thank you,
Victor.
This wasn't very organized information. It seems like you're saying that your while loop processes cout << "Processing..." << endl; twice every iteration. And it seems like your wanting the counter to decrement 5 times to be == 0. Therefor, it should only process the given code 5 times. I can't really help you before I can truly understand what your wanting and what it's doing. Are you wanting it to print 5 times or just 1 time?
Then I fail to see why you need a loop. If you want to print a single time, why not get rid of the loop and use a single statement or cout << "Processing..." << endl;? Or am I misreading here...
Because I have a loop where things are happening and while these things are happening I need to say they are "Processing..." and then stop showing "Processing..." when they stop happening.
Yes, I tried that, but the statement remains showing after the loop terminates. It must disappear when loop terminates because the processing of data finishes and there is no point of showing the statement anymore.
So you want to show the word "processing...", process your logic, and then have the word disappear? I would see if the ncurses library could help you with that. I don't know of any immediate way or anything that would "easy". I might be wrong.
int counter = 5;
std::cout << "Processing..."; //NB: This is 13 characters long
while (counter>0)
{
//do stuff
counter--;
}
std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b "; //There are 13 x \b and 13 x spaces
This should work, as \b essentially puts the console cursor back one space. Do that 13 times to get it to the start of "Processing..." then write over it with thirteen spaces. Bit of a botch job but it should work :)
Also, to actually see this work, I'd change your counter to something a bit higher, obviously :).