Im getting this error in if-condition for string. I cant figure out why

I'm practicing strings and and i can't figure out the error coming at line 31.

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
36
37
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string str1;
	
	getline(cin , str1);
	cout << str1 << endl;
	
	string underscore;
	
	for(int i = 0; i != str1.length(); i++)
	{
		if(str1.at(i) == ' ')
		{
			underscore += " ";
		}
		else
		{
			underscore += "_";
		}
	}
	cout << underscore << endl;
	
	string find;
	getline(cin , find);
	
	for(int j = 0; j != str1.length(); j++)
	{
		if(str1.at(j) == find)
		{
			underscore.at(j) = find;
		}
	}
	cout << underscore;
}
Last edited on
Im getting this error in if-condition for string.

.. and that error is...?


I suspect it's because .at() returns a character and you're comparing it to a string called find. find needs to be a char as on line 15
str.at(j) is a char. find is a string. A char is not a string.
How is it a char? The data type of str1, underscore and find is string.
Can you say what the member function .at() does?
Topic archived. No new replies allowed.