I have a problem with my code. It should be parsing inputs like
1 34 a 2 -5 67 c
or
3455 124 -123 111 s 123 3544 f g
Or any other mix of characters (of length 1) and numbers. Here is my code:
char oper = 0;
int number = 0;
do{
if(scanf("%d", &number)==1)
{
...
}
else
{
scanf("%c", &oper);
if(oper == 'g')
{
...
}
if(oper == 'h')
{
...
}
...
}
} while(oper != '\n');
When it gets to a character the scanf("%c", &oper); copies “\n” into oper and I want it to be “a” or “s”
What is going wrong, and what can I do to fix it?
scanf(“%d” …) will scan past white space (blank, tab, newline) to get to the number.
scanf(“%c” …) will read the next character.
You need to figure out a way to scan past white space. Have you looked at the definitions for the formats to scanf?
Wikipedia seems quite helpful compared to normal man pages.