I'm wanting to receive UI and process that information. Particularly i'm receiving (in this order) 2 (unsigned) ints, 1 char, and an array of strings. The problem is that it doesn't read the string with the scanf()
. Why? because it reads the \n
. The code looks something like this (just a part of it)
int main(){
//(...)
for (int i = 0; i < t; i++){
scanf("%u", &length);
color = getchar();
scanf("%[^\n]s", string);
resultados[i] = trafficLight(string, length, color); //process the information
}
//(...)
To avoid that problem i tried a few things. None of them worked... One of the things i tried was this: 1.
scanf("%u %c", &length,&color); //unsigned int lenght; char color; (all of this above in the code)
scanf("%s", string);
This did not work. So i also tried this feature:
scanf(" %s", string);
(notice the blank space before the %s).
And this other feature:
scanf("%[^\n]s", string);
And this other:
-
`color = getchar();`
Then, i do not know how to fix this. I appreciate the help. Also, while trying to solve all of this, i encountered other problem. This other problem occurred when i tried the first thing. What occurred? Well, the loop never ended. I'm also aware that the problem should be mostly that i don't undestand scanf().