I need help with an error.

I don't know what I did or haven't done but this error,

"no match for 'operator==' in 'std::getline [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((std::basic_istream<char, std::char_traits<char> >&)(&std::cin)), ((std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)(&ch))) == (&str)->std::basic_string<_CharT, _Traits, _Alloc>::operator[] [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((unsigned int)i))' ",

shows up every time I try to run my program, which is to
1.) Enter a message(str)
2.) Enter a letter or word to be found(ch)
3.) Display the no. of times a letter/word appears when it matches any letter/word of the message
4.) Repeat #2 If the entered letter or word does not match any of those of the message (repeat until it matches)


#include<iostream>
using namespace std;

int main()
{
int i, frequency = 0;
string str = "";
string ch = "";

cout << "Enter a message: ";
getline(cin, str);

cout << endl;

{
cout << "Enter a character to be found: ";
getline(cin, ch);
}

for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
}


cout << "The character '" << ch << "' appeared " << frequency << " time(s) in the message.";

cout << "\n\n";
system("pause");
return 0;
}


A sample output should be:
--------------------------------------------------------------------
Enter a message: I need help with an error.

Enter a character to be found: z
Enter a character to be found: zyzyz
Enter a character to be found: xvsvsvs
Enter a character to be found: e
The character 'e' appeared 4 time(s) in the message.
--------------------------------------------------------------------
OR
--------------------------------------------------------------------
Enter a message: I need help with an error.

Enter a character to be found: z
Enter a character to be found: zyzyz
Enter a character to be found: xvsvsvs
Enter a character to be found: need
The character 'need' appeared 1 time(s) in the message.
--------------------------------------------------------------------


Any help would be highly appreciated!

p.s. I don't use #include<studio.h> or static of some sort :)

Last edited on
1. #include <string>

2. We want ch to hold a single character, so declare it as char.
1
2
// string ch = "";
char ch = 0 ;


3. To read a single character into ch:
1
2
3
cout << "Enter a character to be found: ";
// getline(cin, ch);
cin >> ch ;
Thank you for response, JLBorges.

It works now with a single character.
The error isn't showing up anymore and while the program is working, whenever I enter a word
it only recognizes its first letter.

ex.
--------------------------------------------------------------------
Enter a message: I need help with an error.

Enter a character to be found: help
The character 'h' appeared 1 time(s) in the message.
--------------------------------------------------------------------

The program should work with both single character and/or the whole word.

What we want to happen is
--------------------------------------------------------------------
Enter a message: I need help with an error.

Enter a character to be found: zzzz
Enter a character to be found: help
The character 'help' appeared 1 time(s) in the message.
--------------------------------------------------------------------
AND
--------------------------------------------------------------------
Enter a message: I need help with an error.

Enter a character to be found: zzzz
Enter a character to be found: h
The character 'h' appeared 1 time(s) in the message.
--------------------------------------------------------------------
Last edited on
In your if statement if you want to compare the str with ch, you can use the string member function substr The length of the substr depends on the size of the word you are looking for.

1
2
3
if(ch ==ch.substr(/*fill this in*/)){
  ++frequency; 
}


Also for your loop, you can just use i < str.size()
Last edited on
Thanks, Hengry, however the error
"no match for 'operator==' in 'ch == std::basic_string<_CharT, _Traits, _Alloc>::substr(typename _Alloc::size_type, typename _Alloc::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((unsigned int)i), 0u)' " shows up.

What am I missing?

#include<iostream>
using namespace std;

