Note that there are some explanatory texts on larger screens.

plurals
  1. POCommon macro to read input data and check its validity
    primarykey
    data
    text
    <p>I saw on Stack Overflow that many similar questions are repeated and they are related to the reading of one input data item from <strong>stdin</strong> and check its validity.</p> <p>The data could be integer <code>"%d"</code>, double <code>"%f"</code>, string <code>"%s"</code>, unsigned int <code>"%u"</code>...</p> <p>And I want to develop a common macro which could be used for the <strong>majority</strong> of these questions.</p> <p><strong>Example 1 of questions</strong></p> <p>The OP could ask:</p> <ul> <li>scan the input data</li> <li>the data should be integer so <code>11a</code>, <code>aaa</code>, <code>aa44</code>,... inputs are not allowed. Only integer followed with a white space (refer to <a href="http://www.cplusplus.com/reference/cctype/isspace/" rel="nofollow">isspace()</a>) is allowed</li> <li>other conditions could be present in the question like: input integer should be <code>&gt;3</code> and <code>&lt;15</code></li> </ul> <p><strong>Example 2 of questions</strong></p> <p>The OP could ask:</p> <ul> <li>scan the input data</li> <li>the data should be double so <code>11.2a</code>, <code>aaa</code>, <code>aa44.3</code>,... inputs are not allowed. Only double followed with a white space (refer to <a href="http://www.cplusplus.com/reference/cctype/isspace/" rel="nofollow">isspace()</a>) is allowed</li> <li>other conditions could be present in the question like: input double should be <code>&gt;3.2</code> and <code>&lt;15.0</code></li> </ul> <p>Is it possible to develop a common macro like</p> <pre><code>#define SCAN_ONEENTRY_WITHCHECK(FORM,X,COND) // FORM: format of the input data like "%d", "%lf", "%s" // X: address where to store the input data // COND: condition to add in the check of the input data .... // example of calling the macro in the main() int a; SCAN_ONEENTRY_WITHCHECK("%d", &amp;a,(a&gt;3 &amp;&amp; a&lt;15)) </code></pre> <p>The macro should scan the data and IF one of the following criteria is not true then print a message to the user asking him to enter again. and repeat that until the user enters valid data?</p> <p>Criteria:</p> <ul> <li>input data type should be the same indicated by format</li> <li>input data should be followed by white space indicated in the <a href="http://www.cplusplus.com/reference/cctype/isspace/" rel="nofollow">isspace()</a></li> <li>input data should be valid the condition <code>COND</code></li> </ul>
    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.
 

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