-5

I am trying to get the length of the words which I have stored in a string array.First,I specify the number of words in an array using 'n'(input from user), then I take inputs from the user of the 'n' words they want to store. After that, I wish to print the length of a word.

Here is the code....

#include <iostream>
using namespace std;

int main(){
    int n;
    cout<<"enter no"<<endl;
    cin>>n;
    string A[n];

     for(int i=0;i<n;i++){
         cin>>A[i];
    }
    cout<<strlen(A[2])<<endl;
} 

For example, if inputs are:

3
leonardo
tom
brad

(They are written in separate lines)

then output should be :

4

which is length of brad.

But I get a strange error while execution of this code. Please suggest a way to do this , while taking inputs from the user in separate lines

  • 1
    What error are you having? Is there a reason why you're not using `std::string`? – ShadowMitia Sep 08 '21 at 12:16
  • 1
    `strlen(A[2])` should be `A[2].size()`. `strlen` is not for use with `std::string`. – Yksisarvinen Sep 08 '21 at 12:17
  • 3
    You have couple of problems with your short programs... First of all [C++ doesn't have variable length arrays](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). Secondly, the [`std::strlen`](https://en.cppreference.com/w/cpp/string/byte/strlen) function is an old C library function which only handle C null-terminated strings, a.k.a. `char*` type strings, not `std::string` objects. – Some programmer dude Sep 08 '21 at 12:18
  • @ShadowMitia They are using `std::string`, but `` and `using namespace std;` makes it a bit confusing. No idea why the header is included instead of `` though – Lala5th Sep 08 '21 at 12:18
  • 3
    I find it really hard to believe that this code compiled at all. Did you mean compiler errors? – Kaldrr Sep 08 '21 at 12:19
  • @Lala5th Good catch, but this make things more confusing for me... – ShadowMitia Sep 08 '21 at 12:20
  • 2
    error messages look strange at first. It takes some practice to read them, but actually they typically contain a whole lot of information and sometimes hints on how to fix them. If you don't understand it you should still include it in the question so others can explain it – 463035818_is_not_an_ai Sep 08 '21 at 12:25
  • 1
    Step one should be to get a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn the basics. Your mix of C and C++ with VLAs on top suggests to me that you are learning from a bad source, maybe from coding competitions. – Lukas-T Sep 08 '21 at 12:25
  • For everyone asking why I used ? Sorry it was from a previous code that I was trying for that I have edited my question, thanks for pointing it out – Abhinav Pipil Sep 08 '21 at 12:37

2 Answers2

2
cin>>n;
string A[n];

The size of an array must be known at compile-time. Variable-length arrays (VLA) are not supported in C++ (also some compilers do as an extension). Use a container with dynamic allocation instead, like std::vector.

std::cin >> n;    
std::vector<std::string> A(n);

And use A[2].size() instead of strlen(A[2]).

m88
  • 1,968
  • 6
  • 14
0

You should use:

cout << strlen(A[2].c_str());

And, for this to work, you also need #include<string.h>.

There are two more ways to perform the same function without including any additional header file.

1st method:

cout << A[2].size();

2nd method:

cout << A[2].length();
Priya
  • 1
  • 1
  • 4