are you saying thet the '\n' is from when i pressed enter after writing the actual name of the program (such as ./program) ??
|
I don't think so, I guess it's the one you entered to trigger the scanf.
|
No
ok this has to do with the keyboard being buffered. when getchar is called it waits for an enter to confirm the selection but that enter generates another character in the buffer "\n" ok now if you have more then one getchar or you are implementing this in a loop then that \n will be used as the next input method instead of the correct one. You have 2 solutions to this either create a input method that doesn't suffer from this deficiency or flush the buffers after every input. |
'\n'
okay, i get what you mean, but i dont see why getchar would do this. this getchar function gets a single character. this will naturally have to be followed by <enter> to confirm what was typed. surely getchar takes this into account?
secondly: i didnt use getchar in the program, i used scanf("%c"); any ideas? |
yes but it asks me to enter the second one before ive even entered the first. there can be no enter in the buffer other than that which i used to start the program ('./program'). if it was buffereing an '\n' than it would be the second, not the first getchar that would be skipped, would it not?
(the above is out of curiosity as, im not using getchar, im using scanf("%c"); but i guess this wouldnt change things. |
Post your full source please and then we can see whats wrong.
|
If you're using scanf("%c",&somechar);, it will read ONE character in from the buffer. Perhaps you entered a number to tell it the number of numbers, as your pseudocode suggested? For example (input in bold):
Code:
Number of numbers: 5 (NEWLINE) |
Quote:
Quote:
You can have yours reading characters without needing \n by turning the interface in "raw" mode, like "vi" do for example. Wrap your program with a shell script like this: Code:
#!/bin/sh |
code:
sorry for the long delay. as i havnt ever got feedback on code, as well as an answer as to what the fault is, i wouldnt mind your opinions on wether this code would be considered neat/clean/efficient etc.#include <stdio.h>
main() { int a, b, c; char *d; printf("enter amount of values: "); scanf("%d", &a); d = (char*) calloc(a, sizeof(char)); if (d == NULL) printf("cannot allocate memory.\n"); else { for (b=0;b<=a-1;b++) { printf("please enter char %d: ", b+1); scanf ("%c", d+b); } for (b=0;b<=a-1;b++) { if ( *(d+b) == ' ') *(d+b) = '_'; } printf( "you entered: "); for (c=0;c<=a-1;c++) printf("%c ", *(d+c)); printf("\n"); } } |
All times are GMT -5. The time now is 12:10 AM. |