bool class method?

Mar 26, 2012 at 12:44am
Hello, everyone!

I am creating a C++ program that simulates an ATM program.
I want to create a method that will check and see if the PIN# entered is correct or not.
If not, I would like the program to end.

The instructions for the assignment say "A method to prompt the user to enter a PIN# and verify that it is the correct one, returning TRUE or FALSE."

I believe this means I have to use a bool type.

Please help? I have no idea how to do this.
Mar 26, 2012 at 4:37am
bool is like any other return value, it is just limited to either true or false, much like int is constricted to only int values. so you would do

if((your function here)){
//do whatever you want
}
else {
// do other stuff
}

your function

bool functionname(/*whatever you want here*/, int pin){
const int size = 10;
(your type) array[size]; //i using is compared to an array
for(int i = 0; i < size; i++){
if(array[i] == pin) return true;
}
return false;
}


i really like to use bool functions for things like this.
Last edited on Mar 26, 2012 at 4:37am
Mar 26, 2012 at 11:05am
Thank you!!!!
Mar 26, 2012 at 11:39am
The presented function

bool functionname(/*whatever you want here*/, int pin){
const int size = 10;
(your type) array[size]; //i using is compared to an array
for(int i = 0; i < size; i++){
if(array[i] == pin) return true;
}
return false;
}

can be rewritten with using standard algorithms. For example

1
2
3
4
5
6
7
bool functionname(/*whatever you want here*/, int pin)
{
   const int size = 10; 
   (your type) array[size]; //i using is compared to an array

   return ( std::find( array, array + size, pin ) != array + size );
}


Or even more simply

1
2
3
4
5
6
7
bool functionname(/*whatever you want here*/, int pin)
{
   const int size = 10; 
   (your type) array[size]; //i using is compared to an array

   return ( std::any_of( array, array + size, []( (your type ) x ) { x == pin; } );
}


To use standard algorithms you should include in your program the header
<algorithm>
Last edited on Mar 26, 2012 at 11:40am
Mar 26, 2012 at 12:32pm
closed account (zb0S216C)
I suggest something simple. Like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool Enter_pin(void); // 'void' is optional.

int main(void)
{
    return(0);
}

bool Enter_pin(void)
{
    char Correct_pin[5] = "0101";
    char Given_pin[5] = { 0 }; // A

    // Get the pin from the user
    std::cin.read(Given_pin, 4); // B
 
    // Compare the given pin with the correct pin
    if(!std::strcmp(Given_pin, Correct_pin)) // C
        return(true);

    else return(false);
}

Program notes:

A) The way the braces are used here is called an initialiser list. When a single value is placed within the braces, the first elements is set to that value, but the remaining elements are set to 0(zero). However, if too few values are placed within the list, the remaining values are set to 0 (zero). Don't forget that when you create a character array, make sure the array is 1 character larger (for the null-character).

B) std::istream::read()[1] is a function that extracts n bytes from the input buffer and places them within the array. In this case, 4 bytes (or characters) are read from the input buffer and placed within Given_pin.

C) std::strcmp()[2] is a function within the <cstring> header. It compares two strings together. It returns zero if the strings match; non-zero otherwise. I guess you're wondering why you can't do this:

if(Given_pin == Correct_pin)

The reason why is because you won't be comparing strings, but the address of the strings. Unless the strings are situated at the exact same place in memory, the comparison will always be false. Another reason is because the compiler's doesn't know the length of the arrays.

I hope I didn't confuse you.

References:
[2] http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
[1] http://www.cplusplus.com/reference/iostream/istream/read/


Wazzak
Last edited on Apr 17, 2012 at 11:31pm
Mar 26, 2012 at 12:57pm
Framework (1994)
your example is bad.
First of all in C++ main function is declared as

int main() instead of int main( void )

Secondly your condition statement

1
2
if(std::strcmp(Given_pin, Correct_pin)) // C
        return(true);


contains a bug because instead of it your should write

1
2
if(std::strcmp(Given_pin, Correct_pin) == 0 ) // C
        return(true);


And it would be even better to write only one statement with return

