Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Program not giving any output

Reply
Thread Tools

Program not giving any output

 
 
ImpalerCore
Guest
Posts: n/a
 
      11-09-2010
On Nov 9, 5:25*pm, Ian Collins <ian-n...@hotmail.com> wrote:
> On 11/10/10 11:09 AM, ImpalerCore wrote:
>
>
>
> > The type of 'linestart' in the statement 'linestart = (datum == '\n')'
> > is of type 'int', and my personal preference would be to make the
> > integer assignment explicit. *The main reason is that there isn't a
> > 1-1 match between boolean expressions and their integer counterparts,
> > since any integer != 0 is interpreted as true. *I don't have a problem
> > with people who use it, since I can read it just fine, but I still
> > prefer to avoid implicit boolean to int conversions in code that I
> > write.

>
> In this example, "linestart" was being used a s a boolean value. *Its
> value was only tested with if (linestart).


Good point. I didn't look closely enough at his example. In
retrospect I would go with KT's version then. I suppose I'm so used
to using 'bool' to declare boolean types that I autoregister 'int' to
conceptually represent integer numbers.

Best regards,
John D.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-09-2010
Lew Pitcher <> writes:

> On November 7, 2010 14:00, in comp.lang.c, wrote:
>
>> On Nov 7, 6:32Â*am, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:
>>
>>> Side-note, this program is considerably more complicated (and
>>> self-limited) than it needs to be to accomplish its stated task.
>>> As you learn more, you'll see ways to simplify and improve it --
>>> but right now, let's focus on the basics.
>>>

>>
>> I don't think this is what the op was looking for, but like, here is
>> what I came up with...

>
> Ditto....
>
> ----------------------------cut------------------------------
>
> /*
> ** capit.c - capitalize first letter of each input line
> **
> ** Input: stdin
> ** Process: capitalize the first alphabetic character of
> ** each input line.
> **
> ** If ALLOWLEADINGBLANKS is defined, only capitalize
> ** if letter is preceeded by zero or more whitespace;
> ** *DO NOT* capitalize if first letter is preceeded
> ** by non-whitespace.
> ** Output: stdout
> */
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
>
> #define ALLOWLEADINGBLANKS 1
>
> void capit(FILE *input, FILE *output)
> {
> int datum, linestart;
>
> for (linestart = 1,datum = getc(input);datum != EOF;datum = getc(input))
> {
> if (linestart)
> {
> #ifdef ALLOWLEADINGBLANKS
> for (;isblank(datum); datum = getc(input)) putc(datum,output);
> #endif
> if (islower(datum)) datum = toupper(datum);
> }
> putc(datum,output);
> linestart = (datum == '\n' ? 1 : 0);
> }
> }


The ALLOWLEADINGBLANKS version will go wrong when the input ends after
an initial blank. It's possible that such an input (as a text file) is
considered to trigger undefined behaviour, but prefer to handle them
since they are no rare "in the wild".

I would not normally comment on style but, since I'm posting, I'll
mention that I find

for (linestart = 1,datum = getc(input);datum != EOF;datum = getc(input))

a little odd. For one thing, linestart has nothing to do with the loop,
so I's initialise it to 1 in the declaration rather than here but, more
significantly,

while ((datum = getc(input)) != EOF)

is such a widely-know idiom, it took me while to grok yours as being the
same loop!

> int main(void)
> {
> capit(stdin,stdout);
> return EXIT_SUCCESS;
> }

<snip>
--
Ben.
 
Reply With Quote
 
 
 
 
Maxx
Guest
Posts: n/a
 
      11-10-2010
On Nov 9, 11:41*am, Keith Thompson <ks...@mib.org> wrote:
> Maxx <grungeddd.m...@gmail.com> writes:
> > On Nov 7, 5:57*am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> >> Maxx <grungeddd.m...@gmail.com> writes:
> >> > Here i have a program which was supposed to do the following:

>
> >> > Example INPUT:::
> >> > Dolorian refused to release the
> >> > lyrics for the track 'A Part Of Darkness'
> >> > on 'When All The Laughter Has Gone',
> >> > and several parts of the lyrics on almost

