validation - Vector Input Using Scanf (C language) -
first time user of stackoverflow, long time visitor. thought prudent start being more involved. first, have small question :)
basically i'm trying take in 3 numbers (can positive, negative or decimal) each separated space (x y z) using scanf , placed array/struct (ignoring spaces), , returning error message if user not input explicitly numbers or input them in incorrect format. possible scanf? alternative?
i've searched around @ different questions asked couldn't find answer understand (first timer c). link, explanation or little bit of example code appreciated :)
it sounds though want 3 numbers on 1 line; having them spread on multiple lines not acceptable. you've not indicated should happen if there's fourth number on line.
using plain scanf()
, difficult, if not impossible, control this. using fgets()
, sscanf()
feasible:
char line[4096]; while (fgets(line, sizeof(line), stdin) != 0) { double v1, v2, v3; if (sscanf(line, "%g %g %g", &v1, &v2, &v3) != 3) ...format error... ...save v1, v2, v3 structure or array... }
optionally, can find out conversion finished , check there no debris on line after 3 numbers.
Comments
Post a Comment