Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What am I doing wrong? (C++ strings)

Tags:

c++

arrays

string

#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    int n;
    cin >> n;

    string a;
    cin >> a;

    int index;

    for(int i=0;i<strlen(a);i++)
    {
        if(a[i]=="?")
        {
            index=i;
        }
    }

    cout << index;

    return 0;
}

I want to find the "?" in the string if there is any, but I get error: "ISO C++ forbids comparasion between pointer and integer"

Any help?

like image 867
John Smith Avatar asked Jan 18 '26 08:01

John Smith


1 Answers

Use single quotes around the '?' character to indicate a char instead of a string. That's why your comparison is failing.

like image 59
Richard Brown Avatar answered Jan 20 '26 01:01

Richard Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!