As tabstop mentioned, you only need to "flush" the input buffer once per iteration (and yes, iteration, not recursion: you do not need to call main recursively). I still am not clear where does flushall() come from (<conio.h>?), but I would do is write the function myself using the standard library.
Thus, I would propose this instead:
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
char *getLine(char *str, size_t size, FILE *stream);
void clearScreen();
void ignoreUnreadInput();
int main(void) {
int UserChoice;
do {
char UserName[25];
char UserPhone[10];
printf("\nName: ");
getLine(UserName, sizeof(UserName), stdin);
printf("\nPhone: ");
getLine(UserPhone, sizeof(UserPhone), stdin);
printf("\nContinue [Y/N] ? : ");
UserChoice = getchar();
ignoreUnreadInput();
clearScreen();
} while (toupper(UserChoice) == 'Y');
printf("\nPress enter to exit...");
getchar();
return 0;
}
char *getLine(char *str, size_t size, FILE *stream)
{
if ((str = fgets(str, size, stream)))
{
char *new_line = strchr(str, '\n');
if (new_line)
{
*new_line = '\0';
}
}
return str;
}
void clearScreen()
{
clrscr();
}
void ignoreUnreadInput()
{
int ch;
while ((ch = getchar()) != EOF && ch != '\n');
}
I implemented a getLine() function to replace gets(). It uses fgets(), but additionally removes the newline that might be read into the string by fgets().
Instead of scanf() I used getchar() as suggested by tabstop.
Instead of just using one getchar() call as suggested by Adak, I implemented ignoreUnreadInput() similiar to how it was done in the tutorial I linked to, since a user could enter multiple characters instead of just 'y' or 'n'.