Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Program not giving any output

 
 
Seebs
Guest
Posts: n/a
 
      11-07-2010
On 2010-11-07, Chad <(E-Mail Removed)> wrote:
> You do realize that this is probably a case that the OP isn't ready
> for yet don't you?


I am not sure I'm happy with the notion of teaching people to do stuff
wrong because they're "not ready" to do it right.

> I hate to break it to you, but there are some
> posters here that don't consider every case.


Yes.

And one of the benefits of this group is that more experienced programmers
can help them fix that.

> This is because there are
> some posters that are rookies when it comes to structured programming.


Best time to learn.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-08-2010
Chad <(E-Mail Removed)> writes:
> On Nov 7, 2:55*pm, Eric Sosman <(E-Mail Removed)> wrote:
>> * * *Not much, but it introduces a new bug. *And it's odd that
>> all the scrupulous error-checking of each fputc() (why not putc()
>> or putchar(), by the way?) is not matched by similar checking for
>> the printf() -- but then, the printf() seems to be extraneous
>> anyhow. *And it's got a useless cast. *And it's ugly.
>>
>> * * *If you haven't spotted the new bug yet, try this input:
>>
>> oh what a tangled web we weave
>> when first we practice to write c!
>>
>> that should be "ceave," but what the heck:
>> it wouldn't pass a spelling check.

>
> You do realize that this is probably a case that the OP isn't ready
> for yet don't you? I hate to break it to you, but there are some
> posters here that don't consider every case. This is because there are
> some posters that are rookies when it comes to structured programming.
> With that, I'm about a half second from entering you into my kill
> file.


Right, some posters don't consider every case.

Apparently you consider it rude to teach them to do so.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <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
 
 
 
 
Chad
Guest
Posts: n/a
 
      11-08-2010
On Nov 7, 3:54*pm, Seebs <(E-Mail Removed)> wrote:
> On 2010-11-07, Chad <(E-Mail Removed)> wrote:
>
> > You do realize that this is probably a case that the OP isn't ready
> > for yet don't you?

>
> I am not sure I'm happy with the notion of teaching people to do stuff
> wrong because they're "not ready" to do it right.
>
> > I hate to break it to you, but there are some
> > posters here that don't consider every case.

>
> Yes.
>
> And one of the benefits of this group is that more experienced programmers
> can help them fix that.
>
> > This is because there are
> > some posters that are rookies when it comes to structured programming.

>
> Best time to learn.
>


Round 3. This code now works not only with the OPs original file, but
also, with Eric's input file...

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(int argc, char **argv)
{
FILE *fp;
int c, prev;

if (argc != 2) {
fprintf(stderr, "Enter a file name.\n");
exit(EXIT_FAILURE);
}

if ((fp = fopen(argv[1], "r")) == NULL) {
fprintf(stderr, "Can't open %s file.\n", argv[1]);
exit(EXIT_FAILURE);
}

c = getc(fp);
printf("%c", toupper(c));

while ( ((c = getc(fp)) != EOF) ) {
prev = c;
printf("%c", c);

while (c == '\n') {
if ((c = getc(fp)) == EOF) {
break;
}
printf("%c", toupper(c));
}
}

if (prev != '\n')
printf("\n");

fclose(fp);
exit(EXIT_SUCCESS);
}
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-08-2010
Chad <(E-Mail Removed)> wrote:
> Round 3. This code now works not only with the OPs original
> file, but also, with Eric's input file...

....
> * c = getc(fp);
> * printf("%c", toupper(c));


But not with an empty file.

#include <ctype.h>
#include <stdio.h>

int main(void)
{
int ch, nl = 1;

while ((ch = getchar()) != EOF)
{
if (nl) { ch = toupper(ch); nl = 0; }
putchar(ch);
if (ch == '\n') nl = 1;
}

return 0;
}

--
Peter
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-08-2010
On 11/7/2010 8:35 PM, Chad wrote:
> On Nov 7, 3:54 pm, Seebs<(E-Mail Removed)> wrote:
>> On 2010-11-07, Chad<(E-Mail Removed)> wrote:
>>
>>> You do realize that this is probably a case that the OP isn't ready
>>> for yet don't you?

