Comparing Char and Int

My program compiles fine and doesn't have any errors so I am confused as to what the issue might be.

I have a int, which is determined by the user via cin.

I have a char, which is a random word generated from an input file.

1. I want the program to display "The word you entered does match..." if the word entered by the user is the same as the random word.

2. I want the program to display "The word you entered does not match..." if the word entered by the user is not the same as the random word.

The code I'm using for number one is
if (char == "int") cout << "does match..."

The code I'm using for number two is
else if (char != "int") cout << "does not match..."

Basically the programs only outputs "does not match" whether or not it really matches. Even if it matches, it outputs does not match.

Is something wrong with my code?
Last edited on
Is something wrong with my code?

Impossible to tell without seeing it. Those two snippets you gave don't even make sense to me.
Last edited on
The second snippet was supposed to have "else" at the beginning, I have edited it.

I'm basically trying to compare two words and have the program output the appropriate phrase if the words match or not.

Example.

dog = dog "Output is words match"

dog = cat "Output is words don't match"

The program is outputting "words don't match" even though the code if (char == "int") cout << "does match..." is true. It should display "does match".

Sorry if my post is hard to understand.
char is not even a valid variable name. That shouldn't compile.

Maybe you should post enough code that compiles and replicates the issue?
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
#include <iostream>
#include <fstream> 


using namespace std;

int main()
{

        ifstream inputFile; // input file
        inputFile.open ("/tmp/word"); 

        char randomword[21], userword; 
        inputFile.getline (randomword, 20);

        cout << "Enter your word: ";
        cin >> userword;

        if (randomword == "userword"){
                cout << "The word you entered matches the random word."<< endl;
                }

        else if (randomword != "userword"){
                cout << "The word you entered does not match the random word." << endl;
                }
        return 0;
}


Ok there's my code. The program is basically supposed to have the user select a word then match it with the word in the "word" file. When I run my program it only displays only "The word you entered does not match the random word" even if the word matches. Any suggestions?

When I run my program numerous times. It will always display the same random word until I log out of the server and log back in. That's why I can anticipate what the word is going to be.
well, a char is one characters long

so I think it suppose to be
char userword[21];

and the if else seems akward
Why do you have to check twice ?
Don't use char arrays.

Use strings.

See my comments:
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
#include <string>  // <- include string

int main()
{

        ifstream inputFile; // input file
        inputFile.open ("/tmp/word"); 

/*  get rid of this
        char randomword[21], userword; 
        inputFile.getline (randomword, 20);
*/
// use this:
        string randomword, userword;
        getline( inputFile, userword );


        cout << "Enter your word: ";
        cin >> userword;

        if (randomword == userword){   // < -get rid of the quotes here
                cout << "The word you entered matches the random word."<< endl;
                }

        else /*if (randomword != "userword"){*/  // <- get rid of the entire condition here (it's redundant)
                cout << "The word you entered does not match the random word." << endl;
                }
        return 0;
}
Also please note that the usual C function for comparing strings is strcmp(), you can't compare character arrays using "==" (you can do that for c++ strings as std::string class have overloaded == operator).

Please read the documentation.
Last edited on
Topic archived. No new replies allowed.