i'm trying writing program using c (very new c - been learning 2 weeks), , wanted string of input user stdin, in string has char, followed 2 floats (each has space in between). example be: "y 2.1 1.1". question how can obtain , store 3 inputs, while making sure first char, , following 2 inputs floats?
how can obtain , store 3 inputs, while making sure first char, , following 2 inputs ints?
problem code there spaces @ end, , don't know how rid of it.
a simple way use sscanf()
, check if there anything after scanned variable use "%n"
record location of scan @ point.
char in1; int in2, in3; int n = 0; sscanf(array,"%c %d %d%n", &in1, &in2, &in3, &n); if (n > 0 && (array[n] == '\n' || array[n] == '\0')) { success(in1, in2, in3); }
it important check results of sscanf()
. 1 way check return value should 3 here. unfortunately not tell if exist after in3
. setting n == 0
, testing n > 0
, code knows scanning proceeded way "%n"
. code can test character scanning stopped at.
Comments
Post a Comment