Reputation: 19
For part of a homework assignment I need to loop a prompt that has the user enters words until they either enter 20 words or until they enter the word 'done'. Currently, I have both parameters satisfied, but when if I enter the word 'done', it gets scanned into the array as well, which is not what I want to do.
This is my current function:
int row = 0;
int column = 0;
int i = 0;
while(i < 20)
{
printf("Enter words you would like hidden in the puzzle. Type 'done' when finished:\n");
scanf("%s",a[i]);
if(strcmp(a[i],"done") == 0)
{
break;
}
if((strlen(a[i]) > row) || (strlen(a[i]) > col))
{
printf("Error. Word was too long to enter into puzzle.\n");
}
else
{
i++;
}
}
The array 'a' is an array of character strings. I know that the line scanf("%s",a[i]);
is scanning the word 'done' into the array, I just don't know how to tweak it so that doesn't happen.
Can someone please help me figure this part out?
Upvotes: 0
Views: 87
Reputation: 18747
Try this:
char input[256];
while(i < 20)
{
printf("Enter words you would like hidden in the puzzle. Type 'done' when finished:\n");
scanf("%s",&input);
if(strcmp(input,"done") != 0)
{
strcpy(a[i],input);
if((strlen(a[i]) > row) || (strlen(a[i]) > col))
{
printf("Error. Word was too long to enter into puzzle.\n");
}
i++;
}
else
break;
}
Upvotes: 1