Click here to Skip to main content
13,086,019 members (83,668 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I searched this in the internet and for some reason it doesn't work *which is probably because of the "usleep" and "fflush" there.
mind explaining to me what they do--or better yet give me something that I can replace those with?

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
typedef struct { int *x, n; } tower;
tower *new_tower(int cap)
    tower *t = calloc(1, sizeof(tower) + sizeof(int) * cap);
    t->x = (int*)(t + 1);
    return t;
tower *t[3];
int height;
void text(int y, int i, int d, const char *s)
    printf("\033[%d;%dH", height - y + 1, (height + 1) * (2 * i + 1) - d);
    while (d--) printf("%s", s);
void add_disk(int i, int d)
    t[i]->x[t[i]->n++] = d;
    text(t[i]->n, i, d, "==");
int remove_disk(int i)
    int d = t[i]->x[--t[i]->n];
    text(t[i]->n + 1, i, d, "  ");
    return d;
void move(int n, int from, int to, int via)
    if (!n) return;
    move(n - 1, from, via, to);
    add_disk(to, remove_disk(from));
    move(n - 1, via, to, from);
int main(int c, char *v[])
    if (c <= 1 || (height = atoi(v[1])) <= 0)
        height = 8;
    for (c = 0; c < 3; c++)  t[c] = new_tower(height);
    for (c = height; c; c--) add_disk(0, c);
    move(height, 0, 2, 1);
    text(1, 0, 1, "\n");
    return 0;
Posted 6-Apr-13 23:05pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Google can:
usleep function[^]
fflush function[^]

The former is there to let you see the movement before it goes on to the next, and the second to make sure the output is all written (should be unnecessary anyway).

I can't suggest replacements, as I have no idea what environment you are trying to run this in!
DxWhiteman 7-Apr-13 7:28am
ooh, I see does that mean it isn't absolutely necessary? *I tried erasing those btw and the program ends up doing stuff and closes. =_=

oh and I'm trying to make it work on Dev C++ as a c program--if that's what you mean(?)
OriginalGriff 7-Apr-13 7:42am
It's isn't absolutely necessary, no - but it will not show you intermediate steps unless you can read really, really, really fast! :laugh:
Try replacing them with a read from stdin and it should "pause" for you to see what you are doing.
BTW: unless you are learning C using DevC++, I would look for a C++ version of this instead: C doesn't have classes and so makes a poor C++ starter! :)
DxWhiteman 7-Apr-13 7:53am
haha, now I see what you mean. xD
I'll do what I can, you've been very helpful Thanks :)

*frankly speaking, I don't know much--if not a little--about programming so I might bother you with a few things or so. please bear with me! xD
OriginalGriff 7-Apr-13 8:05am
If you are just getting started, then would you mind if I made a suggestion?
Don't start with "C" (or C++) - there are easier languages to start with, and in better development environments than DevC++.

Have a look at C#, and Visual Studio (If cost is an issue, it is available in a free Express edition from Microsoft the limitations of which you will probably never notice...)
It's a much more "modern" language, and it it a lot faster to write apps in. There are also a heck of a lot of books and tutorials helping you learn it. "C" is not dead - far from it - but it is a "specialist" language these days.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

This little program uses so-called ANSI screen control sequences. For example does the
ESC n ; m H 

sequence position the cursor to row n, column m. These control sequences were used in the good old days to control the cursor and other properties on a character-based screen.

If the output device doesn't understand those control sequences it just prints garbeled characters. So that may be the reason you are not seeing anything. The usleep and fflush look ok to me. Depending on your platform look for shell that understands ANSI control sequences and you will be fine.
DxWhiteman 7-Apr-13 7:32am
hmm, I'm using Dev C++ and I saved it as a C program, it doesn't run and displays a linker error: "undefined reference to usleep"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170813.1 | Last Updated 7 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100