Note that there are some explanatory texts on larger screens.

plurals
  1. POInput validation from redirected stdin in C
    text
    copied!<p>I am trying to write a programme in C for an assignment that detects CSV format eg. decimalcommadecimal. and gives a output as to if the file is in the required format or not. I have tried using various inputs from stdin and using isdigit etc. but to no success. I'm a mega noob and have barely done any C programming before, I attempted to use regexc but couldn't figure out the syntax for using it.</p> <pre><code>#include &lt;ctype.h&gt; #include &lt;stdio.h&gt; const char EOL = '\n'; int cbreak(void); int check_dig(void); int value =1; char c; int main() { while((scanf("%c" ,&amp;c)) !=EOF&amp;&amp; value !=0) check_dig(); printf("\n%d\n",value); } int check_dig() { if (c == EOL) scanf("%c", &amp;c); if (c == isdigit) scanf("%c", &amp;c); else if (c == ',') scanf("%c", &amp;c); else value = 0; } </code></pre> <p>Thanks Guys I'm now to this stage but stumped as how to finish, I need to printf either 1 or 0 depending on validation and I want to do this as suggested using a return value.</p> <pre><code>#include &lt;ctype.h&gt; #include &lt;stdio.h&gt; int check_digit(int); int check_comma(int); int skip_char(int); int main() { int c; while ((c = getchar()) !=EOF) if (check_digit(c)) skip_char(c); else if (check_comma(c)) skip_char(c); else return 0; } int check_digit(int c) { if (isdigit(c)) return 1; else return 0; } int check_comma(int c) { if (c == ',') return 1; else return 0; } int skip_char(int c) { c = getchar(); // will this skip 2 chars as i have a while loop that has c=getchar()?? return c; } </code></pre>
 

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