Coding an ATM

I need help, like, my teacher wanted us to code a simple ATM, and he wanted us to somehow make the pincode that the user enters to come out as * but I don't know how to do such a thing. He didn't teach us how to do it, he said to figure it out through our own means of doing so, so now I'm seeking help here.

I'm done with the rest of the code though, just that one part left.

THANKS!

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;

int main ()
{
	int a;
	float b = 10000;
	int c;
	
	cout<< "***********************************************************\n";
	cout<< "*************Welcome to our simple ATM Machine*************\n";
	cout<< "***********************************************************\n";
	cout<< "Enter your pincode please: ";
	cin>> a;
	if (a==1234)
	{
		cout<< "Success!\nAccessing your data..........................";
	}
	annon:
	cout<< "\n\nPlease select one of three options:\n";
	cout<< "[1] Balance Inquiry\n[2] Withdraw\n[3] Deposit\n\n";
	cout<< "Option selected: ";
	cin>> a;
	
	if (a==1)
	{
		cout<< "\nYour current balance is: " << b;
	}
	else if (a==2)
	{
		cout<< "\n\nPlease enter how much you would like to withdraw: ";
		cin>> c;
		if (c>b)
		{
			cout<< "\nError, insufficient balance.";
		}
		else
		{
		b=b-c;
		cout<< "\nTransaction successful, your current remaining balance is: " << b;
		}
	}
	else if (a==3)
	{
		cout<< "\n\nPlease enter how much you would like to deposit: ";
		cin>> c;
		if (c<0)
		{
			cout<< "\nError, you can't 'deposit' a negative amount.";
		}
		else
		{
			b=b+c;
			cout<< "\nTansaction successful, your current balance is now: " << b;
		}
	}
	else
	{
		cout<< "\n\nInvalid choice.";
		goto annon;
		return 0;
	}
	
	cout<< "\n\nWould you like to do something else?\n";
	cout<< "Select an option:\n\n";
	cout<< "[1] Yes\n[2] No\n\n" << "Option selected: ";
	cin>>c;
	if (c==1)
	{
		goto annon;
	}
	else
	{
		cout<< "\nThank you for using our simple ATM!";
		return 0;
	}
}
Last edited on
some out as *

Do you mean come out as *? Meaning that when the user enters the PIN code, the computer should display * instead of the number?

Or do you mean sum out as * meaning that the sum of the digits should always equal the ASCII value for '*', or something else?

BTW, goto's should be avoided if at all possible. You should replace
1
2
3
4
5
annon:
...
if (c==1) {
    goto annon;
}

with
1
2
3
do {
...
} while (c==1);
1. Don't use goto It's terrible programming practice and only brings headache. Use the better loops - while loops, do-while loops and for-loops.

2. Why are you using the same variable for pincode and menu choice? The pincode will be overwritten in that case.

3. It looks like the pincode currently has to be 1234 otherwise it is not succesful, so you can just manually print out ****.

4. If you intend to change it so the pincode can be anything, then this is a way of doing it.

1
2
3
4
5
6
7
8
9
10
11
std::string pincode; // Store the pincode in a string, not an integer.
	cin >> pincode; // User inputs pincode.
	int pincodeLenght = pincode.size(); // This  will store the lenght of the pincode in an integer, if the pincode is "Hello123" pincodeLenght will be equal to 8. 
        
	std::string hiddenPincode; // Create a new variable to hold the star version of the pincode ****

	for (int i = 0; i < pincodeLenght; i++) // Run this loop the pincodes lenght amount
	{
		hiddenPincode += "*"; // For every character in the pincode, add a *
	}
	cout << hiddenPincode << endl; // this is just to print it out to see if it works  


5. A short version of b = b + c or b = b - c is b += c or b -= c
Last edited on
Do you mean come out as *?

Yeah, I did.

I meant, the pincode as it is being typed is showing as asterisks.

Thanks in advance.

