Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Caution: <code>fflush(stdin);</code> may be undefined behavior. Read: <a href="http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1052863818&amp;id=1043284351" rel="nofollow noreferrer">Why <code>fflush(stdin)</code> is wrong?</a></p> <blockquote> <p><strong><code>int fflush(FILE *ostream);</code></strong><br> The <code>ostream</code> points to an output stream or an update stream in which the most recent operation was not input, the <code>fflush</code> function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the <code>behavior</code> is <code>Undefined</code>.</p> </blockquote> <p>You can try a loop and read until <code>EOF</code> or <code>\n</code> given <a href="http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&amp;id=1043284392" rel="nofollow noreferrer">this FAQ entry</a> instead of <code>fflush(stdin)</code> as I have suggested below in my answer. </p> <p>Edit: thanks to @<a href="https://stackoverflow.com/users/15168/jonathan-leffler">Jonathan Leffler</a>:</p> <blockquote> <p>There are platforms where <code>fflush(stdin)</code> is fully defined (as a non-standard extension on that platform). The primary example is a well-known family of systems known collectively as Windows. Microsoft's specification of <a href="http://msdn.microsoft.com/en-us/library/9yky46tz.aspx" rel="nofollow noreferrer"><code>int fflush( FILE *stream );</code></a> <strong>If the <code>stream</code> is open for input, <code>fflush</code> clears the contents of the buffer</strong>.</p> </blockquote> <p>I have additional doubt in your code; what is <code>M</code> in expression <code>unsigned int b = pow(2,M-1);</code>? It should be an error if you don't define it. Are you posting complete code?</p> <p><strong>Regarding you logic of error detection:</strong> </p> <blockquote> <p>re-read the value if user enters an invalid </p> </blockquote> <p>No, <code>scanf()</code> does not return an error code. It returns the number of successful conversions. </p> <blockquote> <p><a href="http://www.cplusplus.com/reference/cstdio/scanf/" rel="nofollow noreferrer">int scanf ( const char * format, ... );</a><br> <strong>Return Value</strong><br> On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or <em>be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.</em> </p> <p><strong>If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (<code>feof</code> or <code>ferror</code>). And, if either happens before any data could be successfully read, <code>EOF</code> is returned.</strong></p> <p><strong>If an encoding error happens interpreting wide characters, the function sets <code>errno</code> to <code>EILSEQ</code>.</strong></p> </blockquote> <p>So actually depending on the error encountered the return value may be zero, EOF. You should use <a href="http://www.cplusplus.com/reference/cstdio/ferror/" rel="nofollow noreferrer"><code>int ferror ( FILE * stream );</code></a> and <a href="http://www.cplusplus.com/reference/cerrno/errno/" rel="nofollow noreferrer"><code>errno</code></a> macro for error detection (check the example given at link). </p> <p>Errors possible because of <em>invalid input</em> can be: </p> <blockquote> <p><code>EILSEQ</code>: Input byte sequence does not form a valid character.<br> <code>EINVAL</code>: Not enough arguments; or format is NULL.<br> <code>ERANGE</code>: An integer conversion would exceed the size that can be stored in the corresponding integer type. </p> </blockquote> <p>Check <a href="http://man7.org/linux/man-pages/man3/scanf.3.html" rel="nofollow noreferrer">scanf manual</a> for complete list.</p> <p><strong>Reason for infinite loop:</strong> </p> <p>The system keeps track of which input has been seen so far. Every call to <code>scanf</code> picks up from where the last one stopped matching input. This means that if an error occurred with the previous <code>scanf</code>, the input it failed to match is still left unread, as if the user typed ahead. If care isn't taken to discard error input, and a loop is used to read the input, your program can get caught in an infinite loop. </p> <p>So for example in your code: </p> <pre><code>x = scanf("%u", &amp;a); // ^ // need a number to be input </code></pre> <p>But suppose you don't input a number but an invalid string is entered e.g. <code>"name"</code> (instead of a number, as you say). This will cause the <code>scanf()</code> function to be fail when attempting to match an unsigned integer (<code>"%u"</code>), and the word <code>"name"</code> is left unread. So the next time through the loop, the <code>scanf()</code> doesn't wait for fresh user input, it tries to convert "name" again. </p> <p>Similarly if the input were <code>29.67</code>, the "<code>%u</code>" will match the first two characters only (the <code>29</code>), leaving the <code>.67</code> as unread input for the next call to <code>scanf()</code>.</p> <p>Even if the input is correct, as <code>29</code>, the newline that ended the input is still left unread. Normally that isn't a problem since most conversions automatically skip leading white-space such as the trailing newline from the previous line. However some conversions (<code>"%c"</code> and <code>"%["</code>) don't skip any leading white-space, so you have to do it manually.</p> <p><strong>To avoid this infinite-loop One suggestion:</strong> </p> <p>(remember: as I recommended the use of <code>ferror()</code>, error-value is preferable to detect invalid input. Additionally, it's just for learning purpose and if you need to implement a serious application you should use <code>fgets(str)</code> instead of <code>scanf()</code> followed by that parse <code>str</code> input to verify whether input is valid)</p> <pre><code>input: //fflush(stdout); //use if needed, as \n used in printf no need of fflush-stdout printf("\nEnter any integer: "); x = scanf("%u", &amp;a); // always wait for new symbols printf("%u", a); if(x == 0){ // x=0, if error occurred // read all unread chars while ((ch = getchar()) != '\n' &amp;&amp; ch != EOF); goto input; } </code></pre> <p>It's just a suggestion that will possibly work with your code (worked for me, your code + gcc). But if you use this technique incorrectly it may leave a bug in your code:</p> <p>Read <a href="http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1044873249&amp;id=1043284392" rel="nofollow noreferrer">How do I flush the input buffer?</a> </p> <blockquote> <p>If you are sure that unwanted data is in the input stream, you can use some of the following code snippets to remove them. However, if you call these when there is no data in the input stream, the program will wait until there is, which gives you undesirable results.</p> </blockquote>
    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. 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.
    3. VO
      singulars
      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