Char Error

Hey, this is a beginner question. I have been doing this for awhile but stopped and need to be refreshed on this. Here is the code:
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
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
    char test[80];
    
    cin >> test;
    
    if(test == "word"){
            cout << "YAY" << endl;
            }
    else{
            cout << "NOO" << endl;
            }
    
    
    
    
    
    
    
    
    
    
    
    
    system("pause");   
}


There is no error, it runs fine, but it never gives me the right answer. How do I compare a char variable and a text string like "test"? I know using strings simplifies that, but I want to use char variables. How would I get the output to be YAY? Is this a syntax error or what? I know this is probably so easy; I am just mad I do not know what to do lol.
What you're doing there is effectively comparing pointers to pointers. Odd are, they aren't going to match. :/

Solution A: Use an std::string instead of char* or char[]. I'd highly suggest this over solution B. Note that you'll need to #include <string>. After changing the type, though, I don't think you'll need to change anything else.

Solution B: Use the strcmp() function in the C standard library, under <cstring>. You wouldn't get the other benefits of C++ strings with this solution, and the C++ solution looks a lot better.
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

-Albatross
So, then what would one use a char for? I have used them before, since they are just an array of characters, but I was just wondering. I knew string would work but am still intrigued by chars. I want to know more about them. I use them some, but rarely. Could you give me a good example? Sorry for being so noobish, but I just never looked into chars too deeply before.
Topic archived. No new replies allowed.