>>
>> I am not sure I'm happy with the notion of teaching people to do stuff
>> wrong because they're "not ready" to do it right.
>>
>>> I hate to break it to you, but there are some
>>> posters here that don't consider every case.

>>
>> Yes.
>>
>> And one of the benefits of this group is that more experienced programmers
>> can help them fix that.
>>
>>> This is because there are
>>> some posters that are rookies when it comes to structured programming.

>>
>> Best time to learn.
>>

>
> Round 3. This code now works not only with the OPs original file, but
> also, with Eric's input file...


Round 3 is your best yet, but the bug isn't quite dead: a
feeble feeler still flutters. And look! another bug has crawled
up to keep the first one company! Here are a few tests you
might want to make:

- Try an input file with an empty first line and a lower-
case letter at the start of the second.

- Try an input file with an embedded run of two or more
empty lines, followed by a line that starts with a lower-
case letter.

- Try a completely empty file, without even a lonesome '\n'.

- Try an input line with embedded empty lines, and compare
its line count with that of the program's output.

It seems to me you're sort of trying to subdue a complicated
problem by whacking it with great wads of complicated code. You've
gotten rid of some of the complexity, but there's still too much --
because the problem is *not* complicated, if you think about it the
right way. All you're trying to do is capitalize the first character
of each line and copy all the others, so the only thing you need to
know about each character as you handle it is "Are you the first of
a line?" If you think about what a "line" is, and consider the
conditions that distinguish a first-of-line character from a rest-
of-line character, I'm sure you'll arrive at a much simpler (and more
clearly correct) program.

A few other comments are in-line below.