@Tarik, it seems that it wasn't exactly what I was looking for. But thanks anyway.
Last edited on
There's no standard way to do this, it is platform-specific. Check out this link about doing it with nothing shown on the screen.
http://stackoverflow.com/questions/1413445/read-a-password-from-stdcin

You will probably have to take input one char at a time and manually print an asterisk.
An update:
The teacher now wants it so that only numbers will be accepted for the pin.
Yet again, he didn't teach us, so please tell me how to do it.

And I forgot to say, thanks Tarik, the code you gave me turned out useful.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include <windows.h>
#include <unistd.h>

using namespace std;

int addition (int d, int e)
{
	int f;
	f = d + e;
	return f;
}
int subtraction (int g, int h)
{
	int j;
	j = g - h;
	return j;
}
int main(void)
{
	int a;
	float b = 10000;
	int c;
	HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE); 
    DWORD mode = 0;
    GetConsoleMode(hStdin, &mode);
    SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
	string s;
	std::string pincode;
	
	retry:
	cout<< "***********************************************************\n";
	cout<< "*************Welcome to our simple ATM Machine*************\n";
	cout<< "***********************************************************\n";
	cout<< "\nEnter your pincode please: ";
   
	getline(cin, pincode);
    int pinlength = pincode.size();
    std::string pin;
    for (int i = 0; i < pinlength; i++)
    {
		pin += "*";
	}
	cout << pin << endl;
	
	cout<< "\nSuccess!\nAccessing your data..........................";
	sleep(3);
	system ("CLS");
	
	repeat:
	cout<< "\n\nPlease select one of three options:\n";
	cout<< "[1] Balance Inquiry\n[2] Withdraw\n[3] Deposit\n\n";
	cout<< "Option selected: ";
	cin>> a;
	system ("CLS");
	
	if (a==1)
	{
		cout<< "\nYour current balance is: " << b;
	}
	else if (a==2)
	{
		cout<< "\nPlease enter how much you would like to withdraw: ";
		cin>> c;
		system ("CLS");
		if (c>b)
		{
			cout<< "\nError, insufficient balance.";
		}
		else if (c<=0)
		{
			cout<< "\nError, the amount cannot be less than 1.";
		}
		else
		{
		b = subtraction (b, c);
		cout<< "\nTransaction successful, your current remaining balance is: " << b;
		}
	}
	else if (a==3)
	{
		cout<< "\nPlease enter how much you would like to deposit: ";
		cin>> c;
		system ("CLS");
		if (c<=0)
		{
			cout<< "\nError, the amount cannot be less than 1.";
		}
		else
		{
			b = addition (b, c);
			cout<< "\nTansaction successful, your current balance is now: " << b;
		}
	}
	else
	{
		cout<< "\n\nInvalid choice.";
		goto repeat;
		return 0;
	}
	
	cout<< "\n\nWould you like to do something else?\n";
	cout<< "Select an option:\n\n";
	cout<< "[1] Yes\n[2] No\n\n" << "Option selected: ";
	cin>>c;
	system ("CLS");
	if (c==1)
	{
		goto repeat;
	}
	else if (c==2)
	{
		cout<< "\n\nThank you for using group 2's simple ATM Machine!";
		return 0;
	}
	else
	{
		cout<< "\n\nInvalid option chosen.";
		return 0;
	}
}
One option:

Read the input character by character and check that it contains only digits.

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
#include <iostream> 
#include <conio.h>
#include <string>

using namespace std;


int main ()
{
  const int CR = 13;

  int ch;
  string buffer;

  do
  {
    ch = _getch();
    if (isdigit(ch))
    {
      buffer += (char)ch;
      cout << "*";
    }
  }while (ch != CR);
  cout << "\n\nPin = " << buffer << endl;
  system("pause");
}
Thanks, though since you said that's one option, what are the other options?
Another option is to read the input into a string like you do now and check that the string only contains digits.
Thanks for all you guys' help.
Topic archived. No new replies allowed.