return ( std::strcmp(Given_pin, Correct_pin) == 0 );


So your example is very bad and demonstrates how programs should not be written!
Last edited on Mar 26, 2012 at 12:59pm
Mar 26, 2012 at 1:14pm
closed account (zb0S216C)
vlad from moscow wrote:
First of all in C++ main function is declared as

int main() instead of int main( void )

No, it's fine. void within the parentheses indicates that the function takes no arguments -- it's the same story with a function with an empty parameter list. Did you know that? Apparently not.

vlad from moscow wrote:
Secondly your condition statement

1
2
if(std::strcmp(Given_pin, Correct_pin)) // C
        return(true);



contains a bug because instead of it your should write

1
2
if(std::strcmp(Given_pin, Correct_pin) == 0 ) // C
        return(true);

Corrected.

vlad from moscow wrote:
So your example is very bad and demonstrates how programs should not be written!

That's it - I've had it. You're a broken record, aren't you? You say that to everybody if their program doesn't follow the "Vlad's C++ Coding Standard". I don't care if this is rude or not, it's the truth. Simply because my program had a minor mistake, you slam it and call it very bad. Either you need to get some manners, or re-evaluate your level of seriousness meter.

Wazzak
Last edited on Mar 26, 2012 at 1:34pm
Mar 26, 2012 at 1:41pm
Framework (1996)
No, it's fine. void within the parentheses indicates that the function takes no arguments -- it's the same story with a function with an empty parameter list. Did you know that? Apparently not.


Before to affirm something it is a good idea to look through the C++ Standard. And I do not know "stories" which you are mentioning, but I know paragraph 3.6.1 "Main function" :
All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }
and
int main(int argc, char* argv[]) { /* ... */ }


So it is better to follow the Standard, is not it?

And the problem is not that you incorrectly wrote the condition. Also it is a bad style that you are using two return statements where it is enough to use only one return statement.

So even such small code arises many questions to it.
Last edited on Mar 26, 2012 at 1:43pm
Mar 26, 2012 at 1:50pm
closed account (zb0S216C)
vald wrote:
Before to affirm something it is a good idea to look through the C++ Standard. And I do not know "stories" which you are mentioning, but I know paragraph 3.6.1 "Main function" :
All implementations shall allow both of the following definitions of main:
int main() { /* ... */ }

I'll quote myself, shall I:

Myself wrote:
void within the parentheses indicates that the function takes no arguments -- it's the same story with a function with an empty parameter list.


main() is no exception. main(void) == main()

vlad from moscow wrote:
Also it is a bad style that you are using two return statements where it is enough to use only one return statement.

Seriously? I could have said this: ((!std::strcmp(...)) ? true : false) Would you moan your bag off about that, too? The fact that you're dismissing perfectly acceptable code is shocking.

Wazzak
Last edited on Mar 26, 2012 at 1:53pm
Mar 26, 2012 at 2:16pm
Framework (1997)
main() is no exception. main(void) == main()


It is not so simple as you think. For example let assume that there is a class with a default constructor

1
2
3
4
struct A
{
   A(){}
};


The C++ Standard does not allow to write A( void ) {}.

Let return to the main function. When somebody read your code and see declaration

int main( void )

the first idea that will come is that the code is written in C. So the declarations

int main()
and
int main( void )

give different information for a reader of the code. So it is a bad idea in general to break this default assumption.

As for the return statement it is more simply and more clear to write as I already pointed out

return ( std::strcmp( Given_pin, Correct_pin) == 0 );


because using such constructions as

!std::strcmp(...)

can lead to various kind of errors. I know many cases when professional programmers made such errors when they used such forms of the expresion with strcmp.
Last edited on Mar 26, 2012 at 2:21pm
Mar 26, 2012 at 2:24pm
closed account (zb0S216C)
vlad from moscow wrote:
It is not so simple as you think. For example let assume that there is a class with a default constructor

1
2
3
4
struct A
{
   A(){}
};



The C++ Standard does not allow to write A( void ) {}.

VC++ & MinGW disagree.

vlad from moscow wrote:
the first idea that will come is that the code is written in C.

