LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-14-2008, 06:27 AM   #1
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Rep: Reputation: 15
Smile 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
 
Old 11-14-2008, 06:42 AM   #2
irishbitte
Senior Member
 
Registered: Oct 2007
Location: Brighton, UK
Distribution: Ubuntu Hardy, Ubuntu Jaunty, Eeebuntu, Debian, SME-Server
Posts: 1,213
Blog Entries: 1

Rep: Reputation: 88
Just out of curiousity, what happens, exactly, when you enter 3 characters?
 
Old 11-14-2008, 07:17 AM   #3
ritam_bkp
Member
 
Registered: Sep 2008
Location: Calcutta,India
Distribution: Fedora 11, OpenSUSE 11.0
Posts: 116

Rep: Reputation: 17
Smile

Quote:
Originally Posted by thelink123 View Post
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
 
Old 11-14-2008, 09:46 AM   #4
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,126
Blog Entries: 2

Rep: Reputation: 124Reputation: 124
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.
 
Old 11-17-2008, 01:59 AM   #5
thelink123
Member
 
Registered: Aug 2008
Location: India, Kerala
Distribution: openSUSE 11
Posts: 46

Original Poster
Rep: Reputation: 15
Smile Its Working

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass the output of one program to another program without backticks? BrianK Linux - General 4 11-11-2008 06:27 PM
how to get the desired output from the following code: pdklinux79 Linux - Newbie 1 06-10-2008 01:51 PM
How to find package name for desired program or command? ozegoods Linux - Software 1 04-17-2007 02:16 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
getting output from a program executed from c++ dfuse Programming 6 08-22-2003 02:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration