简体   繁体   中英

scanf for both character and integer

I am trying to take input character and integer both. But when i use cin>>ch>>val; for taking input,it works.But Using scanf("%c%d",&ch,&val); ,it shows me run time error.What can i do to get rid of this problem? I want to use scanf for faster input. Here is my partial code:

 #include<bits/stdc++.h>
 using namespace std;
 int main()
 {
    int q;
    scanf("%d",&q);
    while(q--)
    {
        char ch;
        int val,in;
        //cin>>ch>>val;
        scanf("%c %d",&ch,&val);
        in=val;
        if(ch=='a'){
            //scanf("%d",&val);
            //update(1,0,m,++indx,val);
            printf("First Case\n");
        }else{
            //si(in);
            //if(in>tree[1]) printf("none\n");
            //else query(1,0,m,in);
            printf("Second Case\n");
        }
    }
}

I ran this code. It works fine for me. So probably you're using scanf in C++ and forgot to include or maybe it's something else with your code. Can you share the full code. There is no issue with scanf() function as you have mentioned, You can take multiple inputs like scanf(%d%d%c%c,&a,&b,&c;&d);

#include <stdio.h>
int main()
{
    printf("Hello World");
    int q=1;
    while(q--)
    {
        char ch;
        int val,in;
        scanf("%c%d",&ch,&val);
        in=val;
        if(ch=='a'){
            printf("%d",val);
        }else{
            printf("%d",val);
        }
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM