Hello I have a question regarding the function length().

I want to find out the length of the string that is inputted by the user. When I use the length() function it only counts the characters from the first word. Can somebody please take a look at my code and determine why my code is only outputting the number of characters from the first word. For example: when the user inputs "hello" my program out puts 5, when the user inputs "hello world" the program still outputs 5.

#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include <cstdio>

using namespace std;

int main()
{
string sentence;
cout<<"please enter a string: "<<endl;
cin>> sentence;
int length = sentence.length();
cout<< length;

   cin.get();
}

the correct function is size()

When I use the length() function it only counts the characters from the first word.

The problem isn't getting the string length, that's correct. The problem is that the way you're getting input stops at the first whitespace. So when you type "hello world", the string only contains "hello" and " world" is left in the stream. Replace this line:

cin>> sentence;

With this one:

getline(cin, sentence);

the correct function is size()

length() behaves as if it returns size(). The two are equivalent, though size() should be preferred since it more closely matches the other standard containers.

Narue thanks so much for that information it worked perfectly. One more thing. This program is a palindrome checker that ignores spacing and punctuation. There is one more issue tho when strings get too large the application crashes. Also the user can choose to enter another string though the program crashes when this option is chosen. Lastly, the program also doesnt properly close. Somebody please try running my program and see if you have any solutions.

#include <iostream>
#include <string>

using namespace std;

int main()
{
int option = 1;
while(option ==1)
{
// Declaring Variables and strings
int i=0,x=0;
string sentence,sentence2,sentence3;

// Prompt user to enter sentence
cout<<"Please enter a string: ";

// Access user defined string
getline(cin, sentence);

// Find character length of user defined string
int length = sentence.size();

// Create string that does not have any spaces or punctuation
while(i<length)
{
    if(isalpha(sentence[i])||isalnum(sentence[i]))
    {
        sentence2[x] = sentence[i];
        x++;
    }
    i++;
}

// Reinitialize while loop counter i
i=0;

// Create string with opposite order of original string
while(i<x)
{
    sentence3[x-i-1] = sentence2[i];

    i++;
}

//Initialize variable b(parameter used for determining if palindrome condition met)
int b=0;

//Determine if the string is a palindrome
for(i=0;i<x;i++)
{

    if(sentence3[i]==sentence2[i])
    {
        b++;
    }
}

cout<<endl<<endl;

//Output result
if(b==x)
{
    cout<< "The string is a palindrome";
}
else{cout<< "The string is not a palindrome";}

cout<<endl<<endl;

// Prompt user to restart program or exit
cout<< "1. Enter another string"<< endl;
cout<< "2. Exit program" << endl << endl;
cout<< "Please select from the menu options above:";
cin>> option;
}
   cin.get();

}

I think a quick example program will help you to see the problem here:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string s;

    s.push_back('A'); // OK, now s == "A"
    s[1] = 'A'; // Boom, s[1] isn't a valid index

    cout << s << '\n';
}

The string class doesn't grow when you use the index operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.