LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Program : Not getting the desired output (https://www.linuxquestions.org/questions/linux-newbie-8/program-not-getting-the-desired-output-683351/)

thelink123 11-14-2008 06:27 AM

Program : Not getting the desired output
 
Hi

For the following code i am getting double the actual result. For example if i enter 2 characters i get 4 as the output.

#include <stdio.h>

int main ()
{
double nc = 0; /* count of entered characters */

while (getchar()!=EOF)
{
nc++;
}
printf ("The number of entered characters = %.0f",nc);
return 0;
}

Note: This is how i enter the input

when i execute the output file, because of getchar() it waits for input. I enter some character and press enter. Continues this until i press Ctrl-D (EOF in Unix).

thanks,
thelink

irishbitte 11-14-2008 06:42 AM

Just out of curiousity, what happens, exactly, when you enter 3 characters?

ritam_bkp 11-14-2008 07:17 AM

Quote:

Originally Posted by thelink123 (Post 3341694)
Hi

printf ("The number of entered characters = %.0f",nc);

i thinsk this would work

printf("The number of entered characters = %lf",nc);

as datatype of nc is double
it should not be called with float syntax(%f)
instead use %lf

estabroo 11-14-2008 09:46 AM

I think you are typing incorrectly, perhaps hitting return or some such. Running your program typing ab followed by enough ctrl-d to get eof to work gives me 2 as the output.

thelink123 11-17-2008 01:59 AM

Its Working
 
It is working now...The problem was with the incorrect input entry.
Thanks estabroo


All times are GMT -5. The time now is 05:07 AM.