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

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.

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

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); 
        }
     }
  }

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

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

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); ?

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

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

This question has already been answered. Start a new discussion instead.