Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your problem is likely that <code>scanf</code> happily does nothing if the format string that is its first parameter doesn't match the available input. That means it won't change the value of <code>choice</code>, so it will still be 2.</p> <p>The cause of this is probably that what you input doesn't match your format strings. You can detect when this happens by checking the return value of <code>scanf</code> - it will return the number of variables written to, basically. If that is less than the number of format specifiers in your format string, something went wrong.</p> <p>At that point, you probably want to consume all the available input (maybe something like <code>int c; do { c = getchar(); } while (c != '\n' &amp;&amp; c != EOF);</code> for a simple program like yours) and then prompt the user again.</p> <p>In particular, I believe your <code>scanf("%c", ...)</code> is likely the culprit: <code>%c</code>, unlike most <code>scanf</code> specifiers, will not ignore leading whitespace, but accept any character. So if you typed in "firstname lastname 1980.6.11 f", for example, the previous <code>scanf</code> call will just have consumed "6.11.1980", leaving " f" in the input buffer (note the space). Then the <code>scanf</code> with <code>%c</code> will read the space into the gender field, and leave the "f" in the input buffer. On the next go around, <code>scanf("%d",&amp;choice);</code> will not do anything because "f" is not a valid number, <code>choice</code> will remain 2 and the "f" will get read as the first name on the next student entry, further confusing matters...</p> <p>The solution is, I believe, to use <code>scanf(" %c", ...);</code> to explicitly consume leading whitespace.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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