does it clean the input stack??
or i need to do
i=getchar();
i cant use fflush();
i need to press twice enter with i=getchar();
do i have to?
does it clean the input stack??
or i need to do
i=getchar();
i cant use fflush();
i need to press twice enter with i=getchar();
do i have to?
I'm not sure what you mean by in a 'row'. Since you said string could you solve it like this?
Code:#include <stdio.h> int main(void) { int index; char input[40]; int length=5; printf("enter string\n"); scanf("%s",input); printf("\n"); input[length]='\0'; printf("%s",input); }
> does it clean the input stack??
It's not a stack. More of a queue of characters (ie a stream).
No... very bad. http://apps.sourceforge.net/mediawik...tle=Scanf_woes
fgets() is the "file" version of gets(). It is also a safer version of gets(), so if you can use gets(), you should be allowed to use fgets() - after all, both functions do the same thing [except for the well-known flaw that gets() makes absolutely no checks against the user's input being longer than the receiving buffer - and thus causing the program to crash].
But if your teacher IS AWARE of this [or at least you are], and you will be told off for using fgets() when gets() will do the job, I would say "go ahead and use gets()" - but be aware that you are lighting your barbecue with petrol, and you do have a good, safe barbeque lighting fluid standing right next to the petrol can.
--
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.
how to clean the input queue and end the input of the string without ffulsh and with 1 enter
only?
can i use only
after i press enter does it cleans the input queue?Code:printf("enter string\n"); gets (input);
No, if you are not allowed to use fgets(), then even more so you are not allowed to use gets(). If you are allowed to use gets() and yet not allowed to use fgets(), then your teacher is an incompetent C programmer with no understanding of security issues, and you may want to consider quitting the class or end up an incompetent C programmer yourself.Originally Posted by transgalactic2
That is just as bad as gets(). With scanf() for a string the length must be given with the format specifier.Originally Posted by lvl99
Use a loop to getchar() until you reach EOF. In fact, it would be wrong to use fflush(), even if you were allowed to, since the behaviour of fflush() is undefined for input streams.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 to do that getchar loop.
its not working
did you mean this?
Code:printf("enter string\n"); gets (input); for(i=0;input[i]!=(int)"\0";i++){ k=getchar(); }
According to the FAQ, it should be:
Are you sure you must use gets()? Can you use scanf()?Code:int ch; while ((ch = getchar()) != '\n' && ch != EOF);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
i can use scanf
but i cant have the length being inputted by the user
he can enter one coordinates and he can enter 5
i know that the maximum string allowed is 40 chars long
Last edited by transgalactic2; 12-30-2008 at 02:22 AM.
In that case use:Originally Posted by transgalactic2
If you need to "flush" the input buffer you can still use the code from the FAQ answer after the scanf(). The purpose of the 39 is to ensure that a maximum of 39 characters can be read (the last character is reserved for the null character, if you recall).Code:scanf("%39s", input);
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
with that code
i have to press twice enter
what to do so i will need to input only one enter?
Code:int main() { int index; char input[40]; char input2[40]; int i,k; int length=5; printf("enter string\n"); gets (input); int ch; while ((ch = getchar()) != '\n' && ch != EOF); }
That should not be the case, and the code that you gave is not the one that I gave you.Originally Posted by transgalactic2
There is a catch though: %s matches a whitespace delimited string, but I believe you want to match whitespace as well. I think that the simplest and safest solution, barring the use of fgets(), would be:
Code:#include <stdio.h> int main() { char input[40]; char ch; int i; printf("Enter a string: "); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0'; printf("You entered: '%s'\n", input); return 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
so in this code.
i use scanf or gets?
Code:#include <stdio.h> int main() { char input[40]; char ch; int i; printf("Enter a string: "); for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i) { input[i] = ch; } input[i] = '\0'; printf("You entered: '%s'\n", input); return 0; }