Problem with boolean function

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.
make two bools like this:

bool buser = false, bpassword = false;

then call your function like this:

1
2
buser = username(u);
bpassword = password(p);


Your return from the fuction will change the bools to true or leave them false.

Then to check if they are both true, an if statement only checks if something is true or untrue:

if(buser && bpassword)


To check if it is false, you use the NOT operator !:

if(!buser && !bpassword)
Last edited on
you are passing true to the function instead you should be passing a string.

 
if(username (u) && password(p))
Many thanks to you both. Both solutions worked, although I can't see entirely how/why. I'm going to have to do much more work with these functions before I finally "get it" I think.
The basic structure of a function is

return_value ---- function --- (parameter)

in your example you are returning a boolean
calling the function username
expecting a parameter of string.

bool username ( string u )

you were passing true which is not a string.
Last edited on
CScrawl, just to elaborate on the function calls pata corrected; when you did these calls:

1
2
username (u);
password (p);


You didn't store their return values. So when you called them, the booleans where lost when the function ended. So when you do this now:

1
2
buser = username(u);
bpassword = password(p);


The functions are called, their boolean values returned and then stored in the buser and bpassword variables.

--
I'm a learner myself, so don't shoot me if... :)
Last edited on
I see, thanks for that Codeez. I'm going to tinker around with this some more before I move on to the next bit. Thanks again everyone, you've all been a great help. =)
if you only have two conditions for lets say where your wrote:
1
2
3
4
5
6
7
8
9
10
11

bool username ( string u )
{
if ( u == "meyou" )
{
return true;
}
else if ( u != "meyou" )
{
return false;
}


you can just write it:
1
2
3
4
5
6
7
8
9
10
11
bool username ( string u )
{
if ( u == "meyou" )
{
return true;
}
else 
{
return false;
}
}

because the else statement is just saying if my if statement condition isnt met do what is in my else statement

but since your doing bools just put it at a starting value of false in this case because it is checking if when user puts their input if it is true. SO unless they type it in properly it will already be set as false. (Hopefully my bad grammar is stating this properly :D )
Last edited on
Topic archived. No new replies allowed.