Until they see std:: or class.

vlad from moscow wrote:
int main()
and
int main( void )

give different information for a reader of the code. So it is a bad idea in general to break this default assumption.


There's absolutely no difference in meaning! How many times do I need to say this?

vald from moscow wrote:
!std::strcmp(...)

can lead to various kind of errors. I know many cases then professional programmers made such errors then they used such forms of the expresion with strcmp.

LOL! Errors? From !std::strcmp(...)? You must be joking! The result of a function is implicitly converted to a bool. That evaluation is perfectly acceptable. Don't correct what doesn't need correcting.
I'm curious, what errors were you referring to?

Wazzak
Last edited on Mar 26, 2012 at 2:30pm
Mar 26, 2012 at 2:44pm
Framework (2002)

Mar 26, 2012 at 6:24pm

vlad from moscow wrote:

It is not so simple as you think. For example let assume that there is a class with a default constructor

1
2
3
4
struct A
{
   A(){}
}; 



The C++ Standard does not allow to write A( void ) {}.

VC++ & MinGW disagree.

Your problem is that you do not know the C++ standard and start to argue. VC++ and MinGW contain numeros bugs and very often do not follow the C++ standard. So your references to VC++ & MinGW are not serious.

From the C++ Standard 12.1 Constructors #10

10 No return type (not even void) shall be specified for a constructor


Next time before you will affirm something please read the C++ standard! Otherwise you look like a ignoramus.

As for the second question


[quote]!std::strcmp(...)

can lead to various kind of errors. I know many cases then professional programmers made such errors then they used such forms of the expresion with strcmp.


LOL! Errors? From !std::strcmp(...)? You must be joking! The result of a function is implicitly converted to a bool. That evaluation is perfectly acceptable. Don't correct what doesn't need correcting.
I'm curious, what errors were you referring to?
[/quote]


you even do not understand about what kind of errors I am speaking.
Last edited on Mar 26, 2012 at 2:48pm
Mar 26, 2012 at 2:49pm
closed account (zb0S216C)
vlad from moscow wrote:
10 No return type (not even void) shall be specified for a constructor


Next time before you will affirm something please read the C++ standard! Otherwise you look like a ignoramus.

The fact that you're giving me irrelevant information makes me question the rest of your information. Everybody knows, except for beginners, that constructors & destructors don't return a value. Why you've deviated from parameter lists to return type is beyond me.

A note to Vlad for future posts: Validate your information before confronting people. You'll just end up making yourself look a like tool.

Wazzak
Last edited on Mar 26, 2012 at 2:49pm
Mar 26, 2012 at 2:50pm
10 No return type (not even void) shall be specified for a constructor


That says constructors have no return type. It says nothing about void being disallowed in a constructor parameter list to specify the empty parameter list.
Mar 26, 2012 at 3:03pm
People who write (void) ussually have a c background where it is needed. But does but does not make a difference in the execution of the program.
Mar 26, 2012 at 3:05pm
closed account (zb0S216C)
ui uiho wrote:
But does but does not make a difference in the execution of the program.


Thank You! I do have a mediocre sized background in C, but I just like the explicitness.

Wazzak
Last edited on Mar 26, 2012 at 3:05pm
Mar 26, 2012 at 3:54pm
@vlad: just to remind, the point of all of this, even if you're right, still, have to speak it out with a proper way. there's rules of how we should speak out our opinion. because we're respecting the others and made out our opinions no offensive-ly. this is a discussion and not some kinda arguing stage... it should be solved cool-headed way

and usually we're just want to give a simple example, not a serious one, sometimes... for example:
1
2
3
4
    if(!std::strcmp(Given_pin, Correct_pin)) // C
        return(true);

    else return(false);


it can be written as:
1
2
3
4
if(!std::strcmp(Given_pin, Correct_pin)) // C
        return(true);

return(false);


but why framework doesn't do that? it's because it wasn't somekinda serious project

the most important thing is, they could understand what we want to say, and thus, they can make a better program...

hope this helps, and this is no offense...
Topic archived. No new replies allowed.