Question

I know this question gets asked a hundred times over, and I've scoured all of the possibilities, but I guess I'm not adept enough to know where this problem lies. I'm programming a program where I need to fill a struct with data (ints and strings). The first time I tried it it skipped over everything but the first one, but I didn't panic since I remembered from class I needed to use fflush(stdin) to overcome this. Websites I've searched vote against use of fflush(stdin), since it has undefined behaviour. They say using getchar() would eat the extra newline, thus fixing the problem. Hence my code:

int manNode(){
Item *p;
int helper;
p = (Item*)malloc(sizeof(Item));
printf("Welk type? (Taak:1, Examen:2, Voordracht:3)\n");
scanf("%u",&helper);                           //selecting an itemtype
if (helper < 1 || helper > 3)
{
    printf("wrong value, please try again");
    return 0;
}
getchar();                                     //I've just put getchars everywhere for safety.
p->entrytype = helper-1;
helper = 0;
printf("Vul een naam in:\n");
scanf("%s", p->name);                          //this one fills in fine
getchar();
printf("Vul een vaknaam in: \n");
scanf("%s", p->course);                        //this one gets skipped if I type more than one letter in the last scanf()                
getchar();
printf("Vul een starttijd in:\n");             //From here on out everything gets skipped
p->start = getTijd();
checkTijd(p->start);                           
printf("Vul een eindtijd in: \n");
p->end = getTijd();
checkTijd(p->end);

I know it's a bit messy, but focus on the scanfs and getchars. getTijd() also has a couple of scanfs in it that scan for integers, they also get skipped. I don't know where to go from here. (The code isn't incomplete, the rest is just irrelevant)

Was it helpful?

Solution

you can define a new getchar

#include <stdlib.h>
#include <stdio.h>

#define getchar(x) (scanf("%c", x))

int main ()
{
    char x, y[10];
    getchar(&x);
    scanf("%s", y);
    printf("got %s\n", y);
    return 0;
}

update: this may be a better approach

#include <stdlib.h>
#include <stdio.h>

void work_to_do()
{
#define getchar(x) (scanf("%c", x))
    char x, y[10];
    getchar(&x);
    scanf("%s", y);
    printf("got %s\n", y);
#undef getchar
}
int main ()
{
    work_to_do();
    return 0;
}

to solve the scanf newline ignorance and getchar (but still scanf ignores whitespace)

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

#define __getchar() (read(2, NULL, 1)) // 2 stands for standard error, we can make the user enter a character.

void work_to_do()
{   
    char y[10];
    __getchar();
    scanf("%s", y);
    printf("got %s\n", y);
    __getchar();
}
int main ()
{
    work_to_do();
    return 0;
}

and it's good to take a look at this: Read space-separated values from file

OTHER TIPS

scanf works as documented. Also, there is nothing wrong with scanf so long as its function is understood.

I created a copy of your code and distilled it to highlight what you want to do. You will need to fill-in the rest yourself.

In this code you need to enter a digit and press enter. Then enter a string and press enter, etc. Note. the fflush(stdout) statement is part of the standard C implementation per K&R. The fflush forces the contents of the buffer out to the console. This flushing makes for a reasonable user/computer dialog.

#include <stdio.h>
main()
{
    char string[100];
    int  helper;

    printf("Welk type? (Taak:1, Examen:2, Voordracht:3)\n");
    fflush(stdout);

    scanf("%d",&helper);    //select an itemtype
    if (helper < 1 || helper > 3)
    {
        printf("wrong value, please try again");
        return 0;
    }

    printf("Vul een naam in:\n");
    fflush(stdout);

    scanf("%s", &string[0]);
    printf("\n%s\n", string);

    printf("Vul een vaknaam in: \n");
    fflush(stdout);

    scanf("%s", &string[0]);
    printf("\n%s\n", string);

    printf("Vul een starttijd in:\n");
    fflush(stdout);
    scanf("%s", &string[0]);
    printf("\n%s\n", string);

    printf("Vul een eindtijd in: \n");
    fflush(stdout);
    scanf("%s", &string[0]);
    printf("\n%s\n", string);
}

This code ran on Eclipse with a Microsoft C Compiler.

Also, let me emphasize that if you enter: 1 AAA BBB CCC DDD and press enter then scanf will get executed five times. In this example, the code would run to completion!

So you need to trace your code logic (in this case a straight line) to see how the scanfs are invoked based on what data is entered.

Hope this helps. Please ask if more questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top