Sorry if you have seen this code already, but I have fixed it to almost work perfectly. My issue this time, is that, when A character is entered exceeding 9 characters, my program does not proceed to the appropreate loop.
Example of error:
Code:
Enter words no longer than 9 characters
Enter your 1st word :Bill
Enter your 2nd word :thiswillbemorethannine
Enter your 3rd word :Enter your 4th word :
The character checking loop is as follows;
Code:
for (b=0; b<10; b++)
{
buff[a][b]=fgetc(stdin);
if (buff[a][b]=='\n')
{
buff[a][b]='\0';
break;
}
if (b>=10)
{
printf("You have entered a word more than 9 characters\n");
while((buff[a][b] = fgetc(stdin))!='\n')
{
a=-1;
goto A;
}
}
}
Maybe I miss understand how loops work, but when 'b' gets increased to 9, the loop runs, then does it get increased to 10, and not follow through with the loop because b<10 is FALSE? Thats my logic of it, so since b = 10 now, it should do my ERROR loop.
I know for a fact the loop above doesn't remove all the information in the buffer, so I don't exactly know how to do that either. (I can't use fflush)
Here is my program;
Code:
#include <stdio.h>
#include <string.h>
char word[10][19];
char buff[10][19];
int a,b,c,d,e;
int main ()
{
printf("Enter words no longer than 9 characters\n");
A:for (a=0; a<10; a++)
{
if (a==0)
printf("Enter your 1st word:");
else if (a==1)
printf("Enter your 2nd word:");
else if (a==2)
printf("Enter your 3rd word:");
else
printf("Enter your %dth word:", a+1);
for (b=0; b<10; b++)
{
buff[a][b]=fgetc(stdin);
if (buff[a][b]=='\n')
{
buff[a][b]='\0';
break;
}
if (b>=10)
{
printf("You have entered a word more than 9 characters\n");
while((buff[a][b] = fgetc(stdin))!='\n')
{
a=-1;
goto A;
}
}
}
for (b=0; buff[a][b]!='\0'; b++)
if (buff[a][b] >='a' && buff[a][b]<='z')
buff[a][b]=buff[a][b] - 'a' + 'A';
else if (buff[a][b] >='A' && buff[a][b]<='Z')
buff[a][b]=buff[a][b] - 'A' + 'a';
b--;
for(c=0; b>=0; b--, c++)
word[a][c]=buff[a][b];
for(b=0; buff[a][b]!='\0'; b++, c++)
word[a][c]=buff[a][b];
word[a][c]='\0';
}
printf("Your unsorted list of words\n");
for (e=0; e<10; e++)
printf("%s \n",word[e]);
return 0;
}