Hi All,
I'm trying to create a password program that does the checking in a separate function.
Apologies for the formatting, I don't know how to indent my code within the post.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
#include <string>
using namespace std;
bool username ( string u )
{
if ( u == "meyou" )
{
return true;
}
else if ( u != "meyou" )
{
return false;
}
}
bool password ( string p )
{
if ( p == "stuff" )
{
return true;
}
else if ( p != "stuff" )
{
return false;
}
}
string u;
string p;
int main ()
{
cout << "Enter username: ";
cin >> u;
cout << "Enter password: ";
cin >> p;
username (u);
password (p);
if (username (true) && password (true))
{
cout << "Access granted!";
}
else if (username (false) || password (false))
{
cout << "Access denied!";
}
}
|
I get the following compile error at line 40 - if (username (true) etc...
could not convert 'true' from 'bool' to 'std::string {aka std::basic_string<cha...
Same result if I comment out the final "else if" statement.
However, when I comment out the final "if (username etc..." and leave the "else if...", the program compiles and runs, but the terminal complains of a:
terminate called after throwing an instance of 'std::logic_error'
I've tried this on CodeBlocks and Xcode.
Amusingly, it suggests I contact the application's support team, which I guess would be me.
In the end I know this is a PEBKAC error but I've re-read the Jumping Into C++ chapter half a dozen times (no really) and I just can't see where I'm going wrong.
I'd be thankful for any clues someone can give me.