-
help with endless loop
Please help me with this code fragment.
for(;;)
{
/* check array with strcmp */
for (array_counter=0; array_counter<=10; array_counter++)
{
if (strcmp(user, words[array_counter]) == 0)
{
printf("DUPLICATE \"%c\"", user);
break;
}
else
strcpy(words[array_counter], user);
}
}
It is supposed to compare a user-entered string with an array of strings. If it is already in the array, the DUPLICATE message should print. If not, it should add it to the list.
When I run the program, it accepts the user string and then goes into an endless loop printfing (DUPLICATE " over and over again.
Any advice you can give would be helpful. Thank you.
-
the break; you have will only break you out from the first for statment. It would be cleaner if you used while(flag) as your forever loop and then you set the flag to 0 when you want to stop your loop
-
I think you need a couple of extra variables. One which tells the current number of words in the array, and another which tells you whether there is a duplicate. I also changed the printf format to %s.
Code:
bool duplicate;
.
.
/* check array with strcmp */
//num_of_words is the current number of words in the array
duplicate = false;
for (array_counter=0; array_counter<num_of_words; array_counter++)
{
if (strcmp(user, words[array_counter]) == 0)
{
printf("DUPLICATE \"%s\"", user);
duplicate = true;
break;
}
}
if (!duplicate)
strcpy(words[num_of_words++], user);
}
-
Thanks so much for your help. Here is the final version, in case anyone is curious as to how it turned out.
/* The purpose of this program is to accept up to 10 user-entered
words of up to 10 characters each. When a duplicate word is
entered, the program will print a message, sort all words in
ascending ASCII order, and print all the words.
*/
#include <stdio.h>
#include <string.h>
main()
{
char words[10][11]; /* an array of 10 10-char(max) words */
char user[11]; /* the word entered by the user */
int counter; /* an array counter */
int num=0; /* the current # of words in the array */
int flag = 0; /* used to mark that a duplicate has been found */
int pass; /* the # of passes in the sorting loop */
char hold[11]; /* an array used as temp storage for sorting */
printf("\nEnter string: ");
scanf("%10s", user);
fflush(stdin);
while(!flag)
{
for (counter=0; counter<=num; counter++)
{
if (strcmp(user, words[counter]) == 0)
{
printf("DUPLICATE \"%s\"", user);
flag = 1;
break;
}
}
if(!flag)
{
strcpy(words[num++], user);
printf("Enter string: ");
scanf("%10s", user);
fflush(stdin);
}
}
for (pass=1; pass<=num+1; pass++)
{
for (counter=0; counter<=num; counter++)
{
if(strcmp(words[counter], words[counter+1]) > 0)
{
strcpy(hold, words[counter]);
strcpy(words[counter],words[counter+1]);
strcpy(words[counter+1],hold);
}
}
}
for (counter = 0; counter<=num+1; counter++)
printf("%s\n", words[counter]);
printf("END OF PROGRAM\n");
return 0;
}
-
>fflush(stdin);
The fflush function is undefined for input streams, this is wrong. If you want an easy way to clear the input buffer, use a loop in place of that ghastly construct:
while ( getchar() != '\n' );
-Prelude