Reputation: 11
I'm trying to do a simple program where the user inputs a string with his/her name, then if the string is equal to a certain name, it executes different commands.
It's something like this:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string input = "";
cout << "What's your name?:\n>";
getline(cin, input);
if(input = "Micaela"){
cout << "You're the best" << input << endl << endl;
}
else
cout << "You kinda suck" << input << endl << endl;
return 0;
}
When compiling, I get the following error:
13 22 C:\Users\Francisco\Desktop\cpp\holanena.cpp [Error] could not convert 'input.std::basic_string<_CharT, _Traits, _Alloc>::operator=, std::allocator >(((const char*)"Micaela"))' from 'std::basic_string' to 'bool'
Upvotes: 0
Views: 491
Reputation: 2524
The problem occurs in the line
if(input = "Micaela")
which assigns "Micaela"
to input
. Use comparison operator ==
instead of assignment operator =
to get what you want:
if(input == "Micaela")
Upvotes: 0
Reputation: 3651
if(input = "Micaela")
is an assignment. You want if(input == "Micaela")
instead, which is a comparison.
Upvotes: 0