Find the answer to your Linux question:
Results 1 to 10 of 10
Alrighty, well, two things before I ask my actual question: First off, this is going to sound like a homework question. However, it is not. I am teaching myself C++ ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252

    Some C++ String Questions


    Alrighty, well, two things before I ask my actual question:

    First off, this is going to sound like a homework question. However, it is not. I am teaching myself C++ from the book "Practical C++ Programming", and I'm not too successful with this chapter question.

    Secondly, I hate C++. Gah. ("Java" > "C++") is so returning true. Grrrr.


    Anyway, here's the question. The little chapter thing is asking me to count the number of words in a string. I figured that to do this, I would simply count the number of spaces and add 1, right?

    Well, here's my relevant code:

    Code:
    int words = 1;
    
    for&#40;int i = 0; i < myString.length&#40;&#41;; i++&#41;
    &#123;
    	if&#40;myString.at&#40;i&#41; == ' '&#41;
    		words++;
    &#125;
    Now then, when I compile, I get the following error message:

    Code:
    bash-2.05b$ make ex9-1
    g++ -g -Wall -o ex9-1 ex9-1.cpp
    ex9-1.cpp&#58; In function `int main&#40;&#41;'&#58;
    ex9-1.cpp&#58;14&#58; warning&#58; comparison between signed and unsigned integer
       expressions
    ex9-1.cpp&#58;23&#58;2&#58; warning&#58; no newline at end of file
    Line 14 is the condition in my for loop, the

    Code:
    i < myString.length&#40;&#41;
    But that's just a warning. Anyway, I am ALWAYS given an answer of one word, no matter how many words are actually there.

    Can anyone help me out here?


    Thanks.

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    I don't know why you're always getting an answer of 1 but your algorithm
    is not satisfactory.
    I figured that to do this, I would simply count the number of spaces and add 1
    What if there are more than 1 successive spaces ? What if there are other
    non alphanumeric characters ? What if the string starts with a space ?
    You got to take all that into account.

  3. #3
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Right, I am aware that there are a few issues with this. However, I'm just trying to get it working most basically. But even traversal to the first non-space will require correct space-matching.

    And hey, on a happier note, this isn't a professional assignment .

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Hrmm, this is intriguing...

    If I change it to look for a hyphen instead of a space, it works correctly, and reports the right number of words.

    But ' ' does not work. Nor does '\ ' (that's a space escape character). Actually, the latter gives a warning.

    So I'm now REALLY confused.

  5. #5
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    The first one is very simple, length of the string is an unsigned number and comparing it to "int i" yields this warning. Try changing its definition to unsigned or cast before comparison in for loop.
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  6. #6
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    A working rewrite:
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main&#40;&#41;
    &#123;
        int words = 1;
        string myString = "ala ma kota";
        for&#40;unsigned i = 0; i < myString.length&#40;&#41;; i++&#41;
        &#123;
            if&#40;myString.at&#40;i&#41; == ' '&#41;
                words++;
        &#125;
        cout << "Number of words " << words << endl;
    &#125;
    Does it work with you too?
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  7. #7
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    This is quite odd...

    When I hardwire the code into my program (as you did), it correctly detects spaces. When I changed it to get from keyboard, it did not work (still told me 1).

    I am now thoroughly and hopelessly confused.

  8. #8
    Linux User
    Join Date
    Jul 2004
    Location
    Poland
    Posts
    368
    How about this
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main&#40;&#41;
    &#123;
      while &#40;true&#41;
      &#123;
          int words = 1;
          string myString;
          getline&#40;cin, myString&#41;;
          for&#40;unsigned i = 0; i < myString.length&#40;&#41;; i++&#41;
          &#123;
    	  if&#40;myString.at&#40;i&#41; == ' '&#41;
    	      words++;
          &#125;
          cout << "Number of words " << words << endl;
      &#125;
    &#125;
    If you're using "cin >> myString;" to read data from the keyboard mind that it only reads one word per call, so this might be the source of the problem.

    Regards
    "I don't know what I'm running from
    And I don't know where I'm running to
    There's something deep and strange inside of me I see"

  9. #9
    Linux Enthusiast
    Join Date
    Jan 2005
    Posts
    575
    Quote Originally Posted by Cabhan
    When I hardwire the code into my program (as you did), it correctly detects spaces. When I changed it to get from keyboard, it did not work (still told me 1).
    Just modify your programme so it prints what it has read from the keyboard.
    Or instead of printing the string as a whole , print the code of every character
    of the string.This should allow you to get to the bottom of the problem.

  10. #10
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Haha, I completely forgot about getline.

    .

    Yeah, that fixed it. w00+.


    Thanks you all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •