0

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");
        }
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Should this question be tagged `c++`? – Fiddling Bits May 27 '20 at 18:49
  • Maybe you need a space: `"%c %d"`. – Fiddling Bits May 27 '20 at 18:49
  • It doesn't work – Al amin Kawsar May 27 '20 at 18:51
  • What does `scanf` [*return*](https://en.cppreference.com/w/c/io/fscanf#Return_value)? And have you tried to catch the crash in a debugger to see where in your code it really happens? Because the code you currently show is perfectly fine by itself. Perhaps the problem is related to something else? Without a [mcve] it's really impossible to say anything more. – Some programmer dude May 27 '20 at 18:51
  • scanf() is working perfectly all right... Can you show us the full code and share the error too – San May 27 '20 at 18:52
  • Also please read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). "It doesn't work" is not a suitable description. Please [edit] your question to improve it. – Some programmer dude May 27 '20 at 18:52
  • @FiddlingBits is your space in the wrong place? `%d` filters leading whitespace but `%c` does not, so I would use `" %c%d"` with a space before `%c`. – Weather Vane May 27 '20 at 18:59
  • @WeatherVane Are you asking rhetorically? I use `printf` so little, I'm not sure. – Fiddling Bits May 27 '20 at 19:00
  • 1
    @FiddlingBits not rhetorical. There is never a *need* for a space before `%d` or `%s` or `%f` but there *can be* a need for one before `%c` or `%[]`, because those do not automatically filter leading whitespace. – Weather Vane May 27 '20 at 19:04
  • 1
    @WeatherVane thanks a lot..It works perfectly .. I'v used " %c%d" – Al amin Kawsar May 27 '20 at 19:06
  • @AlaminKawsar you also **must** check the return value from `scanf` functions – the number of items converted. And please don't correct code posted here: correct the code on your own machine, and leave this question so that comments make sense to others who read it. – Weather Vane May 27 '20 at 19:07
  • @WeatherVane Ok..It shows 2 iteam. Thank you so much. – Al amin Kawsar May 27 '20 at 19:09
  • Required reading: [Disadvantages of scanf](https://stackoverflow.com/questions/2430303/disadvantages-of-scanf) – Thomas Matthews May 27 '20 at 19:30
  • Since you tagged as C++, you should be using the safer `cin` and `cout`. Also, use `std::string`. – Thomas Matthews May 27 '20 at 19:32

1 Answers1

0

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;
}