Re: Exercise 7-1



mdh <mdeh@xxxxxxxxxxx> writes:
On Sep 2, 8:53 pm, Keith Thompson <ks...@xxxxxxx> wrote:
mdh <m...@xxxxxxxxxxx> writes:
I am not sure how relevant to C this is, but here goes.
.

Your indentation is inconsistent and doesn't correspond either to the
actual structure of your code or to the intended structure of your
code.

Sorry about the formatting.I will have to figure that out sometime.


Here's your program after it's been filtered through "indent -kr" (and
after I manually joined the split line):

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

int main(int argc, const char *argv[])
{
    int c;
    if (strcmp(argv[0], "/Users/m/Desktop/upper/build/Release/upper") == 0)
        while ((c = getchar()) != EOF)
            putchar(toupper(c));
    else
        putchar(tolower(c));
    return 0;

}

This should be enough to tell you what the problem is.

The solution, except for the omission in error of a second "while ((c
= getchar()) != EOF)" is from Tondo and Gimpel.

That would be "The C Answer Book". If you're going to cite books
other than K&R, please be specific; I had to look that up.

I think it is really
simple to illustrate the issue that K&R are trying to make, instead of
making it bullet proof?




while ((c = getchar()) != EOF){
putchar(toupper(c));
}
else {
while ((c = getchar()) != EOF)
putchar(tolower(c)); }

<<<<<

But, have I missed something. Is that the reason one needs a 2 step
invocation of this program?

(Does the subject "Exercise 7-1" refer to K&R? My copy isn't handy.)

I'm afraid I can't tell from your response whether you understood my
point or not. In the code you've marked with ">>>>>" and "<<<<<", you
have an "else" with no corresponding "if". If that's a fragment of
Tondo and Gimpel's solution, it's a poorly chosen fragment.

If I understand correct, the program is supposed to convert its input
to upper case if it's invoked as "upper", and to lower case otherwise.

The problem is that, due to the way you've structured the code, if
it's invoked as "upper" (or rather as "/Users/.../upper") it uses a
while loop to read each character from stdin, otherwise it reads only
a single character.

There are two obvious ways to do what you're trying to do.

In pseudo-code, with structure shown only by indentation, you can do
either:

if my name is "upper"
for each input character
print that character in upper case
else
for each input character
print that character in lower case

or:

for each input character
if my name is "upper"
print that character in upper case
else
print that character in lower case

What you've done is an incorrect mixture of the two approaches.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <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"
.



Relevant Pages

  • Re: What Happens to Superman in 2013
    ... or at least retaining a mark when it has happened de facto. ... A character can't really be generic. ... to argue that Superman has many versions ...   ...
    (rec.arts.comics.dc.universe)
  • Re: YASD - Crawl 0.4.3 - Spriggan Enchanter
    ... Slash'EM character ...     ... Sling and you devoted a lot of inventory weight to ammo, ... Very unusual choice of spells and skills there. ...
    (rec.games.roguelike.misc)
  • Re: Silver Nemesis reborn, only with cappier music
    ... All Shakespeare did was give that Moor a name. ... to the Shakespearean character, and it isn't given sufficient detail ... told the history of the real Othello which he then based his play on. ...
    (rec.arts.drwho)
  • Re: Design Goals.
    ...    abilities; instead they can just get whatever their favorite kit ... character class system whereby every character has the same innate ... character's weapon, armor, ring or staff. ... charges, and they will recover charges over time by themselves. ...
    (rec.games.roguelike.development)
  • Re: Help on getting a result in a report
    ... treated as greater than 100% because the first character is greater than ... If you wanted to do this without code, you could use the Switch() function ...
    (microsoft.public.access.reports)