trouble counting characters.

This is a discussion on trouble counting characters. within the C Programming forums, part of the General Programming Boards category; I am having trouble writing a program that counts the amount of th's in an inputted text. I think I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    35

    trouble counting characters.

    I am having trouble writing a program that counts the amount of th's in an inputted text. I think I have the right code, but I keep getting an error saying that left operand must be an l-value. This would be in the else-if block (thisChar = 'h').

    here is my code.

    Code:
       int numThs = 0;
       char thisChar, lastChar = ' ';
       
      while (scanf("%c", &thisChar) != EOF){
         if (scanf("%c", &thisChar) == 1){
    		 if (thisChar = 't')
    			lastChar = thisChar;
    		 else if (thisChar = 'h' && lastChar = 't')
    			 numThs++;
    	 }
      }
      printf("There were %d th's.\n", numThs);
    thanks for any help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You are calling scanf two times, so you are reading two different characters.
    = assigns a value to a variable
    == compares values


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    ok so this is what I changed it to. everything compiles and it runs, but my count is 0 no matter what I type.

    Code:
    #include <stdio.h>
    
    int main() {
    int numThs = 0;
       char thisChar, lastChar = ' ';
       
      while (scanf("%c", &thisChar) == 1){
         if (thisChar = 't')
    		lastChar = thisChar;
         else if (thisChar == 'h' && lastChar == 't')
    		numThs++;
    	 
      }
      printf("There were %d th's.\n", numThs);
    }
    i'm telling it to do the if statement as long as scanf successfully reads a character. In the if statement, I want thisChar copied to lastChar if thisChar is a 't'. I am doing this because I need to hold onto that 't', so the loop can go again and read another character. If this character is an 'h', while lastChar is 't', then it'll add 1 to numThs. Is my logic wrong?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You missed one:
    Code:
         if (thisChar = 't')
    If you had compiled with warnings turned on, you wouldn't have.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    ahhhhhhh!!! thank you so much. I'm pretty new at programming and it's forcing me to be very detail oriented.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are able to specify what compiler options you use, try adding -Wall


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    35
    i'm using visual studio C++.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably want something like this: How to: Enable or Disable Compiler Warnings


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by christianB View Post
    ok so this is what I changed it to. everything compiles and it runs, but my count is 0 no matter what I type.

    Code:
    #include <stdio.h>
    
    int main() {
    int numThs = 0;
       char thisChar, lastChar = ' ';
       
      while (scanf("%c", &thisChar) == 1){
         if (thisChar = 't')
    		lastChar = thisChar;
         else if (thisChar == 'h' && lastChar == 't')
    		numThs++;
    	 
      }
      printf("There were %d th's.\n", numThs);
    }
    i'm telling it to do the if statement as long as scanf successfully reads a character. In the if statement, I want thisChar copied to lastChar if thisChar is a 't'. I am doing this because I need to hold onto that 't', so the loop can go again and read another character. If this character is an 'h', while lastChar is 't', then it'll add 1 to numThs. Is my logic wrong?
    Ok... climb into your compiler settings and turn the warning levels up to maximum. Then actually read the report it gives while trying to compile your code... treat each reported warning and error as a problem to be fixed. Get it to compile without warnings or errors.... see what happens.

    Right off the top your program "skeleton" is incorrect. The minimum C program is...
    Code:
    int main (void)
      {
    
         // your code here
    
        return 0; }
    Also with your present logic it will catch "this" ... "to his" ... and "top of hot stove"... as th combos.
    Last edited by CommonTater; 07-16-2011 at 07:05 AM.

  10. #10
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    is there a way to do this to account for capital letters ?

    the ctype.h functions are available

    i've got:

    while (scanf("%c", &thisChar) == 1) {
    if (thisChar == 'h' || thisChar == 'H' && lastChar == 't' || lastChar == 'T')
    numThs++;
    lastChar = thisChar;
    }

    but is there a better way to re-do that if() statement?
    Last edited by ps2fats; 07-21-2011 at 02:52 AM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ps2fats View Post
    is there a way to do this to account for capital letters ?

    the ctype.h functions are available
    You could always go look at the ctype.h functions (you know, in your book, on the internet) so that you could have seen isupper or tolower etc.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    right, i am aware of those functions, but on how to incorporate it so that I am able to reduce that if() statement to fewer characters.

    Is there a way to assign both thisChar and lastChar to tolower so that all i have to do is count lowercase?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ps2fats View Post
    right, i am aware of those functions, but on how to incorporate it so that I am able to reduce that if() statement to fewer characters.
    Maybe programming isn't your thing then. Turn the small letters into big ones, or the big ones into small ones, and then only check for one or the other.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Registered User
    Join Date
    Jul 2011
    Posts
    4
    is there a way to do that within the if statement?

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ps2fats View Post
    is there a way to do that within the if statement?
    Sure.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help counting characters
    By dom89 in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 10:25 PM
  2. Counting Characters
    By jrdoran in forum C Programming
    Replies: 2
    Last Post: 11-28-2006, 08:06 PM
  3. Counting characters
    By jmajeremy in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 10:14 AM
  4. Counting Characters HELP !!
    By Zozo17 in forum C Programming
    Replies: 2
    Last Post: 03-01-2005, 08:00 PM
  5. Counting characters
    By Calavera in forum C Programming
    Replies: 5
    Last Post: 10-01-2004, 11:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21