Aborting in......

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...

Thanks in advance...
With Warm Regards...
Kinjal
Last edited on
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.

@jonnin...thanks for your reply...

My compiler tools don't support compiler tools of gotoxy(x,y)...
won't there be any other solution for it similar to my logic...

A quick and appreciable answer will be highly appreciated...

Again...Thanks in advance...
With Warm Regards...
Kinjal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
}

system("cls") clears the console output.

Last edited on
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.
@dhayden... Thanks for your reply...

I am a noob...and couldn't understand ur flush things...i wanted some simple logics...

Please reply soon..
with warm regards...
Kinjal
Last edited on
@bokisof... Thanks for your reply...

Sorry... i can't use clear screen because i have a long program... Clearing the screen would clean other things... But that's not my purpose...

Please reply soon...
With warm regards...
Kinjal
Last edited on
@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;
}

Here is link for std::flush
http://stackoverflow.com/questions/14105650/how-does-stdflush-work
The example which Neil Anderson gave is very good to understand what std::flush does.
Last edited on
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 requiring cout to flush itself, so in order to accomplish what is wanted portably, use the flush.
@kinjal2209

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;
}
@whitenite1 thanks for your reply...

you solved my problem very well by pointing out just a silly mistake thanks....

others were right in their respective places but I actually wanted what @whitenite1 replied...

Although thanks to everyone...

With warm regards...
Kinjal
Topic archived. No new replies allowed.