Learning Scanf

So I am trying to enter a competition and they said that cin and cout in iostream are just too slow for that kind of stuff

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

    int testcase;
    scanf("%i", &testcase );

    int char_ke;
    char f, s;

    while( testcase-- ){
        cin >> f >> s >> char_ke;

        if( fibo_chara( char_ke ) ){
            printf("%c\n", f );
        } else {
            printf("%c\n", s );
        }
    }


This code just works but too slow
when I changed it to scanf like the one below


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

    int testcase;
    scanf("%i", &testcase );

    int char_ke;
    char f, s;

    while( testcase-- ){
        scanf("%c %c %i", &f, &s, &char_ke );

        if( fibo_chara( char_ke ) ){
            printf("%c\n", f );
        } else {
            printf("%c\n", s );
        }
    }


So is my syntax wrong
I don't think that's it
so can someone please point out what's the problem
How the input looks like?
if it is something like ab123, you need to change your format string to "%c%c%i"
Last edited on
closed account (zb0S216C)
rmxhaha wrote:
"so can someone please point out what's the problem"

You need to tell us what the problem is.

Wazzak
hmm, the input is smt like
1
2
3
2
a b 3000
c d 3000


and the output suppose to be
1
2
a
d


The program output correcly if I use cin as an input if scanf then the output is outrageous smt like
a 2147483647a

scanf returns a value, so you should make use of this to see how well it worked. This can help avoid uninitialised data when the scanf doesn't work for some reason.

The same applies for output to a file - I tend to sprintf, see how well that went, then use fprintf to write the string to the file.

HTH
You forgot the space before the first %c. To make it similar to cin >> f >> s >> char_ke;, it's supposed to be
scanf(" %c %c %i", &f, &s, &char_ke )
Did you read the documentation on scanf? Make sure to understand exactly what each character, especially space, does in the format string.
Last edited on
Topic archived. No new replies allowed.