whats "one off"
i searched in google there is no definition
Printable View
whats "one off"
i searched in google there is no definition
Obviously need to practice your google skills - http://en.wikipedia.org/wiki/Off-by-one_error
--
Mats
i debugged it again
and when i=7 (the last char before enter)
i goes to
so i guessed i[7] gets the \n charCode:if ((ch=='\n')&&(i!=size))
{
return 0;
}
so i changed i==size-1
now it returns 1 but
it doesnt put the 8th number in the array
??
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-1)
{
return 1;
}
flag=scanf("%d",&input[i]);
ch=getchar();
if ((ch=='\n')&&(i!=size))
{
return 0;
}
if (ch<0)
{
return 0;
}
if (flag==0)
{
return 0;
}
else
{
}
rt=read_array(input,i+1,size);
return rt;
}
So what do you think you should do to fix that?
--
Mats
i should some how catch this eights char using getchar
and put it into the array
but where to do that?
Think about what happens in your code, and where you get to for each possible scenario of newlines, count of i, and numbers entered. Where should you exit the recursion? What are the conditions?
Coding is about solving problems - you _NEED_ to learn to think about what happens in your code, not just repeatedly post "and how do I do that?"
--
Mats
i get \n only onces i the end of the array
i dont know why it happens
thats how it should be
a row of numbers and enter
what is the problem?
one new line means one enter
i solved it thanks :)
how to check if there are more member then the size??
If you count to MORE than size and haven't received a newline yet, is that a good enough clue?
--
Mats
nothing stops me from counting further
so its not much an obsticle