Thread: Program skips fgets, can't find out why

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    Exclamation Program skips fgets, can't find out why

    So, There isn't exactly too much to explain here. I'm building this one program that's eventually going to take flash card terms, wrtie and read them into a file, and then let you guess the word of the definition.

    I'm not even done making the part that writes the terms into the struct, and I've gotten a problem.

    When it gets to asking what the first definition is, it skips over my fgets() command that would be the answer, and asks what the word is for this definition. I try commenting out the first if statement, and it does this for the second function. I tried isolating the first if statements into their own little program, and it works fine in there, which would point to it being a problem with the main program, but I can't seem to find an error there either.

    I should mention I wrote all 50 of the if statements that save into the struct using a bash script I whipped up in command prompt, but I don't think thats the issue, because all the other ones work fine.

    Any help would be appreciated!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    	scanf("%s", string);
    	if (atoi(string) == 1) {  //if using already made cards
    		//Put something here after you set up [2]
    	}
    Instead of using "atoi" I would just scanf into an integer variable.

    Edit1: Found the fgets finally.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I should mention I wrote all 50 of the if statements that save into the struct using a bash script
    Try moving the for loop you wrote in bash to being inside the C code instead.

    > c1, c2, c3, c4, c5, c6 OMG
    Anytime you start putting numeric suffixes on variable names, there should be a voice in your head screaming 'just use a f.... array dude!"


    Code:
    struct Card {
    	char def[500];
    	char word[30];
    } cards[50];
    
    int flashCardMaker(int n, Card cards[]) {
        char buff[BUFSIZ];  // stop using messy global variables for your input
        for ( int i = 0 ; i < n ; i++ ) {
            printf("What is term %d",i+1);
            fgets(buff, sizeof(cards[i].def), stdin);
            strcpy(cards[i].def, buff);
            printf("What is word %d",i+1);
            fgets(buff, sizeof(cards[i].word), stdin);
            strcpy(cards[i].word, buff);
        }
    }

    fgets() and scanf() do not play nicely together, because scanf() only processes the minimum number of characters to satisfy the conversion. This typically means it leaves a \n on the input stream (the very same \n that fgets() detects as end of line).

    The best solution is to use fgets() for ALL input, then use sscanf() / strtol() / strtod() / str... or whatever to validate and convert the in memory buffer of the input just read.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Sep 2018
    Posts
    4
    Quote Originally Posted by Salem View Post
    > I should mention I wrote all 50 of the if statements that save into the struct using a bash script
    Try moving the for loop you wrote in bash to being inside the C code instead.

    > c1, c2, c3, c4, c5, c6 OMG
    Anytime you start putting numeric suffixes on variable names, there should be a voice in your head screaming 'just use a f.... array dude!"
    okay so, I just wanna start by saying, Salem, your a beautiful genius. I'm still learning the ropes on programming, and while I've gotten a lot down in theory, that sort of programmer mind set is still kinda lost to me. The reason I had like 50 if statements is because I couldn't find any way to get the struct to call things dynamically, and not only does the array fix my huge number of structs, it allows me to just use a single for statement. I feel so stupid for not thinking of this before, so thank you for the pointer!

    As for my problem, I still haven't figured it out, but seeing as it skips the first fgets I have, my friend (who had no idea what I was saying when I told her about it) just said to put it in a second time, and so I did that, and then, working off her cheesed work around, I put it outside of the new for statement I had. this effectively skipped the useless fgets statement before the for statement, letting the rest of the program run smoothly. If someone does happen to find the problem though, much obliged to you, I'd like to see what it is. until then, Thanks I guess, lmao.

    EDIT: Just wanted to add that I have yet to try making them all fgets, so I'll try that when (if) I get the chance this week.
    Last edited by AntonRobotron; 09-17-2018 at 08:18 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my program skips the second scanf. pls help!
    By aske10 in forum C Programming
    Replies: 5
    Last Post: 01-08-2016, 03:39 AM
  2. Program skips scanf
    By nic050 in forum C Programming
    Replies: 2
    Last Post: 06-05-2015, 10:54 PM
  3. fgets after scanf, skips over fgets?
    By willane1965 in forum C Programming
    Replies: 1
    Last Post: 08-17-2014, 11:13 PM
  4. fgets prompt in while loop "skips"
    By Jesdisciple in forum C Programming
    Replies: 16
    Last Post: 04-12-2009, 06:55 PM
  5. program skips over code
    By willc0de4food in forum C Programming
    Replies: 9
    Last Post: 11-16-2006, 06:38 PM

Tags for this Thread