Thread: scanf behaving abnormally

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    21

    Question scanf behaving abnormally

    Hello,
    I am writing a program in which user enters number of coordinates and then coordinate values, and then it enters number of queries and then queries, here is the partial code..
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main()
    {
        int i,j,Q;
        scanf("%d",&Q);
    
        int queries[Q][2];
        char qtype[Q];
    
        for(i=0; i<Q; i++)
        {
            //this portion is troubling...... 
            scanf("%c %d %d",&qtype[i],&queries[i][0],&queries[i][1]);
        }
    
        printf("\n");
        for(i=0; i<Q; i++)
        {
            printf("Query is %c %d %d\n",qtype[i],queries[i][0],queries[1]);
        }
    
        return 0;
    }
    Now suppose i take Q as 2 then it only asks me once instead of asking twice...

    I want inputs to be
    2 //value of Q
    M 2 4
    S 4 5
    But it's not working as expected.. i can't figure out how to fix it...

    **Thnx!!**

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Try the below code; the space before the %c should skip the newline.

    Tim S.

    Code:
    scanf(" %c %d %d",&qtype[i],&queries[i][0],&queries[i][1]);
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    21
    Thnx! it solved my problem...
    Last edited by greendragons; 06-13-2012 at 04:24 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. waitpid behaving strange...
    By mattholm in forum C Programming
    Replies: 6
    Last Post: 11-28-2011, 01:12 PM
  2. fprintf behaving strangely
    By cwmccart in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2008, 09:56 AM
  3. substr not behaving how I expect
    By jEssYcAt in forum C++ Programming
    Replies: 4
    Last Post: 08-13-2007, 10:42 AM
  4. Client abnormally terminates when server isn't found
    By Brain Cell in forum Networking/Device Communication
    Replies: 10
    Last Post: 03-16-2005, 04:29 AM
  5. while loop not behaving properly.
    By Dreamerv3 in forum C++ Programming
    Replies: 20
    Last Post: 01-08-2002, 05:51 PM

Tags for this Thread