Hello, everyone...I am stuck with yet another problem...please help me out
here I want to display something as follows...
Aborting in 7 seconds...
Aborting in 6 seconds...
Aborting in 5 seconds...
Aborting in 4 seconds...
Aborting in 3 seconds...
Aborting in 2 seconds...
Aborting in 1 seconds...
Aborting in 0 seconds...
after each line is displayed...it would be removed and the new line will be displayed...
I used some logic...but I can do it till the numbers but cannot display the seconds correctly...
1 2 3 4 5 6 7 8 9 10 11 12 13
cout<<"Aborting in "for(i=7000;i>0;i--)
{
if(i==7000||i==6000||i==5000||i==4000||i==3000||i==2000||i==1000||i==0)
{
t=i/1000;
cout<<t;
Sleep(1000);
cout<<"\b";
cout<<" seconds";//somewhere this line goes wrong with the use of \b
}
}
}
From the above code, you can understand that I am just changing the numbers...the rest of the part is absolutely same in each iteration...
Please help me with the program and please use some simple and easy logic and pretty statements for this newbie...
see if your compiler tools support some version of gotxy(x,y) which lets you over-write from that location, allowing you to just overwrite the number only each iteration.
#include <iostream>
#include <unistd.h>
using std::cout;
using std::flush;
int main()
{
cout << "Aborting in 7 seconds" << flush;
for (int i=6; i>= 0; --i) {
sleep(1);
cout << '\r' // return to beginning of line
<< "Aborting in " << i << " seconds" << std::flush;
}
}
It is not possible to remove character(s) which is(are) already printed from console without accessing console API, so I think there is not standard way for doing this platform independent. dhayden has given nice solution using std::flush.
Also, you can do this on windows platform using windows header, cstdlib library and system() function like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <cstdlib>
#include <windows.h>
using std::cout;
using std::endl;
int main()
{
for (int i=7; i>= 0; --i)
{
system("cls");
cout<< "Aborting in " << i << " seconds" << endl;
Sleep(1000);
}
return 0;
}
The secret to my solution isn't std::flush, it's '\r'. That's a carriage return character and the meaning is to return the cursor to the beginning of the line without advancing it. This is standard ASCII and most terminals support it.
@kinjal2209
Solution which dhayden gave is very simple and it is very good way for solving your problem. As he said he uses '\r', carriage return, which will set the cursor to the beginning of the line.
About std::flush, you are not needed to use it if you are on Windows or Linux (I haven't got any problem without using std::flush). It is used to show the output immediately. std::cout by default is buffered, so it waits until buffer is filled before printing, or it needs flush to occur (I learned this thanks to dhayden, I am beginner so I didn't know that).
So you can write your problem like this (the same like dhayden without std::flush):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <cstdlib>
#include <windows.h>
using std::cout;
int main()
{
for (int i=7; i>= 0; --i)
{
system("cls");
cout<< "\rAborting in " << i << " seconds";
Sleep(1000);
}
return 0;
}
About std::flush, you are not needed to use it if you are on Windows or Linux (I haven't got any problem without using std::flush)
Maybe you won't have any problems crossing the street without looking both ways for a while, too. There is nothing in the code requiringcout to flush itself, so in order to accomplish what is wanted portably, use the flush.
The problem you were having, was not the '\b', but not having enough of them, and not having it in the right place.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include <Windows.h>
using std::cout;
using std::endl;
int main()
{
int i;
cout<<"Aborting in ";
for(i=9;i>=0;i--)
{
cout <<i ;
cout<<" seconds.\b\b\b\b\b\b\b\b\b\b";//Puts cursor back where i prints
Sleep(1000);
}
cout << endl;
}