I think you are simply having a case of 'fence-post-error' aka "off-by-one" error (look either of that up in google).
--
Mats
I think you are simply having a case of 'fence-post-error' aka "off-by-one" error (look either of that up in google).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
you said that its not on the right order
why??
where is the mistake
why its giving me 0 on a legal input??
Have you tried using a debugger?Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i tried debugger
it stops on the getchar line
it want me to press a number and enter
but when i run it normally i inputs a several numbers in a row
so debugger cant help
??
So input several numbers in a row.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i notices that it doesnt goto the i==size "if"
why?
the ch variable during the debbuging proccess gives number like 49
instead of numbers that i entered
Last edited by transgalactic2; 01-28-2009 at 12:21 PM.
The problem is that you use getchar() before using scanf(). This means that if there is a single digit number in the input buffer, it would mysteriously "disappear" because it would be read and discarded by the getchar(). A solution is to use scanf() and then use getchar().Originally Posted by transgalactic2
That's fine. Remember that in ASCII the value of '1' is 49.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i did like you said
when i input
1 2 3 4 5 6 7 8 enter
it returns 0
??
Code:#include <stdio.h> int read_array(int input[],int i,int size); int main() { int i; int input[40]; printf("%d\n",read_array(input,0,8)); for(i=0;i<8;i++) { printf("%d ",input[i]); } printf("\n"); return 0; } int read_array(int input[],int i,int size) { int flag,rt,ch; if (i==size) { return 1; } flag=scanf("%d",&input[i]); ch=getchar(); if ((ch=='\n')&&(i!=size)) { return 0; } if (flag==0) { return 0; } else { } rt=read_array(input,i+1,size); return rt; }
Suppose when you use scanf(), there is nothing to read. What happens? Suppose when you use getchar(), there is nothing to read. What happens?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
in this broken code
nothing happens
it doesnt stop the proccess
it goes to a new line
till it get a real input
where is my mistake
i flipped the getchar with the scanf in places
Code:#include <stdio.h> int read_array(int input[],int i,int size); int main() { int i; int input[40]; printf("%d\n",read_array(input,0,8)); for(i=0;i<8;i++) { printf("%d ",input[i]); } printf("\n"); return 0; } int read_array(int input[],int i,int size) { int flag,rt,ch; if (i==size) { return 1; } flag=scanf("%d",&input[i]); ch=getchar(); if ((ch=='\n')&&(i!=size)) { return 0; } if (flag==0) { return 0; } else { } rt=read_array(input,i+1,size); return rt; }
That's only part of the fix. As I mentioned, you need to handle EOF correctly.Originally Posted by transgalactic2
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
in the while loop that you showed me you handled EOF by ch!=EOF or
ch>=0
i need to use this term here to?
you mean that
??
Last edited by transgalactic2; 01-28-2009 at 01:42 PM.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.