One tip not related to your problem: Don't use 'system("cls");'. It is DOS/Windows specific and dangerous (what if someone put a cls.exe in the directory with a virus?)...
Here's a clearscreen.c for you: Works on Unix terminals who accept ANSI codes and on Windows as well (using GCC):
Code:
/* console.h */
#ifndef CONSOLE_H_INCLUDED__
#define CONSOLE_H_INCLUDED__
void clearscreen( void );
void gotoxy( int, int );
#endif
Code:
/* console.c */
#if defined(__linux) || defined(__APPLE__)
// Linux or MacOS
#include <stdio.h>
/* Uses ANSI codes */
void clearscreen(void) { fputs("\x1b[2J", stdout); }
// coordinates start at (0,0).
void gotoxy(int x, int y) { printf("\x1b[%d;%dH", ++x, ++y); }
#else
#include <windows.h>
/* Uses Windows API */
void clearscreen(void)
{
HANDLE hConsole;
COORD coord = { 0 };
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD dwConsoleSize, dwWriten;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsole, &csbi);
dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y;
FillConsoleOutputCharacter(hConsole, (TCHAR)' ',
dwConsoleSize, coord, &dwWriten);
FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
dwConsoleSize, coord, &dwWriten);
SetConsoleCursorPosition(hConsole, coord);
}
void gotoxy(int x, int y)
{
HANDLE hConsole;
COORD coord = { .X = x, .Y = y };
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsole, coord);
}
#endif
Now you can use 'clearscreen();'.