Compiling Error Problem: Could not conver string to bool

Here is the exact compiler error:

error: could not convert 'user_input.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits<char>, std::allocator<char> >(((const char*)"start"))' from 'std::basic_string<char>' to 'bool'|

user_input has been declared a string in my header file:
1
2
3
4
5
6
7
8
9
 #ifndef _DECLARATIONS_H_INCLUDED
#define _DECLARATIONS_H_INCLUDED


std::string user_input;



#endif 


Here is the code from main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()

{


std::cout<<"Tye the word \"start\" to begin: ";
std::getline(std::cin, user_input);

if(user_input = "start")
{
    std::cout<<"started properly. . user_input var is working. . . move on"<<std::endl;
}


}


I realize this code is meaningless, but I'm starting small and testing everything constantly before I build more on it.

I had received this before and was able to fix it. . . somehow. I don't remember how I fixed it. Also, I've tried using "start" and 'start' but get the same result both ways. I always get it mixed up which I'm supposed to use in that regard.

Any help would be appreciated.
Welp, nevermind. I fixed it myself. LOL

Turns out I should've used (user_input == "start") instead of just one '='. Derp.

Anyway, that makes me curious -- why does it try to convert it to 'bool' when using just one '='?
Last edited on
It's always good practice to debug things yourself!

Because the if statement takes a bool as input:

1
2
if(1) DoThis();
if(!1) DoThat();


The equation will evaluate to a "Yes or No", so in your example with the std::string:

1
2
user_input = "Start"; // Assigns Start to user_input
if(user_input == "Start") // Start is Equal to Start, return 1, true, yes 
Ahhh. . . I see. Thank you so much! :)
Topic archived. No new replies allowed.