Need help with strings and if statments.

Hi, I am new here and I am trying to learn C++. I am trying to use if statements to switch between different ouptuts when the string in argument 3 is chosen. I do not understand why its not working. Could some one explain what is wrong with my if statement?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  

#include <iostream>
#include <string>
using namespace std;


double name( double x, double y, char str1) {
	
	if (str1 == 'Mike') {
		cout << "This is Mike" << endl;
cout << x+y << endl;
	}

	

}

Last edited on
well, first of all, you need a return statement or something, since this is a function that's being called by main, right? That's why it won't compile. Also, I think you meant to use a string type instead of a char, since you included <string>. If you did that, it would solve the problem of line 10, since that also returns an error, but you should also use a double quote there.
char str1 here, str1 is a single char, not word.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

void name( string str1) 
{	
	if (str1 == "Mike")  // true
	{
	cout << "This is Mike" << endl;
	}
}

int main()
{
	name("Mike");
	
	return 0;
}


EDIT: you can also use char array- in that case you have to compare every chars with "Mike" using a loop.
using string instead of char array as the example is simpler.
Last edited on
Thank you so much.
Topic archived. No new replies allowed.