Working through H&K... Strange Output

This is a discussion on Working through H&K... Strange Output within the C Programming forums, part of the General Programming Boards category; Hi All! I'm working though H&K, specifically, I'm looking at exercise 1-8 (Write a program that counts blanks, tabs, and ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Working through H&K... Strange Output

    Hi All!

    I'm working though H&K, specifically, I'm looking at exercise 1-8 (Write a program that counts blanks, tabs, and lines.) My code seems to count spaces and lines correctly, but for some reason the number of tabs always ends up at around 4,100,000.

    Any ideas as to what might be wrong?

    Code:
    #include <stdio.h>
    
    /*Exerceise 1-8 from H&K- counts spaces, tabs, and lines */
    int main () {
    
    int c, blank, tab, newline = 0;
    
    while ((c = getchar()) != EOF) {
    if (c == ' ')
      ++blank;
    
    else if (c == '\t')
      ++tab;
    
    else if (c == '\n')
      ++newline;
    
    }
    
    printf("\nSpaces: %d\nTabs: %d\nLines: %d\n", blank, tab, newline);
    
    }
    Thanks for any help you may be able to render in advance!

  2. #2
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    only newline equals zero, the rest have the value of some junk in memory.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Oh, I see. I could have sworn you could initialize variables like that. Ha, it's been a long time I suppose.

    Thanks! I'm sure I'll be back with more dumb questions soon enough.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    maybe you confused with c = blank = tab = newline = 0.

  5. #5
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    You can, but you have to be explicit
    Code:
    int c, blank = 0, tab = 0, newline = 0;

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    I think I'll just use this method from now on:

    Code:
    int a, b, c;
    
    a = 0;
    b = 0;
    c = 0;
    Ok so now I'm on exercise 1-9 and I have another issue... The idea is to get input from the keyboard, and print that input to the screen replacing any series of blanks with one blank.

    Code:
    #include <stdio.h>
    
    int main() {
    
      int c = 0;
      
      while ((c = getchar()) != EOF)
        if (c != ' ')
          putchar(c);
        else {
         putchar (' ');
         c = getchar(); //not sure about this
        
         if (c != ' ')
           putchar(c);
        
        }
    }
    Currently, it works properly with a string with 2 spaces, such as "abc abcd", but with a string like "jfjf <several spaces> jfjf" it seems to simply cut the number of blank spaces in half instead of reducing the number to one.

    I've looked through this code several times, and it looks like it should work. However, I don't understand much about the inner workings of getchar(). Is this the proper method to tell getchar() to skip a character and go to the next one, or does calling c = getchar() again cause it to look for new input...?

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    You just need to call putchar(' ') once for every line, so why don't you use a flag to check if you already have put a bllank in the current line?

    your first if statement if ok, the else is wrong. You only need your first getchar.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    what is H&K???

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Heckler & Koch. Or Helsingin Kauppiaat. Firearms or meat or C code, same stuff.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Another noob question!

    Code:
    #include <stdio.h>
    
    int main () { 
    
    int c = 0;
    
    while ((c = getchar()) != EOF) {
    
      if (c == '\t') {
        putchar('\\');
        putchar('t');
      } else if (c == '\b') {
        putchar('\\');
        putchar('b');
      } else {
      putchar (c);
      }
    }
    
    return 0;
    }

    This code is supposed to replace tabs with "\t" and backspaces with "\b". Works correctly with the tabs, but not the back slashes. When I type the backspace, is a backspace character actually added, or does it simply erase the previous character?
    Last edited by Ghiofish; 10-06-2009 at 08:45 PM. Reason: Corrected "Backslash

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It probably doesn't actually do either. Most input is line buffered, meaning it doesn't actually end up sending \b to your program. Write \b to a file, then read it back in, and run it through the above loop, and you'll more than likely get the correct input. (Or write it out, and pipe it in.)


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

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Quote Originally Posted by quzah View Post
    It probably doesn't actually do either. Most input is line buffered, meaning it doesn't actually end up sending \b to your program. Write \b to a file, then read it back in, and run it through the above loop, and you'll more than likely get the correct input. (Or write it out, and pipe it in.)

    Quzah.
    Hmm... I was afraid of that. I guess I'll have to learn how to input from a file before testing that one.

    I really hate to keep bothering you guys, but what about this one?

    Code:
    #include <stdio.h>
    
    #define IN  1
    #define OUT 0
    
    int main () {
      
      /* counts lines, words, and characters in input */
      
      int c, nl, nw, nc, state;
      nl = nw = nc = 0;
      
      state = OUT;
      
      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", nc, nl, nw);
    
    }

    (Counts characters, words, and lines then outputs this to the screen.) For some reason this program always tells me I'm getting 30 or so more words than I actually input. As far as I can tell, this is the exact code from the book. What's going on here?

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remove one equal sign from this line:

    state == IN

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  2. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 10:20 AM
  3. Binary Search - Strange Output
    By mike_g in forum C Programming
    Replies: 13
    Last Post: 06-16-2007, 02:55 PM
  4. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM
  5. Removing *Unchanged* Output File (ofstream) :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 01-05-2002, 06:47 PM

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