Like Tree1Likes
  • 1 Post By stahta01

scanf behaving abnormally

This is a discussion on scanf behaving abnormally within the C Programming forums, part of the General Programming Boards category; Hello, I am writing a program in which user enters number of coordinates and then coordinate values, and then it ...

  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
    2,507
    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]);
    greendragons likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  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, 12: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, 03:29 AM
  5. while loop not behaving properly.
    By Dreamerv3 in forum C++ Programming
    Replies: 20
    Last Post: 01-08-2002, 04:51 PM

Tags for this Thread


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