so i´m taking harvard´s cs50 , benefit people take coure , stuck issue.
i´m making program called resize, take 3 command line arguments, first 1 is 'f'. exact quote:
the first (f) must floating-point value in (0.0, 100.0].
the question is: how set range of accepted values command-line argument. should use sscan() , atof() functions that.
there no way restrict command line arguments because entirely decided user of command. can argument , check against requirement. in case, take argument, , convert double value , check if in desired range.
if (argc != 4) { puts("not enough argument"); exit(1); } char *strf = argv[1]; char *end = null; double f = strtod(strf, &end); if (end == strf || *end != '\0') { puts("not valid floating point value"); exit(1); } if (errno == erange) { puts("value out of range"); exit(1); } if (f <= 0.0 || f > 100.0) { puts("value not in required range"); exit(2); } // ... other work f
Comments
Post a Comment