I've recently taken up learning programming, finding that Java just wasn't for me I've headed over to C++ which I've taken to like a duck to water. I'm working through a book I've got and getting really into it but I've hit a complete dead end that I can't seem to find a solution for anywhere.
Basically, I have some logical operators, namely && and || as you can see below which are given in the book's source code to explain boolean operators. I know how they work, but the curious thing is that they're not working in this snippet of code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << "Game Designer's Network\n";
int security = 0;
if (username == "guest" || password = "guest");
{
cout << "\nWelcome, guest.";
security = 1;
}
if (!security) // this checks for 'not' security, meaning security must havea false value, any number other than 0 in the security value will return true
cout << "\nYour login failed.";
} while (security = false);
return 0;
}
I'm using Code::Blocks with the GSS compiler that comes with it as standard and debugging/building the code returns the error:
D:\Programming C++\Designers Network\main.cpp|23|error: no match for 'operator&&' in 'std::operator== [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)(& username))), ((const char*)"S.Meier")) && password'|
Does the same further down the code for the || operator. I've already done some digging to make sure I wasn't having some kind of lack of sleep induced moment of stupidity and apparently && and || are the operators.
Any help would be appreciated as I am essentially completely new to programming and would like to find a way to stop my IDE from doing this.
if (username == "S.Meier" && password = "civilization")
Im new to c++ too, freshmen comp sci major at college... I am not sure if this will help, but I think the line above should be
if (username == "S.Meier" && password == "civilization")
Also you might want to make secutiry a bool? So you could just go
while (security)
^ Thats simpler becuase since security is a bool it can only be true or false. If it is true it will perform the loop, but if it is false it will skip over it. No need to say == true or false for this.
Oh wow. It was sleepless stupidity that caused the error, just not in the way I thought.
Any idea why the debugger threw up the && and || errors instead of noting the missing =?
Might help me understand why the debugger was so misleading, the error message it gave me was causing me to focus entirely on the operators instead of the actual error.
Needless to say, your contribution has given me the exact solution I needed and I'm now well on my way to completing that chapter of the book. Thankyou!
Well, I've got another conundrum with the same piece of code now.
It compiles and runs without throwing up an error but when you enter a username and password it simply displayed all the cout text ignoring the if statements completely.
I've proofread the whole code against the book one line at a time now (to make sure there's no silly little typo causing it) and it totally matches the book, so there must be something wrong with the code itself.
EDIT: Whoopsie! (Tobias Boon voice) - two of those if statements have a ; on the end of the line. Book has a two typos in the same snippet of code. Guess I need to spend more on my learning materials.