Thread: cant get the loop to work right

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    5

    cant get the loop to work right

    im trying to create a program that right justifies the text entered by the user.. i already figured how to justify the text i just dont know how to get out of the loop that i created .. here's my code :
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #define Maxlength 200
    
    void justify(int x, char parag[Maxlength + 1]);
    
    main()
    {
    
          int length, ans;
          char text[Maxlength + 1];
          printf("Enter 1 to continue, any key to stop ");
          scanf("%d", &ans);
          while(ans == 1)
          {
             printf("Enter length of the line ");
             scanf("%d", &length);
             printf("Enter text:\n");
             justify(length, text);
             printf("\nEnter 1 to continue, any key to stop ");
             scanf("%d", &ans);
          }
    }
    
    void justify(int x, char parag[Maxlength + 1])
    {
    
       int currlinelength = 0;
       int wordlength;
       while(scanf("%s", parag) != EOF)
       {
       for(wordlength = 0; parag[wordlength] != '\0'; wordlength++)
          ;
       if(wordlength > x)
          printf("\nERROR: word length exceeds line length");
       if(currlinelength == 0)
       {
          printf("%s", parag);
          currlinelength = wordlength;
       }
       else
       {
          currlinelength += wordlength + 1;
             if(currlinelength > x)
             {
                printf("\n%s", parag);
                currlinelength = wordlength;
             }
             else
                printf(" %s", parag);
       }
       }
    }
    first off i try to ask the user if they want to continue.. after the user is done entering the text, i try to ask them again if they want to continue, but the program just stops once it's done justifying the entered text. . i cant seem to figure out how to have that statement "Enter 1 to continue ...." to show up again.. does anyone know what the problem is ?? thanx in advance

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Make sure ans is set to a value different from 1, and the loop will end.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    5
    but i cant get it to show the prompt ... "Enter 1 to continue any key to stop" .. so i cant enter any value for ans ... do u think the problem is with the while statement in the void justify function?
    Code:
    void justify(int x, char parag[Maxlengfth + 1])
    while(scanf("%s", parag) != EOF)
    {
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you're testing for EOF from scanf(), the only time you'll get this is when stdin has finished providing data to your program; eg the user has hit CTRL+D/Z (OS dependant key). Once this has happened, I am presuming that further calls to scanf() would be unrealiable, but this isn't something I've bothered looking at before.


    Also, you do realise that scanf() using %s will input a word only, not a group of words? Maybe you'd be better using fgets().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Replies: 26
    Last Post: 06-15-2005, 02:38 PM
  4. The Bludstayne Open Works License
    By frenchfry164 in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 11-26-2003, 11:05 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM