Note that there are some explanatory texts on larger screens.

plurals
  1. POscanf gets skipped, even with safeties (getchar())
    primarykey
    data
    text
    <p>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 <code>fflush(stdin)</code> to overcome this. Websites I've searched vote against use of <code>fflush(stdin)</code>, since it has undefined behaviour. They say using <code>getchar()</code> would eat the extra newline, thus fixing the problem. Hence my code:</p> <pre><code>int manNode(){ Item *p; int helper; p = (Item*)malloc(sizeof(Item)); printf("Welk type? (Taak:1, Examen:2, Voordracht:3)\n"); scanf("%u",&amp;helper); //selecting an itemtype if (helper &lt; 1 || helper &gt; 3) { printf("wrong value, please try again"); return 0; } getchar(); //I've just put getchars everywhere for safety. p-&gt;entrytype = helper-1; helper = 0; printf("Vul een naam in:\n"); scanf("%s", p-&gt;name); //this one fills in fine getchar(); printf("Vul een vaknaam in: \n"); scanf("%s", p-&gt;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-&gt;start = getTijd(); checkTijd(p-&gt;start); printf("Vul een eindtijd in: \n"); p-&gt;end = getTijd(); checkTijd(p-&gt;end); </code></pre> <p>I know it's a bit messy, but focus on the scanfs and getchars. <code>getTijd()</code> 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)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload