#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    24
    Rep Power
    0

    Problem with scanf()??


    Hello to all,
    I have a n issue with a code and i cannot find what is the wrong?
    I suppose that maybe is at 19 line (scanf) cause i ask 4 numbers and i can give 5??
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int C[2][2];
    int B[20];
    int INDICES[4];
    typedef struct STOIXIO{
    						int TIMI;
    						int GRAMMI;
    						}P[4];
    int n,m;
    
    int insert_value()
    {
    	printf("Give 4 numbers with increased values\n");
    int	A[2][2];
    	for(n=0;n<2;n++){
    		for(m=0;m<2;m++){
    	scanf("%4d\n",&A[n][m]);}}
    	printf("SCANF VALUES %d %d %d %d\n",A[0][0],A[0][1],A[1][0],A[1][1]);	
    		for(n=0;n<2;n++)
    			for(m=0;m<2;m++)
    				while(A[n][m]>A[n][m+1])
    				{
    					printf("PUT WRONG VALUES %d %d %d %d\n",A[0][0],A[0][1],A[1][0],A[1][1]);
    					insert_value();
    				}
    				A[2][2]=C[2][2];
    					return 0;
    }
    
    main()
    {
    insert_value();		
    printf(" %d %d %d %d\n",C[0][0],C[0][1],C[1][0],C[1][1]);
    }
    Thanks!! :confused:
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,222
    Rep Power
    2222
    Originally Posted by Giorgos
    I suppose that maybe is at 19 line (scanf) cause i ask 4 numbers and i can give 5??
    I don't understand your question. You can enter any number of values, more than 5 even, but if you're only going to be calling scanf so that it will read in four, then it will only read the first four and leave the rest in the input buffer.

    Also, what is this line about?
    A[2][2]=C[2][2];
    Those array elements do not exist, so you're clobbering the memory location after the A array with whatever garbage comes after the C array.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    24
    Rep Power
    0
    Sorry, about the A[2][2]=C[2][2] forget it.I made some tests and i left it :( .
    Please try to run this code and give 1 2 3 4 numbers.You will understand what i mean.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,222
    Rep Power
    2222
    No, you describe to us what happens. For one thing, I have no idea what language you're using. It's certainly not C, because C won't let you declare a variable after executable code.

    So then, tell us exactly what you did to create this "problem". Exactly what inputs you gave it and exactly how (eg, all on one line or on separate lines). Exactly what outputs you got, and exactly how that differs from what you expected.

    Comments on this post

    • bdb disagrees : C allows intermixing declarations and code since 1999.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2012
    Posts
    24
    Rep Power
    0
    Ι give it 1 2 3 4 and wait for another one so i give it 5. Αccordance with line 23 it have to go to line 35 but instead this it goes to 26.
    That is the problem.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Your format string for scanf ("%4d\n") means:

    Part1 -- "%4d": ignore any amount of whitespace and read up to 4 characters that may be part of an integer value. Put that value in the corresponding address.
    Part2 -- "\n": ignore any amount of whitespace

    So with the input "1 2 3 4<ENTER>" the scanf() in the loop, first doesn't ignore any whitespace and directly converts the "1"; then it ignores the whitespace ending when it sees the 2
    Next time through the loop, scanf doesn't ignore whitespace because there is no whitespace, then converts the "2"; after that it ignores whitespace
    ...
    After reading the "4" it ignores whitespace (the <ENTER> you provided). After ignoring the <ENTER>, scanf() is still in "ignoring whitespace" mode: you can type as many ENTERs, TABs, SPACEs you want, scanf will remain in "ignoring whitespace" mode. When scanf sees a 5 (or a P or a * or anything that is not whitespace it terminates the "ignore whitespace" mode) and goes to the next instruction.

    So ... do not make scanf ignore whitespace after reading a number.

IMN logo majestic logo threadwatch logo seochat tools logo