> #include<stdio.h>
> #include<stdlib.h>
> #include<ctype.h>
>
> int main(int argc, char **argv)
> {
> FILE *fp;
> int c, prev;
>
> if (argc != 2) {
> fprintf(stderr, "Enter a file name.\n");


fputs("Enter...", stderr) would also work, and might well be
FASTER OMIGOD SPEED YES GOTTA HAVE IT!

> exit(EXIT_FAILURE);


Since this is main(), `return EXIT_FAILURE' would also work.

> }
>
> if ((fp = fopen(argv[1], "r")) == NULL) {
> fprintf(stderr, "Can't open %s file.\n", argv[1]);
> exit(EXIT_FAILURE);
> }
>
> c = getc(fp);
> printf("%c", toupper(c));


Consider putchar(), instead of revving up all that printf()
machinery and laboriously interpreting a format string -- just
to arrive at a character code that's already in your grasp.

> while ( ((c = getc(fp)) != EOF) ) {
> prev = c;
> printf("%c", c);
>
> while (c == '\n') {
> if ((c = getc(fp)) == EOF) {
> break;


And what happens next, after breaking out of this loop?

> }
> printf("%c", toupper(c));
> }
> }
>
> if (prev != '\n')
> printf("\n");
> fclose(fp);
> exit(EXIT_SUCCESS);


Why "success," when all you know (sort of) is that getc()
stopped? You don't know whether you've read all the input, or
maybe had an I/O error. And what about output errors, hmmm?

> }


--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Chad
Guest
Posts: n/a
 
      11-08-2010
On Nov 7, 5:59*pm, Peter Nilsson <(E-Mail Removed)> wrote:
> Chad <(E-Mail Removed)> wrote:
> > Round 3. This code now works not only with the OPs original
> > file, but also, with Eric's input file...

> ...
> > * c = getc(fp);
> > * printf("%c", toupper(c));

>
> But not with an empty file.
>
> * #include <ctype.h>
> * #include <stdio.h>
>
> * int main(void)
> * {
> * * int ch, nl = 1;
>
> * * while ((ch = getchar()) != EOF)
> * * {
> * * * if (nl) { ch = toupper(ch); nl = 0; }
> * * * putchar(ch);
> * * * if (ch == '\n') nl = 1;
> * * }
>
> * * return 0;
> * }
>


Using 'nl' as a test for true and false still eludes me. This, along
with a few other things, are probably the reasons why I don't work as
a computer programmer for a living.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-08-2010
On 11/7/2010 8:59 PM, Peter Nilsson wrote:
> Chad<(E-Mail Removed)> wrote:
>> Round 3. This code now works not only with the OPs original
>> file, but also, with Eric's input file...

> ...
>> c = getc(fp);
>> printf("%c", toupper(c));

>
> But not with an empty file.
>
> #include<ctype.h>
> #include<stdio.h>
>
> int main(void)
> {
> int ch, nl = 1;
>
> while ((ch = getchar()) != EOF)
> {
> if (nl) { ch = toupper(ch); nl = 0; }
> putchar(ch);
> if (ch == '\n') nl = 1;


This would be a little sleeker, I think, as

putchar(nl ? toupper(ch) : ch);
nl = (ch == '\n');

.... but not everybody likes the conditional operator. (On a recent
code review, one of my reviewers protested three uses in two statements
and made me rewrite them as if's, even though this turned five or six
lines of code into about a dozen. Can't please everyone ...)

> }


A few calls to fflush() and ferror() might be welcome here.

> return 0;
> }


--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-08-2010
Chad <(E-Mail Removed)> writes:
<snip>
> Round 3. This code now works not only with the OPs original file, but
> also, with Eric's input file...
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <ctype.h>
>
> int main(int argc, char **argv)
> {
> FILE *fp;
> int c, prev;
>
> if (argc != 2) {
> fprintf(stderr, "Enter a file name.\n");
> exit(EXIT_FAILURE);
> }
>
> if ((fp = fopen(argv[1], "r")) == NULL) {
> fprintf(stderr, "Can't open %s file.\n", argv[1]);
> exit(EXIT_FAILURE);
> }
>
> c = getc(fp);
> printf("%c", toupper(c));
>
> while ( ((c = getc(fp)) != EOF) ) {
> prev = c;
> printf("%c", c);
>
> while (c == '\n') {
> if ((c = getc(fp)) == EOF) {
> break;
> }
> printf("%c", toupper(c));
> }
> }
>
> if (prev != '\n')
> printf("\n");
>
> fclose(fp);
> exit(EXIT_SUCCESS);
> }


You are adding bugs as fast as you remove them. For example, this
version goes wrong if there is no input at all or if the input consists
of a single character. This is often a sign that it's time to take a
step back and re-think.

Here's a tip... I've found that lots of beginners start by writing
programs by following the sequence of what should happen: "first do
this, and then do..." to which various conditions get added along
the way "except if this... but when that..." as it becomes clear that
the program does not quite do what is expected. But your first version
was better because it always did the same thing: every line gets read
and printed with the first letter capitalised. That's good and clear
but it imposes a needless limit on the program, and the array and the
fgets call make it mode fiddly than it needs to be.

The ideal solution here has the simplicity of your first version without
either of the drawbacks. Try to find a rule whereby you can treat every
character in the same way: for every character, print it plain or
capitalised if... what?

I fear that I've not explained this well and that you won't get what I
am driving at until you have exactly the solution that I have in mind
because, of course, you don't treat every character in the same way --
only some get capitalised -- but the point is to make the same
assessment of every character.

--
Ben.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      11-08-2010
Chad <(E-Mail Removed)> wrote:
> Peter Nilsson <(E-Mail Removed)> wrote:
> > * #include <ctype.h>
> > * #include <stdio.h>
> >
> > * int main(void)
> > * {
> > * * int ch, nl = 1;
> >
> > * * while ((ch = getchar()) != EOF)
> > * * {
> > * * * if (nl) { ch = toupper(ch); nl = 0; }
> > * * * putchar(ch);
> > * * * if (ch == '\n') nl = 1;
> > * * }
> >
> > * * return 0;
> > * }

>
> Using 'nl' as a test for true and false still eludes me.


Then consider...

int ch;
int pch = '\n'; /* previous character */

while ((ch = getchar()) != EOF)
{
if (pch == '\n') /* start of a new line? */
ch = toupper(ch);
putchar(ch);
pch = ch;
}

--
Peter
 
Reply With Quote
 
Morris Keesan
Guest
Posts: n/a
 
      11-08-2010
On Sun, 07 Nov 2010 17:55:55 -0500, Eric Sosman
<(E-Mail Removed)> wrote:
....
> If you haven't spotted the new bug yet, try this input:
>
> oh what a tangled web we weave
> when first we practice to write c!

....

That's not a new bug. The OP's original code would have left the
initial 'o' as an 'o'.

--
Morris Keesan -- (E-Mail Removed)
 
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