LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cant divide with C (https://www.linuxquestions.org/questions/programming-9/cant-divide-with-c-350676/)

skoot 08-22-2005 03:38 PM

are you saying thet the '\n' is from when i pressed enter after writing the actual name of the program (such as ./program) ??

jlliagre 08-22-2005 04:02 PM

I don't think so, I guess it's the one you entered to trigger the scanf.

exvor 08-22-2005 04:09 PM

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.

skoot 08-22-2005 04:16 PM

'\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?

skoot 08-22-2005 04:38 PM

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.

exvor 08-22-2005 04:55 PM

Post your full source please and then we can see whats wrong.

Matir 08-22-2005 05:02 PM

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)
Reads 5 in, and leaves the newline in the buffer.

jlliagre 08-22-2005 09:47 PM

Quote:

if it was buffereing an '\n' than it would be the second, not the first getchar that would be skipped, would it not?
The first \n, the one following the command is gotten by the shell, not your program.
Quote:

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?
This is wrong, enter is not mandatory to confirm something was typed, at least from a program point of view.
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
stty raw
./program
stty -raw

This also can be done in plain C, but the former is simpler, at least to test the "uncooked" mode.

skoot 08-26-2005 06:50 PM

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 01:45 PM.