Thread: anyone understand this?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    6

    Question anyone understand this?

    ok im trying to compile this code from a book and it isnt compiling
    here it is:

    Code:
    if (c == ' ' || c == '\n' || c == '\t')
    should be if 'c' is blank , newline or tab but the compiler (dev for windows) is saying "Invalid lvalue in assignment"
    any ideas?

    Oh and if you need more of it I can post it

  2. #2
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    What type is 'c'? If it is a char as in:
    Code:
    char c;
    Than the tests are okay, I think! But if you defined 'c' like this:

    Code:
    char c[2];
    Than you need to use double quotes " " in your test.

    Also since I can't see your code, make sure you use the & when you scan the character c in scanf, or else don't use the & if c is a string. Maybe show some more code next time.

    Okay for example:
    Code:
    #include<stdio.h>
    int main()
    {
      char c;
      scanf("%c",&c);
      if(c== ' ' || c == '\n' || c == '\t') puts("Owtch");
      return 0;
    }
    Actually it won't work with strings because you have to use the function: strcmp(source,compare); So just use characters instead like in the code above.
    Last edited by Witch_King; 09-03-2001 at 11:45 PM.
    I compile code with:
    Visual Studio.NET beta2

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    6
    been away for a day or two but here is the entire program:

    Code:
    #include <stdio.h>
    
    #define IN 1        /*inside a word*/
    #define OUT 0     /*outside a word*/
    
    /*count lines, words, and characters in input*/
    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;
                  else if (state == OUT) {
                        state = IN;
                        ++nw;
                   }
              }
            printf ("%d %d %d\n", nl, nw, nc);
    }
    its still the if (c == ' ' || c == '\n' || c =='\t') line that is causeing me problems (or so my compiler tells me)
    thanks for any help on this

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    I tried you code on MSVC and Dev C++ and it compiled on both of them.

  5. #5
    Anti-Terrorist
    Join Date
    Aug 2001
    Location
    mming, Game DevelopmentCSR >&<>&2Minimization of boolean functions, PROM,PLA design >&0>&WA, USA guitar, dogsCommercial Aviation >&>>&USAProgramming
    Posts
    742
    I didn't compile the code since zen said that it compiles but this looks like one of those 'clear the standard input buffer' problems.
    Inside your while loop, include this code:
    Code:
    while(getchar() != '\n') continue;
    I compile code with:
    Visual Studio.NET beta2

  6. #6
    Registered User Xaviar Khan's Avatar
    Join Date
    Sep 2001
    Posts
    10
    I compiled this code as well. Compiled with 2 warnings that were easily fixed. Just one question, what exactly are you trying to do with this code. Maybe it can be done another way. There is always another way! (well most times :O)
    Xaviar Khan
    Programmer/Owner
    Shards of Destiny (SoD)
    ---------------------------------------
    Reach us with any MUD client at ShardsofDestiny.org
    port 9000.
    +------------------------------------+
    | Html/Java support added, |
    | 'www.ShardsofDestiny.org' |
    +------------------------------------+

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by Xaviar Khan
    I compiled this code as well. Compiled with 2 warnings that were easily fixed. Just one question, what exactly are you trying to do with this code. Maybe it can be done another way. There is always another way! (well most times :O)
    It's an exercise from a book. I recognize it, it's from K&R.

Popular pages Recent additions subscribe to a feed