how could i get the length of my name

closed account (16MoizwU)
I have an assiganment which ask for sentence and the length of the sentecne, i write out program but i could not get the lenght of the sentence. the program is written below

#include<iostream>
#include<string>
using namespace std;

int main() {
// use string variable to write a sentence
string str;
cout << " please enter your full name: ";
getline (cin,str);
cout << " The sentence typed was: " << str << ".\n";
string ln (" ");
cout << " the length of your name is: " << ln.length() << " charactares.\n:";
return 0;
}

please help me out
1. Use the code tag.
2. Change ln.length() to str.length() Because you want to get the length of the string str. Not the string ln.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include<string>
using namespace std;

int main() {
// use string variable to write a sentence
string str;
cout << " please enter your full name: ";
getline (cin,str);
cout << " The sentence typed was: " << str << ".\n";
string ln (" ");
cout << " the length of your name is: " << str.length() << " charactares.\n:";
return 0;
}
Last edited on
closed account (16MoizwU)
thank you so much LacViet, I have another two questions can you help me solve it out.
Topic archived. No new replies allowed.