>
> >> > Example OUTPUT:::::
> >> > Dolorian refused to release the
> >> > Lyrics for the track 'A Part Of Darkness'
> >> > On 'When All The Laughter Has Gone',
> >> > And several parts of the lyrics on almost

>
> >> > What this program does is basically checks the first letter of every
> >> > line and changes it to upper case.

>
> >> > Here is the program:::::

>
> >> > #include <stdio.h>
> >> > #include <stdlib.h>
> >> > #include <ctype.h>
> >> > #define BUFFER * * 5000

>
> >> There is no obvious need to store a large number of anything in this
> >> program. *It's a good exercise to re-think it so that the data you need
> >> to store is unrelated to how long the input or input lines might be.

>
> [snip]
>
> >> > * *if((fp=fopen(argv[1],"a")) == NULL)

>
> >> What do you think "a" does here? *If you are not sure, look it up. *I
> >> could tell you, but you'll need to look up other stuff tomorrow, so find
> >> some book or on-line reference now and be prepared.

>
> [snip]
>
> > I was actually hoping for it to work against quite large files, so i
> > set the BUFFER to 5000.

>
> What is the significance of the number 5000? *A "large" file is going
> to be *much* larger than that (megabytes or more).
>
> The point is that, regardless of the size of the input file, you don't
> need to store more than a few bytes at a time -- probably not more than
> one.
>
> > Here i'm trying to append some characters to a file, so i used file
> > mode "a".Yeah i've foolishly omitted the error check, for argc=0.

>
> Why are you appending to a file? *Your problem description talks about
> input and output.
>
> [...]
>
> > An i was confused with that c. I thought as it was an integer so the
> > compiler would throw me an error if i tried to compare it against a
> > character(a error that i seldom encountered in Java). Thats why i 've
> > used the ascii values for carriage-return and line-feed to compare it
> > against the int. Thanks for your help, actually i've learnt a lot from
> > it.

>
> "char" is an integer type in C.
>
> When you read from a file in text mode, each end-of-line appears
> as a '\n' character. *Don't make any assumptions about the integer
> value of '\n'.
>
> --
> Keith Thompson (The_Other_Keith) ks...@mib.org *<http://www.ghoti.net/~kst>
> Nokia
> "We must do something. *This is something. *Therefore, we must do this."
> * * -- Antony Jay and Jonathan Lynn, "Yes Minister"



I get it, i should read a few bytes at a time rather than set aside a
buffer of random size. And the integer confusion is also
resolved...thanks
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-10-2010
Maxx <> writes:
> On Nov 9, 11:41Â*am, Keith Thompson <ks...@mib.org> wrote:
>> Maxx <grungeddd.m...@gmail.com> writes:

[...]
>> > I was actually hoping for it to work against quite large files, so i
>> > set the BUFFER to 5000.

>>
>> What is the significance of the number 5000? Â*A "large" file is going
>> to be *much* larger than that (megabytes or more).
>>
>> The point is that, regardless of the size of the input file, you don't
>> need to store more than a few bytes at a time -- probably not more than
>> one.

[...]
> I get it, i should read a few bytes at a time rather than set aside a
> buffer of random size. And the integer confusion is also
> resolved...thanks


Why "a few"? (Yes, I know I wrote "a few bytes at a time" myself;
I was being sloppy.)

The most obvious solution only reads one byte at a time.

And please trim quoted text when posting a followup. There's no need
to quote (and make your readers wade through) material that you're
not commenting on. I particular, don't quote signatures (the stuff
following the "-- " line) unless you're actually commenting on them.
Just be sure to keep the attribution lines ("So-and-so wrote:")
for any text you quote.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Infix to Postfix program not giving correct output Maxx C Programming 5 02-07-2011 12:59 PM
The giving that keeps on giving sixteenmillion C Programming 0 11-19-2007 10:59 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
why it is not giving any error? prasi C Programming 3 10-03-2005 11:42 PM
why it is not giving any error? prasi C Programming 12 10-03-2005 08:59 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57