1
I have to read N lines with one of the following 3 formats:
- "P k": the letter P followed by k being k an integer number;
- "A k": the letter To followed by k being k an integer number;
- "Q": the letter Q.
Since I don’t know if the person goes by P/A or Q first I just have to read a letter.
If the letter is Q, I write the code to the letter. If it is P/A I still have to read an integer and then make the code of the letter P/A and its number.
My problem is I do scanf
for the first letter this way more at least:
for(idx = 0; idx < N ; idx++)
scanf("%c",&opc);
Within the for
I have some more code. The problem is this. The first time goes well. The second time it comes to scanf
of char
compiler automatically puts ASCII code Character 10 representative of line break.
This is because the scanf
does not clear the Enters buffer so I did this:
for(idx = 0; idx < N ; idx++)
scanf("%c\n",&opc);*
I added a \n
us scanf
for him to somehow consume the line break.
This way I get another error that is: (example) if the first input is Q for example it asks me two the Q to read it only once.
So I put a Q and nothing happens. I put the second that there it advances and follow normally.
puts a
\n
before the%c
.scanf("\n%c", &opc);
– Brumazzi DB
The best (safer, more controllable) option to get user input is with the
fgets()
possibly followed by removing ENTER and/orsscanf()
to isolate parts of the input.– pmg