int main()
{
int i, frequency = 0;
string str = "";
char ch = 0;

cout << "Enter a message: ";
getline(cin, str);

cout << endl;

{
cout << "Enter a character to be found: ";
cin >> ch;
}

while(i < str.size())
{
if(ch == str.substr(i,0))
++frequency;
}

cout << "The character '" << ch << "' appeared " << frequency << " time(s) in the message.";

cout << "\n\n";
system("pause");
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

int main()
{
    std::string message ;
    std::cout << "Enter a message: ";
    std::getline( std::cin, message );

    std::string substring ;
    std::cout << "Enter a substring or a character to be found: ";
    std::getline( std::cin, substring );

    if( !substring.empty() )
    {
        int frequency = 0 ;

        // http://www.cplusplus.com/reference/string/string/find/
        std::size_t pos = message.find(substring) ; // locate the first occurrence

        while( pos != std::string::npos ) // if found (ie. pos is a valid position in the string)
        {
            ++frequency ; // increment the count
            pos = message.find( substring, pos+1 ) ; // and locate the next occurrence
                                                     // (ie. the first occurrence starting from position pos+1)
        }

        // a string with size == 1 is just a single character
        std::cout << "the " << ( substring.size() == 1 ? "character" : "substring" ) << " '"
                  << substring << "' appeared " << frequency << " time(s).\n" ;
    }
}
I wanted to refrain from giving the OP the answer, but since someone has already posted an answer, this is the one that I had.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<iostream>
#include<string>
using namespace std;

int main()
{
    int i, frequency = 0;
    string str = "";
    string ch = ""; // Change this back to string, so you can compare the strings to each other

    cout << "Enter a message: ";
    getline(cin, str);

    cout << endl;


    cout << "Enter a character to be found: ";
    cin >> ch;


    for(i = 0; i < str.size(); ++i) // Change this into a for loop so you go through the whole string
    {
        //http://www.cplusplus.com/reference/string/string/substr/
        if(ch == str.substr(i,ch.size())){ // Checks if the substring is equal to the chararacter
            ++frequency;
        }
        cout << "substring: "<< str.substr(i,ch.size()) << '\n'; // Use this to see how the program is getting the substring.
    }

    cout << "The character '" << ch << "' appeared " << frequency << " time(s) in the message.";

    cout << "\n\n";
    system("pause");
    return 0;
}
Last edited on
Thank you so much, JLBorges, what you sent is the closest to what I have in mind!

Just one more thing though, what I have in mind is this:
--------------------------------------------------------------------
Enter a message: good morning!

Enter a substring or a character to be found: zzzz
Enter a substring or a character to be found: asd
Enter a substring or a character to be found: zxc
Enter a substring or a character to be found: o
The character 'od' appeared 1 time(s) in the message.
--------------------------------------------------------------------
^ This is what we want to happen. The program doesn't stop prompting the user to enter another substring or character until the user enters a letter that matches any letter within the message, it prompts again and again and again, until it detects that it matches.



In our current case, this happens:
--------------------------------------------------------------------
Enter a message: good morning!

Enter a substring or a character to be found: zzzz
The substring 'zzzz' appeared 0 time(s) in the message.
--------------------------------------------------------------------
What we want to happen is the one above

I know that I should use loop, but I tried every loop I know and I must be missing something, again. Thank you in advance :)


Edit:
May I know what the substring.empty() is for? in "if( !substring.empty() )"
And the ::npos in "while( pos != string::npos )"
Our instructor hasn't taught us that yet. :)
Last edited on
Thank you, Hengry! :)
I understand where you're coming from, about not giving away the answer, but our exams are coming up and I just need to know how this program works so I can study and analyze it. This was a programming activity we had last week(which I didn't get to answer correctly), and our instructor never gave away the codes after.

P.S. I'm a civil engineering major and I never envisioned programming to be a part of our curriculum and careers but I'm trying my best to understand it. :)
Last edited on
> May I know what the substring.empty() is for? in "if( !substring.empty() )"

empty() returns true if the string does not contain any characters. ie. if its size() is zero.
http://en.cppreference.com/w/cpp/string/basic_string/empty
http://en.cppreference.com/w/cpp/string/basic_string/size


> And the ::npos in "while( pos != string::npos )"

std::string::npos is a special value returned by find() when it does not find what we are looking for.
(If found, find() returns the position at which the sub-string/character was found, so std::string::npos represents an invalid position.)
http://en.cppreference.com/w/cpp/string/basic_string/npos
http://en.cppreference.com/w/cpp/string/basic_string/find

Get into the habit of looking up the C++ reference documentation.
http://www.cplusplus.com/reference/
http://en.cppreference.com/w/cpp


> I know that I should use loop, but I tried every loop I know and I must be missing something

When we want to do something at least once, and keep repeating it till some condition is true, the canonical loop to use is a do-while loop.
http://en.cppreference.com/w/cpp/language/do

For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    int number ;

    do // execute the following block statement at least one
    {
        std::cout << "enter a positive integer: " ;
        std::cin >> number ; // for now, we will assume that the user actually types in an integer
                             // that there is no input error
    }
    while( number < 1 ) ; // repeat the block statement (ask the user again, and accept another number)
                          // if the number entered is non-positive.

    std::cout << "you entered " << number << '\n' ;
}
Topic archived. No new replies allowed.