Thread: How do I do this?

  1. #1
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    How do I do this?

    In my book I am given this exercise to do at the end of Chapter 5.

    Code:
     Exercise 5-3
    Create a program that uses a do while loop to count the numbers of
     non-whitespace characters entered on a line. The count will end
     when it first encounters the character '#'.
    Now I have a good idea of what to do - IE: using funtions in the cctype header. What erks me though, is the part about the characters 'entered on a line' What does that mean? Do I ask the user to enter a sentance with a # at the end? I am confused by this because the only variable that I have learned about so far that stores a character are variable of type char... and if I am not mistaken a variable of type char is only able to hold one character, right? I have not learned about strings yet, that's the next chapter.

    So, how am I supposed to do this? The book is Ivor Horton's Beginning ANSI C++: The complete language. So if you are familiar with the book -- please help me out here! Also, the source code for the exercises is available from the books website to view... IE: If you are stuck or if you want to see if it's done right. I don't want to look at the code -- but I am completely stumped on how to take the input from the user - or if it's even supposed to be input from the user! AHHHH!
    Oh my goodness.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    A line ends at a newline character '\n'. Therefor you would count the amount of whitespace until you encounter a newline. Store that number for line one, reset the count variable and start counting again for line two. If you wish you could also make a running total of all the whitespace until the pound sign.
    And yes, the idea of this exersize would be to ask the user to enter a pound sign. If this is for class, go for bonus points by looking for a pound sign and giving an error message if they didn't put one or append it to the end automatically.
    By the way, this is quite simple to do without strings. Just read it in one character at a time. If it's a space or tab, increment the count. If it isn't don't. Then just read over your variable with the next character.
    Last edited by SlyMaelstrom; 02-20-2006 at 05:39 PM.
    Sent from my iPadŽ

  3. #3
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by SlyMaelstrom
    A line ends at a newline character '\n'. Therefor you would count the amount of whitespace until you encounter a newline. Store that number for line one, reset the count variable and start counting again for line two. If you wish you could also make a running total of all the whitespace until the pound sign.
    And yes, the idea of this exersize would be to ask the user to enter a pound sign. If this is for class, go for bonus points by looking for a pound sign and giving an error message if they didn't put one or append it to the end automatically.
    By the way, this is quite simple to do without strings. Just read it in one character at a time. If it's a space or tab, increment the count. If it isn't don't. Then just read over your variable with the next character.
    I'm still not sure how to store a statement that the user enters as type char. *or however I need to format it so I can break it apart with a loop that detects letters.
    Last edited by mabufo; 02-20-2006 at 05:48 PM.
    Oh my goodness.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You don't store it. It's already stored in the input buffer. All you do is extract it one character at a time, check if it's whitespace, increment your count if it is, then read in the next character.
    Sent from my iPadŽ

  5. #5
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    I see. The problem with that though - is I have no idea how to extract something from the input buffer,in fact, I'm not sure what that is - as that was not in the book.
    Last edited by mabufo; 02-20-2006 at 05:54 PM.
    Oh my goodness.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If you were there on day one, you know how to store data into a variable.

    Code:
    cin >> character1;
    Sent from my iPadŽ

  7. #7
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by SlyMaelstrom
    If you were there on day one, you know how to store data into a variable.

    Code:
    cin >> character1;

    I thought you said I wasn't supposed to store it?
    Oh my goodness.

  8. #8
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    No, I said you weren't supposed to store the statement (as a string). You need to store the variable you're checking.
    Sent from my iPadŽ

  9. #9
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    I think what we have here is a failure to comunicate. I caved - I looked at the source code and I have to say, I'm suprised at the solution.... Here it is:

    Code:
    // Exercise 5.3 Using a do-while loop to count characters
    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;
    int main() {
      long count= 0L;
      char ch;
    
      cout << "Please enter a sequence of characters terminated by '#':" << endl;
    
      // We have to read at least one character - even if it's '#' - so do-while is best
      do {
        cin >> ch;
        ++count;
      } while (ch != '#'); 
    
      // We do not count '#' as a character, so count must be adjusted
      --count;
      cout << "You entered " << count 
           << " characters (not counting spaces and the terminal #)." << endl;
    
      return 0;
    }
    Very... simple. (now this is the part where I feel like a moron... not due to me not being able to solve it - but not understanding really how to solve it... the exercises really are not very specific as you can probably tell.)

    EDIT: and I was under the impression that char could only hold one variable. IE: one letter. I was very wrong... and I guess that's what confused me the most.
    Oh my goodness.

  10. #10
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    I think that I'm just thinking about these way too hard... time for a break I suppose.
    Oh my goodness.

  11. #11
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    What about that code didn't I say? Other than the fact that it's just counting characters, not counting whitespace. It declares a count variable an a character variable. Reads in a character and increments count. It keeps doing that until it encounters a #.
    Sent from my iPadŽ

  12. #12
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by SlyMaelstrom
    What about that code didn't I say? Other than the fact that it's just counting characters, not counting whitespace. It declares a count variable an a character variable. Reads in a character and increments count. It keeps doing that until it encounters a #.
    I can tell what the thing does, thank you. I was more than a bit confused is all - thanks for the help I guess.
    Oh my goodness.

  13. #13
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    Quote Originally Posted by mabufo
    EDIT: and I was under the impression that char could only hold one variable. IE: one letter. I was very wrong... and I guess that's what confused me the most.
    No, you were right the first time - a char can only store one character at a time. What leads you to believe otherwise?

  14. #14
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    The program posted above - I see where I was mistaken considering the loop counts the characters one by one... what confused me though about what char could store is that if I entered an entire sentance into the program - ending with a #.. for example I enter:
    Code:
    I enjoy a good slice of pie. #
    It will tel me I entered 22 characters! How can it do this, if it's only supposed to store one letter?

    (that's what got me so confused)
    Oh my goodness.

  15. #15
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    It doesn't store 22 letters. Those are currently in your buffer. In this case I/O stream has done all the work up to this point. You just need to extract the first character from the string and put it into a variable.

    Code:
    cin >> ch;
    Now the 'I' is in the ch variable and the cin buffer has " enjoy a good slice of pie. #\n" in it. If you were to cin another character, you'd get the space.... and so on and so on until you get to the pound sign.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed