-5

I want to make a program that would talk with someone but I have started learning C++ only 2 days ago so now I don't understand what to do to get rid of this error. I want program to "cout" Hello if someone typed "Hello" but it doesn't work :( It says:

A value of type "const char*" cannot be assigned to an entity of type "int".

I tried to change "int" to char, long long, unsigned char, unsigned long long, unsigned int but it didn't work :D

#include <iostream>
#include <conio.h>
using namespace std;

int main() {
    int Answer;
        cout << "Hello! I'm a new born AI.";
        cin >> Answer;
        if (Answer = "Hello") //ERROR IS HERE {
            cout << "Hello" << endl;

        }

        }

        _getch();
        return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    `Answer` is an `int` (it can only contain numbers) and you want to compare it to a string, what do you expect? – Jabberwocky Feb 21 '18 at 15:56
  • `Answer = "Hello"` makes no sense at all. Even if you were to do a comparison instead of an assignment – UnholySheep Feb 21 '18 at 15:56
  • Probably: `int Answer;` -> `string Answer;` and `Answer = "Hello"` -> `Answer == "Hello"` – Jabberwocky Feb 21 '18 at 15:57
  • 6
    I would recommend you start with [a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – UnholySheep Feb 21 '18 at 15:58
  • 3
    Trying to do these kind of programs after only two days learning C++ isn't a good idea. For example, you're using an assignment instead of a comparison. Also you can't compare a string with an integer in the way you did. And finally, isn't a good idea to use an ancient, non-standard library as ``. Better spend some time reading a good C++ book. – Xam Feb 21 '18 at 15:59

2 Answers2

3
int Answer;

this states that Answer is a variable of type signed fixed bit count integer (typically 32 bits).

cin >> Answer;

this reads an integer from input and stores it in Answer.

"Hello" is a constant array of characters.

Answer = "Hello"

here = is assignment, not comparison. == is comparison in C++.

Variables have types in C++. You cannot store an array of characters in an int. When that fails, it tries storing a pointer to the array of characters; that also fails. It gives you an error.

Try making Answer a std::string instead of an int, and using == instead of = to compare it to "Hello".

Your code will at least compile.

Live example.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
0

First, in c++ when you want to compare two objects/structs/literals, you use double equal ==, not single equal =. Single equal is to assing a value, and unlike other programming languages where the meaning of = changes if you are in an "if" condition, in C++ you are assigning a value and returning that value to be evaluated by the condition.

Second, when you want to put text into a variable, you don't use and int variable like you are doing with "Answer", you use for example a std::string.

Jorge Y.
  • 1,123
  • 1
  • 9
  • 16