when asking the user for input is there any way that you can enable the user to go back and alter there input before they press enter
when asking the user for input is there any way that you can enable the user to go back and alter there input before they press enter
yes. Using getchar() and gets() make you press enter before it sends the information, and will allow you to go back and change what you type.
do you mean going back with the arrow keys or erasing everythign?
¿Red Baron?
"Imagination is more important than knowledge"
-Albert Einstein (1879-1955)
Check out my games!
[code] /* dont forget code tags! */ [/code]
Best not use gets(), it's not good! It has no bounds checking and you'll end up in trouble. Use fgets() instead..Originally posted by Draco
yes. Using getchar() and gets() <snip>
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
ok, ok....my book doesn't cover fgets(). What's the difference by the way?
>What's the difference by the way?
fgets is more generic, safer, and it makes you feel all warm and fuzzy. Whereas the call to gets looks like this:
/* No size is given, very unsafe */
gets ( array );
fgets looks more like this:
/* A size is given, and you can read from files too! */
fgets ( array, sizeof array, stdin );
Note that gets was a part of the original portable I/O library in the early days of C. It should have been removed with the rest of the trash in the C89 ANSI/ISO standard, but it wasn't for unknown reasons.
-Prelude
My best code is written with the delete key.
hmm.... fgets is just like a file structure. however, that situation isn't using any fgets ...
Why not use
: scanf ("%30[^\n]", &array);
well... the system could control the input if char more that the limit. the next 29 char (not include '\0') will not scan it.
>Why not use
>: scanf ("%30[^\n]", &array);
scanf was designed for formatted input, not line input, and most certainly not user input. So why take advantage of it's features to create a nasty and dangerous hack when there's a function much better suited to this task?
-Prelude
My best code is written with the delete key.
Yup, i guess you are right.
however if using ...
fgets ( array, sizeof array, stdin );
we din't know how long the string of the input could be?
or we can try to give a warning msg that the input is too long and tell the user to input again.
>we din't know how long the string of the input could be?
I assume you mean that the input could theoretically be longer than the array can hold? In such a case where you suspect that to happen you can read the size of the buffer (fgets will stop at the delimiting size) and then test to see if there is a newline in the buffer. If there isn't then there is still more input to read.
This can be handled many ways, one of which would be an intermediate buffer which reads from input and appends its contents to a larger buffer until the newline is reached. The larger buffer would almost always be dynamically resized accordingly.
-Prelude
My best code is written with the delete key.
Is using scanf in this way dangerous? I mean will it lead to some buffer overflow or somethin' like that?Originally posted by Prelude
>Why not use
>: scanf ("%30[^\n]", &array);
scanf was designed for formatted input, not line input, and most certainly not user input. So why take advantage of it's features to create a nasty and dangerous hack when there's a function much better suited to this task?
-Prelude
oh ok ... thank guy
nop ... it'll scan until 29 char including the \0 char. so the next 29 char that user inputed will not save in the array variable.
well... it's could control the user not to enter more than 29 char.
but you should set the warning msg to user to give them know there's an error on input data
:[^\n] > will scan until the '\n' char