-4
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    char a[n+1];            // intializing array
    cin.getline(a,n);    // inputting array
    cin.ignore();
    int maxlen=0,curr=0,i=0;
    while(1)
    {
        
        if (a[i] ==' ' || a[i] == '\0'){
            maxlen=max(curr,maxlen);      
            curr=0;
        }
        curr++;

        if (a[i]=='\0'){
            break;
        }
        i++;
    }
    cout<<maxlen;

}

here i am trying to find the max length of word in this sentence but i am not able to save the sentence or input the array. To check i put cout for array and its not printing the array i inputed i want to know the reason

dinesh
  • 1
  • 2
  • `char a[n+1];` -- This is not valid C++. Looks like you're learning C++ from one of those very bad C++ websites. – PaulMcKenzie Apr 12 '22 at 15:50
  • i used the compiler explorer and its saying this but i dont know what it means source>(6): error C2131: expression did not evaluate to a constant (6): note: failure was caused by a read of a variable outside its lifetime (6): note: see usage of 'n' – dinesh Apr 12 '22 at 15:50
  • 1
    Change the compiler to MSVC in compiler explorer. Your program will fail to compile due to that `bits` header, and the invalid C++ syntax I pointed out. If you followed the instructions of a good [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which will not show code like this, neither mistake would have been made. – PaulMcKenzie Apr 12 '22 at 15:52
  • You can avoid allocation issues by using `std::string` and `std::vector`. – Thomas Matthews Apr 12 '22 at 16:04

1 Answers1

2

error C2131: expression did not evaluate to a constant (6):

This is caused due to using non-standard C++ here:

cin>>n;
char a[n+1];            // intializing array

Arrays in C++ must be declared using a constant expression, not a runtime-based expression. Since n is only known at runtime, this is not legal C++.

As to solving the issue, you don't need raw char arrays. This can easily be done using std::string. In addition, using std::stringstream will make the code even simpler by not having to check for whitespace:

#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>

int main()
{
    std::string sentence;
    // Read the sentence
    std::getline(std::cin, sentence);

    // Parse each word
    std::stringstream strm(sentence);
    std::string word;
    size_t maxLen = 0;

    while (strm >> word)
        maxLen = std::max(maxLen, word.size());

    // Output results
    std::cout << sentence << "\n" << maxLen;
}

For this input:

This is a test of getting the longest word in a string

the output is:

This is a test of getting the longest word in a string
7

Finally, you should follow a good C++ book instead of going to websites that shows any coding using:

#include<bits/stdc++.h>

or the invalid array syntax mentioned above.

No reputable C++ book, or reputable C++ website that has been peer-reviewed will show code using that header, or the invalid array syntax.

If you go to a website that shows code like this, and the website's purpose is to teach C++, it isn't one you should learn any C++ from.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45