frandarre
frandarre

Reputation: 11

C++ Could not convert input string when using cin and variables

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

Answers (3)

Mustafa Khaled
Mustafa Khaled

Reputation: 5

use == instead of =
== is for comparison
= is for assingment

Upvotes: 1

nils
nils

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

dwcanillas
dwcanillas

Reputation: 3651

if(input = "Micaela") is an assignment. You want if(input == "Micaela") instead, which is a comparison.

Upvotes: 0

Related Questions