c++ program not acting how I expect

Discussion in 'Web Design & Coding' started by undeadpenguin, Feb 9, 2004.

  1. undeadpenguin

    undeadpenguin Bang. VIP Member

    Joined:
    Sep 1, 2003
    Messages:
    925
    Likes Received:
    4
    Location:
    US, CT
    I learned some very basic C++ a little while ago, and have since forgotten it all. Last week I reopened my C++ book though.

    My question concerns the following block of code:
    Code:
    //A program to count words in a sentence
     #include <iostream>
     #include <string>
     
     std::string sentence;
     int spaceCounter = 0;
     int current;
     
     int main()
     {
     	std::cout << "Sentence: ";
     	std::cin >> sentence;
     	int lengthOf = sentence.length();
     	for(current = 0; current <= lengthOf; ++current)
     	{
     		if(sentence.at(current) == ' ')
     			++spaceCounter;
     	}
     	std::cout << spaceCounter + 1 << " words.\n";
     	return(0);
     }
    I expect this program to act like this:
    Code:
    $ ./spaceCounter
     Sentence: a simple sentence
     3 words.
    Instead, it runs like this:
    Code:
    $ ./spaceCounter
     Sentence: a simple sentence
     Aborted
    Can anyone either a) find the error or b) explain to me why it does not work?
     
    undeadpenguin, Feb 9, 2004
    #1
    1. Advertisements

  2. undeadpenguin

    Fenis-Wolf VIP Member

    Joined:
    Apr 30, 2003
    Messages:
    2,951
    Likes Received:
    35
    Location:
    Ann Arbor, Mi
    Did the compiler scream during compiling?
     
    Fenis-Wolf, Feb 9, 2004
    #2
    1. Advertisements

  3. undeadpenguin

    undeadpenguin Bang. VIP Member

    Joined:
    Sep 1, 2003
    Messages:
    925
    Likes Received:
    4
    Location:
    US, CT
    nope - no errors...
     
    undeadpenguin, Feb 9, 2004
    #3
  4. undeadpenguin

    Core in pounce mode Moderator

    Joined:
    Jun 30, 2003
    Messages:
    1,557
    Likes Received:
    24
    Location:
    Akaa, Finland
    a wild guess here... suppose there are n characters in the string. your for() loop is accessing 0, 1, ... , n-1, n. but if you count the elements of this sequence, you'll see that there are n+1 of them! something's amiss, no?

    rule of thumb is that if n is the number of elements you want to iterate over, you start at 0 and continue while you are strictly less than n. what you should really be doing is to understand what you're accessing. what your conditions are and why?

    anywho...reckon your for loop should look something like this:

    Code:
     for(int current = 0; current < lengthOf; ++current) { // strictly less than
      
         if(sentence.at(current) == ' ') ++spaceCounter;
      
     }
     
     
    Core, Feb 9, 2004
    #4
    undeadpenguin likes this.
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.