If, Else statements help

I just started learning C++ last week. I'm trying to write a simple code using if and else statements, apparently there is an error in my code and I can't identify it. Any help would be appreciated. And yes I tried google.

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;
int main ()
{
cout << "What is your name?  ";
char name [9];

cin >> name;

if (name == steve)
{cout << "Welcome Boss\n";}
else
  {cout << "Fuck off\n";}
return 0;

}
Last edited on
Any compiler would tell you pretty quickly what the error is (line 11 btw)

What is the value of variable steve? That's what the compiler is thinking, even though you know you want it to be "steve"

After you fix that, here's another question to consider. What happens if I type in "Steve"?
Sorry, I don't understand. I declared char = Steve and still got an error
A variable of type char can only store just that: a single character. If you're just starting C++ and you're self-learning, I would highly suggest either getting a book as outlined here:
http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

I would also start with your basic data types, such as int, string, char, bool, and not container objects (even though string is a container, and you'll learn why).

Btw steve != "steve". Think about that for a bit. You'll probably understand why you're getting the error.
Thanks for that tip. Here is the updated code. *sigh* I still have a lot to learn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
#include <string.h>
using namespace std;

int main ()
{
cout << "What is your name?  ";
char name [9];

cin >> name;


if(strcmp(name,"Steve") == 0)
{cout << "Welcome Boss\n";}
else
  {cout << "Fuck off\n";}
return 0;

} 
Topic archived. No new replies allowed.