954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Refreshing output line in C

Dear all,

I am working on the code below:

int cookTime(int time) {
int second, minute;
    for (minute = time; minute > -1; minute--) {
       for (second = 59; second > -1; second--) {
	sleep(1);
	printf ("%i", minute); 
        printf (" : %i\n", second);
        }
     }
  }


Now this works as I want it. So it produces an output which goes like this:
5 : 59
5 : 58
5 : 57 ....
etc. until it reaches 0.

Now I would like to modify the printf line so that the numbers are constantly refreshing on the same line. I have tried using refresh() function in curses.h but my gcc compiler says that the symbols are not found as below:

Undefined symbols:
"_wrefresh", referenced from:
_cookTime in ccyfOosO.o
"_stdscr", referenced from:
_cookTime in ccyfOosO.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

I am doing this on my mac with 10.6.2 OS and the latest xcode development environment (I think xcode 3).

Any suggestions would be great.

rjani1

rjani1
Newbie Poster
7 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Well to use a library takes two steps.

The first tells the compiler about (say wrefresh(), it's name, parameters etc), and you do this with #include

The second tells the linker WHAT actually implements the functionality you requested. If your library is called libcurses.a, then you typically have -lcurses as a command line option when you compile.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Thanks Salem,

Using gcc -nested-functions -lcurses solved that issue. However, this did not rectify the output. So I may be approaching this problem the wrong way altogether. The output was still as before. Is there a different command other then printf that would produce the desired display?

Many thanks,

rjani1

rjani1
Newbie Poster
7 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

you could just use '\r' to make the cursor return back to the beginning of the line

int cookTime(int time) {
int second, minute;
    for (minute = time; minute > -1; minute--) {
       for (second = 59; second > -1; second--) {
	sleep(1);
	printf ("\r%2i : %2i", minute, second); 
        }
     }
  }
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hi Ancient Dragon,

I have tried that but this does not produce an output on the screen. I always seem to have to use the newline character (\n) in order to get anything.

I hope there is another way.

rjani1

rjani1
Newbie Poster
7 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
I have tried that but this does not produce an output on the screen. I always seem to have to use the newline character (\n) in order to get anything.


Try \r in your printf, and follow that with fflush(stdout); . (If you continue with this approach.)

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 
I have tried that but this does not produce an output on the screen. I always seem to have to use the newline character (\n) in order to get anything.

Have you tried fflush(stdout); ?

mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
 

> Is there a different command other then printf that would produce the desired display?
Well you could post some code which actually shows your attempt at using curses.

Abandoning it, and stating "do you have any other idea" isn't the way forward.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Dear Everyone,

Thank you so much for all your help. I had tried using fflush(stdout) before but never in conjunction with a carriage return (/r) in printf. So now the whole function goes like this:

int cookTime(int time) {
int second, minute;
    for (minute = time; minute > -1; minute--) {
       for (second = 59; second > -1; second--) {
	sleep(1);
	printf ("\r%i : %i", minute, second); 
        fflush(stdout);
        }
     }
  }


Works like a charm.:)

Many thanks,

rjani1

rjani1
Newbie Poster
7 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: