0

I keep getting this error each time i compile and run : main.cpp:15:29: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]

#include <iostream>

using namespace std;

int main()
{
    string paragraph;
    cin>>paragraph;

    int wordsCount;
    int pSize = paragraph.length();

    for (int i = 0; i<pSize ; i++)
    {
        if (paragraph[i] == " ") {
            wordsCount++;
        }
    }




    cout << pSize << endl;


    return 0;
}

1 Answers1

3

" " is a string literal. This is an immutable character array and is converted to a pointer to the first element of the array in this case.

You should ' ' (character surrounded by single quote, not double quote) to represent one character.

Also the variable wordsCount is incremented without being initialized. You should initialize first before doing calculation with its value.

What is done in the for loop is actually not used in this case, so you can remove the loop like this:

#include <iostream>

using namespace std;

int main()
{
    string paragraph;
    cin>>paragraph;

    int pSize = paragraph.length();

    cout << pSize << endl;

    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70