Thread: help with endless loop

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    4

    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.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    589
    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

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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); 
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    4
    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;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fscanf endless loop
    By krum in forum C Programming
    Replies: 3
    Last Post: 12-05-2007, 12:14 AM
  2. return to start coding?
    By talnoy in forum C++ Programming
    Replies: 1
    Last Post: 01-26-2006, 03:48 AM
  3. loop needed also how to make input use letters
    By LoRdHSV1991 in forum C Programming
    Replies: 3
    Last Post: 01-13-2006, 05:39 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM