I think you'll like this one better:
Code:
#include <stdio.h>
#include <windows.h> // windows.h is used for clearing the screen
// Removed conio.h
// Function prototypes
void clear_screen ( void );
void clear_buffer(FILE* file);
int main() // int main() instead of void main()
{
int n;
char ch;
clear_screen(); // replaced clrscr();
do
{
printf("Enter number: ");
scanf("%d",&n);
// Added a '\n' for the sake of the user
printf("Square of %d is %d\n",n,n*n);
// clear the buffer the windows way
clear_buffer(stdin);
printf("Do you want to enter another?");
scanf("%c",&ch); // Didn't replace this because it's
// pretty safe for char's and int's
// just not for strings
}while(ch=='y'); // Added a semi-colon
return 0; // How any happy program should end
}
/* This function was copied out of the FAQ
* Credit: Sunlight
* Only works on windows but similar functions
* can be written for other OS's */
void clear_screen ( void )
{
DWORD n; /* Number of characters written */
DWORD size; /* number of visible characters */
COORD coord = {0}; /* Top left screen position */
CONSOLE_SCREEN_BUFFER_INFO csbi;
/* Get a handle to the console */
HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
GetConsoleScreenBufferInfo ( h, &csbi );
/* Find the number of characters to overwrite */
size = csbi.dwSize.X * csbi.dwSize.Y;
/* Overwrite the screen buffer with whitespace */
FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
GetConsoleScreenBufferInfo ( h, &csbi );
FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );
/* Reset the cursor to the top left position */
SetConsoleCursorPosition ( h, coord );
}
/* This function is based off
* of Abda92's suggestion */
void clear_buffer(FILE* file)
{
int c;
while ((c = fgetc(file)) != '\n');
}