if i input 9 chars instead of 8
it returns 1
although it returns 0
how to check for that additional char??
is this the correct way?
Code:int i; i=getchar(); if (i!=0) { return 0; }
if i input 9 chars instead of 8
it returns 1
although it returns 0
how to check for that additional char??
is this the correct way?
Code:int i; i=getchar(); if (i!=0) { return 0; }
Where do you input these chars?
Who is "it" that returns 1?
what do you call additional character?
You ask your questions as if everybody have read your previous multipost discussion in whole... You are wrong on this assumption.
your way is incorrect in any way - because if no chars are left getchar will wait till you enter more.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
There is no standard IO function that allows you to check if the input buffer has a character available or not.
In a Linux OS, I guess you COULD use select() with the filenumber of stdin as argument [but it will stlll not tell you the complete truth, as stdin is buffered, so it may have buffered some of the data and have data ready to read].
Generally, you have to rely on KNOWING what went on before the getchar() to know if you have more characters to read or not.
--
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.
when i enter
1 2 3 4 5 6 7 8 9
it should return 0
it doesnt further after 1 2 3 4 5 6 7 8
if the size is 8
then we need to have only 8 numbers in a row
9 numbers in a row is an error
i tried this
is it correct?
Code:int i; i=getchar(); if (i!=0) { return 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-1)) { return 0; } if (ch<0) { return 0; } if (flag==0) { return 0; } else { } rt=read_array(input,i+1,size); return rt; }
So, you return 1 when you have collected size numbers, whether or not you have found the newline at the end of the line.
Again, you need to figure out what the correct order of events is, and how that corresponds to your specifications.
--
Mats
Last edited by matsp; 01-29-2009 at 10:25 AM.
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.
read the whole line and then use strtol to convert each number from the string, thus you know exactly if something left to convert
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Except if you had read t' other thread, you'd know that no other input functions than scanf(), gets() and getchar() are allowed, and no functions to convert strings.
There is a relatively simple answer to the problem, but I'd like transgalactic2 to figure out the answer, rather than me spoon feeding it.
--
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.
i tried to put it in a base case
it returns 0 for nine numbers
but for the legal input of
1 2 3 4 5 6 7 8
it stops after two enters and return 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) { ch=getchar(); if (ch!=0) { return 0; } return 1; } flag=scanf("%d",&input[i]); ch=getchar(); if ((ch=='\n')&&(i!=size-1)) { return 0; } if (ch<0) { return 0; } if (flag==0) { return 0; } else { } rt=read_array(input,i+1,size); return rt; }
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
did i get it close??
i can use only getchar scanf gets
how to catch this extra char if it exists
return 0
if not return 1
??
and i got another problem that if my input ends with a space
then it doesnt stops on enter
but goes to a new line
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-1)) { return 0; } if (ch<0) { return 0; } if (flag==0) { return 0; } else { } rt=read_array(input,i+1,size); return rt; }
Last edited by transgalactic2; 01-29-2009 at 11:55 AM.
That's because you are doing the same thing as this
If you enter "3 45 6SPACE" and then press enter, getchar catches the space and not the newline. But you cannot trap a space, because the spaces are necessary.Code:#include <stdio.h> int main() { int ray[4], i=0; printf("Enter: "); while (i<4) { scanf("%d",&ray[i]); i++; if (i<4) if (getchar()=='\n') puts("Not enough numbers yet!"); } for (i=0; i<4; i++) printf("%d\n",ray[i]); return 0; }
What you want, as I tried to tell you yesterday or the day before, is not possible this way. You must write a function using getchar to process the input one character at a time.
If you keep trying to do it your way anyway, you will never run out of little problems like this.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
so you say that
i need to save it as a string
and then transform it into an array of integer
but its a very difficult thing to transform
first because
if i read it from left to right
and i will start "-2345" string
how could i know to multiply 2 by 10^3 +3*10^2+4*10^1 +5*10^1
from the starting point i cant know when it ends
and i got this "-" or "+" char
and doing that recursively its impossible
Last edited by transgalactic2; 01-29-2009 at 12:44 PM.