I am trying to clear the buffer with the while(getchar() != '\n') ; but it does not appear to be clearing. What am I doing wrong?
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define str_length 100
void main()
{
char str1[100]; /* array for first string */
char str2[100]; /* array for second string */
char *pstr1 = NULL; /* pointer to str1 */
char *pstr2 = NULL; /* pointer to str2 */
int count1; /* length of str1 */
int count2; /* length of str2 */
char more = 'y' ; /* variable to continue with the loop y||Y */
int i = 0; /*loop counter */
char *pbuffer1 =str1;
char *pbuffer2 =str2;
printf("Please enter the first word\n");
pstr1 = pbuffer1;
count1 = strlen(pstr1);
while((*pbuffer1++ = getchar()) != '\n');
*(pbuffer1 - 1) = '\0';
printf("Please enter the second word\n");
pstr2 = pbuffer2;
count2 = strlen(pstr2);
while((*pbuffer2++ = getchar()) != '\n');
*(pbuffer2 - 1) = '\0';
do
{
/* concatenate str2 to end of str1 */
if(count1 == count2 ||count1 < count2 )
/*{
for(i = 0; i < count1; i++)
printf(" you entered %c\n", pstr1 + i);*/
{
printf(" %s %s and \n", pstr1,pstr2);
printf(" %s %s\n",strrev(pstr1), pstr2);
}
else
/* concatenate str1 to end of str2 */
{
printf(" %s %s and \n", pstr2,pstr1);
printf(" %s %s \n",strrev(pstr2),pstr1);
}
printf(" Would you like to continue? y or n:\n"); /* ask user if they want to enter more data */
scanf("%c", &more); /* read the input */
while(getchar() != '\n') ; /* flush input buffer it appears that the scanf leaves a \n inthe buffer */
}while(more == 'y' || more == 'Y'); /*Test to continue or not*/
printf("The strings enter were %s\n %s\n", pstr1, pstr2);
}