Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

lvalue required

Name: Anonymous 2012-09-02 1:19

So, I've been trying to learn C out of the book "The C Programming Language", and in chapter 1 it gives me a program to count words, but every time I try the code:

#include <stdio.h>

#define IN 1
#define OUT 0

main(){
    int c, nl, nw, nc, state;

    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF){
        ++nc;
        if (c == '\n')
            ++nl;
        if (c == ' ' || c == '\n' || c = '\t')
            state = OUT;
        if (state == OUT){
            state = IN;
            ++nw;
        }
    }
    printf("%d %d %d\n", nl, nw, nc);
}

I try to compile it and it gives me the error "lvalue required as left operand of assignment" on line 15. I've looked it up on google and I couldn't find anything wrong with this. And from just looking at it, it looks fine.

What's going on here?

Name: Anonymous 2012-09-02 1:29

Well, after going back through it, I find those OR statements to be a problem. I don't see why they are, but after I turned


        if (c == ' ' || c == '\n' || c = '\t')


into just:


        if (c == ' ' )


and tested it from there, everything worked as it should have. I mean, except that the program isn't complete and tabbing or entering a new line wont show up as another word. But still, this doesn't really resolve the question, because I still can't see what's wrong with line 15. I'll have to take a look at OR statements and see what I can do.

Name: Anonymous 2012-09-02 1:36

I'm guessing the way the ORs work is the problem. Adding parenthesis fixes the error, so the computer must be reading the if statement funky.

Name: Anonymous 2012-09-02 1:42

>>3
The computer rarely makes mistakes, I would rather assume it was because you wrote c = '\t' instead of c == '\t'.

If something doesn't work from the book assume you made a mistake and not the machine, that way you're guaranteed to learn something. If you can't spot the mistake delete everything and write it over freshly.

Name: Anonymous 2012-09-02 1:50

1. Count how many equals in if (c == ' ' || c == '\n' || c = '\t').
2. The compiler error makes sense once you know the operator precedence of ==, ||, =.
3. Read SICP.

Name: Anonymous 2012-09-02 1:53

I believe you want this instead:

        if (c == ' ' || c == '\n' || c = '\t') {
            state = OUT;
        } else if (state == OUT){
            state = IN;
            ++nw;
        }

Name: Anonymous 2012-09-02 1:54

>>6
Why would he want to repeat the same mistake but now with an even shittier formatting?

Name: Anonymous 2012-09-02 1:58

>>7
Oh, I didn't notice the '=' mistake. But look carefully at the change I made. In his version state will immediately assigned IN every time it's assigned OUT.

Name: Anonymous 2012-09-02 1:59

>>8
That is true, I did not notice that mostly since I didn't bother reading the code in the OP, it's astonishing how poorly OP managed to copy some text from a book.

Name: Anonymous 2012-09-02 2:01

>>8
That's intentional, otherwise you miss a character.

saging the top thread

Name: Anonymous 2012-09-02 2:10

>>10
nw stores the number of words, not characters!

Name: Anonymous 2012-09-02 2:38


#include <stdio.h>
main(){ int c, nl, nw, nc; bool out; out = false;
  nl = nw = nc = 0;
  while ((c = getchar()) != EOF){
    ++nc;
    if (c == '\n') ++nl;
    if (c == ' ' || c == '\n' || c == '\t') out = true;
    if (out){ out = false; ++nw; }}
  printf("%d %d %d\n", nl, nw, nc);}

Fucking optimized for space and corrected for proper function.
K&R is pig disgusting.

Name: Anonymous 2012-09-02 3:29

>>12
Your fucking disgusting!

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-09-02 6:59

More than 90% of programmers have trouble pronouncing the 'lv' syllable. It properly sounds like a mix between 'lalue' and 'value'.

As a workaround, people have adopted the rather less concise pronounciation 'ell-value'.

Name: Anonymous 2012-09-02 10:19

OP here. I passes out last night, before I could see any responses.

>>9
I actually copied the book, character for character, 100%. I now realize the book actually has c = '\t' instead of c == '\t', but it must be noted that instead of book, I mean pdf copy of a book. I am actually really embarrassed that I didn't see that though. I looked through this code so many times, and line 15 even more, and I didn't even notice the 1 = sign instead of two. If I had noticed, I would have got it.

Thanks all for helping me with this problem.

Name: Anonymous 2012-09-02 10:45

Poorfag stole the book and it's full of typos.

Name: Anonymous 2012-09-02 11:32

part of The Ghetto Programmer series of books.

Name: Anonymous 2012-09-02 22:38

Name: FFP 2012-09-02 23:13

>>18
fuck you asshole IHBT
WYPMP

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2012-09-03 4:02

>>18
The 'xh' syllable is similarly difficult to pronounce.

Name: Anonymous 2012-09-03 5:27

>>20
PRONOUNCE MY ANUS

Name: Anonymous 2012-09-03 10:49

>>20
ecks
hamster

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List