Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I am working in our project in school. Our project is about ATM.

I first put asterisk while inputing the user's PIN, just like in ATM.

but since it is a char I can't compare it to numbers like

if (pin >= 0) ... it won't work. Can you help me?

What I have tried:

char pin[4];
string pass = "afgt";
int p;
int i=1;

cout << "ENTER PIN\t::\t";
if ((pin >= 0) && (pin <= 9)){
for (i=1;i<=4;i++){
pin[i]=getch();
cout << "*";
}
}

this one....
Posted
Updated 29-Oct-18 21:56pm

you should learn to use the debugger. That will help you learn by finding your own errors.

As it is, you are saving the user's input in the variable within the IF snippet.
So, if the variable has not been saved yet, how is the if ((pin >= 0) && (pin <= 9)) statement going to match?

Secondly, what happens if the user inputs something wrong (e.g. a letter)? As it is, the program would end without giving any reason.

I recommend you to think what you want to achieve, put it in paper and then try to translate it into code. That should make it a bit easier for you to order your ideas.
 
Share this answer
 
v3
Comments
CPallini 30-Oct-18 3:43am    
5.
Nelek 30-Oct-18 7:11am    
Thanks Carlo
C++
if ((pin >= 0) && (pin <= 9)){

You have declared pin as an array of 4 characters so you can not compare it to a number. You should check each character in turn like:
C++
pin[i] = getch();
if (pin[i] < ‘0’ || pin[i] > ‘9’)
{
    // error message here
}
 
Share this answer
 
Comments
CPallini 30-Oct-18 4:18am    
But if you don't allow the user to enter alphabetic characters, how could he possibly match the password?
:-)
Richard MacCutchan 30-Oct-18 11:15am    
If the password is letters and the pin is numbers, the criminals will never get it.
CPallini 30-Oct-18 12:08pm    
Brilliant! :thumbsup:
As matter of fact, the
Quote:
f ((pin >= 0) && (pin <= 9))
is plain wrong (other than useless).
It looks you're allowed to use C++ strings. If you are also allowed to use modern C++ features then try:
C++
#include <iostream>
#include <array>
using namespace std;


int main()
{
  using Code = array<char, 4>;

  const Code pwd{'a','f','g','t'};
  Code pin;
    
  cout << "please enter the pin\n";
  for (auto & c : pin)
    cin >> c;
  
  if ( pwd == pin)
    cout << "that's OK\n";
  else
    cout << "sorry, no luck\n";
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900