Thread: while loop that wont loop

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    Unhappy while loop that wont loop

    Being new and all in C, Im facing a problem which I dont know how to get out of. The following is just a simple exercise that was set and for some strange reason I cant get my loop to loop. I keep getting this error msg "segmentation fault"....as to what it means...no idea! Any help would be much appreciated.

    #include <string.h>
    #include <stdio.h>
    #include <ctype.h>

    main()
    {
    char buf[256];
    char ans;
    int pos;

    do
    {
    printf("Please enter a word\n");
    scanf("%s", buf);

    pos = strlen(buf) - strlen(".web");

    if (0==strcmp(buf + pos, ".web"))
    {
    buf[pos]='\0';
    strcat(buf, ".html");
    printf("%s\n", buf);
    }

    printf("To enter another word type 'Y'\n");
    scanf("%c", ans);

    ans=toupper(ans);


    } while (ans=='Y');

    exit(0);
    }
    simple is always an understatement.....

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >scanf("%c", ans);
    scanf takes a pointer, ans is not a pointer so you need to add the address of operator in front of ans and cover for the lameness of scanf by also reading the newline that it is sure to leave in the input stream:
    scanf ( "%c%*c", &ans );

    You'll also want to read the newline in just about every case of scanf in your program. Note that arrays don't need the address of operator when using them with scanf because the array name that you use is already a pointer.

    >exit(0);
    Use return to exit the main function. And the use of exit requires that you include stdlib.h.
    Code:
    #include <string.h> 
    #include <stdio.h> 
    #include <ctype.h> 
    
    main() 
    { 
      char buf[256]; 
      char ans = '\0'; 
      int pos; 
    
      do 
      { 
        printf("Please enter a word\n"); 
        scanf("%s%*c", buf); 
        pos = strlen(buf) - strlen(".web"); 
        if (0==strcmp(buf + pos, ".web")) 
        { 
          buf[pos]='\0'; 
          strcat(buf, ".html"); 
          printf("%s\n", buf); 
        } 
        printf("To enter another word type 'Y'\n"); 
        scanf("%c%*c", &ans); 
        ans=(char)toupper(ans); 
      } while (ans=='Y'); 
      return 0; 
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522
    Hey, I was just about to say that.
    *shudders at scanf*
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: while loop that wont loop

    Originally posted by sweets
    if (0==strcmp(buf + pos, ".web"))
    {
    buf[pos]='\0';
    strcat(buf, ".html");
    printf("%s\n", buf);
    }
    Well, this part seems very suspicious to me... Could be wrong though...
    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >*shudders at scanf*
    You and me both

    >Well, this part seems very suspicious to me...
    It's nasty, but it works...for the most part.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    94

    Talking

    thankyou thankyou thankyou

    you cant imagine what a relief it is to see it work...never thought of using %*c...as for the exit, I thought I had gotten rid of that


    Sophie
    simple is always an understatement.....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM