(There's an error in the code when I run the program how can I fix this?)
(This is the question and the answer also answered but, there's an error, hope you could help) (Question #282822 in C++ for qq_576)
main.cpp:71:13: error: ‘_getch’ was not declared in this scope; did you mean ‘getch’?
71 | pin[i] = _getch();
| ^~~~~~
| getch
main.cpp:72:4: error: ‘_putch’ was not declared in this scope; did you mean ‘fputc’?
72 | _putch('*');
| ^~~~~~
| fputc
main.cpp:95:18: error: ‘_getch’ was not declared in this scope; did you mean ‘getch’?
95 | NEWpin[i] = _getch();
| ^~~~~~
| getch
main.cpp:96:6: error: ‘_putch’ was not declared in this scope; did you mean ‘fputc’?
96 | _putch('*');
| ^~~~~~
| fputc
main.cpp: In function ‘int main()’:
main.cpp:135:12: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
135 | ATM atm(0,"mad123");
| ^~~~~~~~
#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;
class ATM
{
double amount;
char PIN[7];
public:
ATM(double _amount, char _PIN[6]):amount(_amount)
{
int i;
for (i = 0; i < 6; i++)
{
PIN[i] = _PIN[i];
}
PIN[i] = '\0';
}
void CheckBalance()
{
cout << "\nYOUR CURRENT BALANCE IS: \nPhp " << amount;
}
void DepositCash()
{
double insAmount;
cout << "\nINSERT YOUR CASH: \nPhp ";
cin >> insAmount;
amount += insAmount;
}
void WithdrawCash()
{
double insAmount;
bool done = false;
do
{
cout << "\nINPUT THE AMOUNT OF WITHDRAWAL: \nPhp ";
cin >> insAmount;
if (insAmount > amount)
{
cout << "CASH NOT ENOUGH, TRY AGAIN";
}
else
{
amount -= insAmount;
done = true;
}
} while (!done);
}
void ChangePIN()
{
cout << "\nINPUT YOUR CURRENT PIN ";
char pin[7];
int i;
for (i = 0; i < 6; i++)
{
pin[i] = _getch();
_putch('*');
}
pin[i] = '\0';
if (strcmp(pin, PIN) != 0)
{
cout << "\nWRONG PIN!\n";
}
else
{
char ch;
do
{
cout << "\nINPUT YOUR NEW PIN ";
for (i = 0; i < 6; i++)
{
pin[i] = _getch();
_putch('*');
}
pin[i] = '\0';
cout << "\nVERIFY YOUR NEW PIN ";
char NEWpin[7];
for (i = 0; i < 6; i++)
{
NEWpin[i] = _getch();
_putch('*');
}
NEWpin[i] = '\0';
if (strcmp(pin, NEWpin) != 0)
{
cout << "\nWRONG NEW PIN!\n";
}
else
{
for (i = 0; i < 6; i++)
{
PIN[i] = pin[i];
}
}
cout << "\nENTER[Y/N]:";
cin >> ch;
if (ch == 'Y')
{
break;
}
} while (true);
}
}
};
void Menu()
{
cout << "[C]HECK BALANCE\n"
<< "[D]EPOST CASH\n"
<< "[W]ITHDRAW CASH\n"
<< "C[H]ANGE PIN\n"
<< "[E]XIT PROGRAM\n";
}
int main()
{
ATM atm(0,"mad123");
char pin[7];
char ch;
int attempt = 0;
do
{
cout << "******MAD AUTOMATED TALLER MACHINE IS ONLINE******\n";
cout << "INPUT YOUR 6 DIGIT PIN ";
int i;
for (i = 0; i < 6; i++)
{
pin[i] = _getch();
_putch('*');
}
pin[i] = '\0';
cout << "\nENTER[Y/N]:";
cin >> ch;
if (ch == 'Y')
{
if (strcmp(pin, "mad123") != 0)
{
cout << "WRONG PIN!\n";
attempt++;
cout << "You have only " << 3 - attempt << " attempts left!\n";
}
else
{
break;
}
}
} while (attempt<3);
if (attempt == 3)
return -1;
Menu();
do
{
cout<<"\nTRANSACTION: [C|D|W|H]: ";
cin >> ch;
switch (ch)
{
case 'C':
{
atm.CheckBalance();
break;
}
case 'D':
{
atm.DepositCash();
break;
}
case 'W':
{
atm.WithdrawCash();
break;
}
case 'H':
{
atm.ChangePIN();
break;
}
}
} while (ch != 'E');
}
#include <conio.h> won't work on os other than Windows
To run this program in other os install extra packages. For more detail visit
https://stackoverflow.com/questions/8792317/where-is-the-conio-h-header-file-on-linux-why-cant-i-find-conio-h
Comments
Dear Eva, please try to use dev-cpp for Windows
what compiler did you use to run this program?
Leave a comment