1

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:

  1. scanf(" %s", string); (notice the blank space before the %s).

And this other feature:

  1. scanf("%[^\n]s", string);

And this other:

  1. `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().

  • 1
    Make your life easier and use `fgets()`. And you may want to check the return value of library functions. I believe the authors had something in mind when they designed `scanf()` to return something. Using `scanf()` without a width specifier is also asking for trouble. See https://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – Harith May 18 '23 at 17:28
  • 2
    `%[^\n]s` in a [`fs`]`scanf` format string is almost always wrong. People who write that usually mean `%[^\n]` (no trailing 's'). The bracket expression is not a modifier, but rather the conversion specifier itself. – John Bollinger May 18 '23 at 17:29
  • ... or `scanf(" %[^\n]", string);` with a leading space, just as you have a leading space before `%c` in `scanf("%u %c", &length, &color);` (and don't need one before `%u`). Also note that `%s` and `%[]` are quite distinct specifiers with different behaviour. One is not a subset of the other. – Weather Vane May 18 '23 at 17:32
  • 2
    Questions seeking debugging help should generally provide a [mre] of the problem, which includes a **complete** function `main` and all `#include` directives, as well as the exact input required to reproduce the problem. This allows other people to easily test your program, by simply using copy&paste. – Andreas Wenzel May 18 '23 at 17:50
  • You're doing pretty well: you've managed to simultaneously hit quite a few of scanf's classic list of 17 gotchas to bite the unwary. You need to (a) not try to mix `scanf` and `getchar`, (b) not use `%[…]`, or at least, not misspell it as `%[…]s`, and (c) if you must use `%[…]`, use a leading space before it: `" %[…]`". Or, perhaps better, (z) abandon `scanf` entirely and [use something else](https://stackoverflow.com/questions/58403537). – Steve Summit May 18 '23 at 18:35
  • @dumping the core – _because it reads the `\n`_ - which `\n`? – Armali May 19 '23 at 07:34

0 Answers0