Thread: fgets stucks the program

  1. #1
    Registered User
    Join Date
    Aug 2017
    Posts
    23

    fgets stucks the program

    Hi,

    I need to get a sentence from a user and I have problem with that.

    scanf - not works because I need to get the spaces.

    gets - not secure and for some reason not working, it just skips the part where i need to write the sentence.

    fgets - don't even write: "Enter the argument: " and getting stuck.

    Line 17 - please help


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "Stack_point.h"
    
    
    // print_table
    void print_table() {
        char str[50];
        stack s1;
        stack s2;
        int i = 0;
        char poped;
        create_stack(&s1);
        create_stack(&s2);
    
        printf("Enter the argument: ");
        fgets(str,50,stdin);
        while(str[i] != '\0'){
            if(str[i] == 32){
                i++;
            }
            else if((str[i] > 64) && (str[i] < 91) || (str[i] > 47) && (str[i] < 58)){
                push(str[i], &s1);
                i++;
            }
        }
    }
    
    
    void first_main(){
      print_table();
    }
    
    void second_main(){
      printf("Second Main\n");
    }
    
    
    int main(){
      int taskNumber;
    
      while(1){
        printf("Please enter task number(1-2, 0 to exit): ");
        scanf("%d",&taskNumber);
    
        switch(taskNumber){
    
          case (0):
            printf("\nGood bye\n");
            return 0;
    
          case(1):
            first_main();
            printf("\n");
            break;
    
          case(2):
            second_main();
            printf("\n");
            break;
    
          default:
            printf("\nError. Invalid task.\n");
        }
      }
    }
    stack_point.h :
    Code:
    typedef char stack_type;
    
    typedef struct stack_element
    {
        stack_type data;
        struct stack_element *link;
    }    stack_element;
    
    typedef stack_element *stack;
    
    void create_stack (stack *);
    
    int push (stack_type, stack *);
    
    int pop (stack *, stack_type *);
    
    int stack_top (stack *, stack_type *);
    
    int stack_is_empty (stack *);
    
    int stack_is_full (stack *);

    stack_pont.c :
    Code:
    #include <stdlib.h>
    
    #include "Stack_point.h"
    
    void create_stack (stack *ps)
    {
        *ps = NULL;
    }
    
    int push (stack_type x, stack *ps)
    {
        stack_element *temp;
      temp = malloc(sizeof(stack_element));
      if (!temp)
            return 0;
      temp->data = x;
      temp->link = *ps;
      *ps = temp;
      return 1;
    }
    
    int pop(stack *ps, stack_type *px)
    {
        stack_element *temp;
    
      if (*ps)
        {
            temp = *ps;
        *ps = temp->link;
            *px = temp->data;
        free (temp);
            return 1;
        }
        return 0;
    }
    
    int stack_top (stack *ps, stack_type *px)
    {
        if (*ps)
            {
            *px = (*ps)->data;
            return 1;
        }
        return 0;
    }
    
    int stack_is_empty (stack *ps)
    {
        return *ps == NULL;
    }
    
    int stack_is_full (stack *ps)
    {
        return 0;
    }
    Last edited by DoK; 09-06-2017 at 01:25 AM.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    scanf leaves the newline character in the input stream, therefore when your fgets runs it reads only that and returns. You need a bit of code to ignore the newline, I use the following but remember that this only works if you are certain there's a newline left:
    Code:
    {
        int c;
        while ((c = getchar()) != EOF && c != '\n')
            continue;
    }
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The problem you're describing is being caused by switching between scanf() and fgets(). The scanf() function leaves the end of line character in the input buffer which fgets() retrieves as it's input, remember fgets() stops processing a line when it encounters th end of line character. Try adding a getchar() call after your scanf().

    Next look at this snippet:
    Code:
        while(str[i] != '\0'){
            if(str[i] == 32){
                i++;
            }
            else if((str[i] > 64) && (str[i] < 91) || (str[i] > 47) && (str[i] < 58)){
                push(str[i], &s1);
                i++;
            }
        }
    Be careful with that else if. What happens if the character is not a space and the else if() also doesn't match the character. By the way I recommend you use the character constants instead of the decimal values.

    Code:
            if(str[i] == ' '){

    Jim

  4. #4
    Registered User
    Join Date
    Aug 2017
    Posts
    23
    Thanks!
    I did this:

    Code:
    int main(){
      char taskNumberChar[4];
      int taskNumberInt;
    
      while(1){
        printf("Please enter task number(1-2, 0 to exit): ");
        fgets(taskNumberChar, 4, stdin);
        taskNumberInt = atoi(taskNumberChar);
        switch(taskNumberInt){
    
          case (0):
            printf("\nGood bye\n");
            return 0;
    
          case(1):
            first_main();
            printf("\n");
            break;
    
          case(2):
            second_main();
            printf("\n");
            break;
    
          default:
            printf("\nError. Invalid task.\n");

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program closes when buffer is over limit fgets
    By NativeCodeX in forum C Programming
    Replies: 13
    Last Post: 08-19-2011, 10:05 AM
  2. stucks on second command over network
    By botty in forum C++ Programming
    Replies: 1
    Last Post: 07-14-2011, 02:58 AM
  3. Program crashing at fgets() - any ideas why?
    By avi2886 in forum C Programming
    Replies: 4
    Last Post: 03-06-2009, 09:24 PM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. program ignoring fgets/scanf!
    By threahdead in forum C Programming
    Replies: 4
    Last Post: 01-12-2003, 04:43 PM

Tags for this Thread