An assingment of nust. ATM machine software using file handling in C++
1. Our Modern ATM machine has the following Menu
a. Create new account
i. When user selects this option, ask for username and password. Create a new text file with his/her name. If user already exists then ask the user to enter another name.
b. Cash Withdraw
i. Ask the user the amount he/she wants to withdraw. Check his account if the user has that amount of money in his/her account. If the amount exceeds, a message should be display saying that please enter less amount.
c. Cash Deposit
d. Fill Cash in ATM
i. Ask for amount and add in ATM machine.
f. The menu should be displayed again after every action. E.g., After cash withdrawal ATM software should display the menu again instead of exiting.
a. Create new account
i. When user selects this option, ask for username and password. Create a new text file with his/her name. If user already exists then ask the user to enter another name.
b. Cash Withdraw
i. Ask the user the amount he/she wants to withdraw. Check his account if the user has that amount of money in his/her account. If the amount exceeds, a message should be display saying that please enter less amount.
c. Cash Deposit
d. Fill Cash in ATM
i. Ask for amount and add in ATM machine.
f. The menu should be displayed again after every action. E.g., After cash withdrawal ATM software should display the menu again instead of exiting.
#include<iostream>
#include<stdio.h>
#include<conio.h>
using namespace std;
void main(){
char a;
FILE *myFile;
char name[10];
char password[10];
char temp;
int id_no = 0;
int user_id = 0;
int all_bal[1000];
int acc_bal = 0;
int bal = 0;
int counter = 0;
bool vl = 0;
int char_matched = 0;
int atm_bal = 0;
while (true){
cout << "Select Option: "
<< endl << "1. Create New Account"
<< endl << "2. Withdraw Cash"
<< endl << "3. Cash Deposit"
<< endl << "4. Cash fill in atm"
<< endl << "5. Account Summary" << endl;
a = _getch();
system("cls");
switch (a){
case '1':
cout << "Enter Your Name: ";
cin >> name;
cout << "Enter Your Password: ";
do{
temp = _getch();
cout << "*";
password[counter] = temp;
counter++;
} while (temp != 13 && counter < 10);
counter--;
password[counter] = '\0';
counter = 0;
cout << endl;
cout << "Enter Opening balance:";
cin >> acc_bal;
if (acc_bal < 0){
acc_bal = 0;
}
vl = 0;
myFile = fopen("all_names.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-'){
if (name[counter] == temp){
char_matched++;
counter++;
}else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(name)){
vl = 1;
}
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
if (vl == 0){
myFile = fopen("all_names.txt", "a");
fprintf(myFile, "%s", name);
fprintf(myFile, "%c", '-');
fclose(myFile);
myFile = fopen("all_passwords.txt", "a");
fprintf(myFile, "%s", password);
fprintf(myFile, "%c", '-');
fclose(myFile);
myFile = fopen("all_accounts.txt", "a");
fprintf(myFile, "%d", acc_bal);
fprintf(myFile, "%c", '\n');
fclose(myFile);
cout << "Account created succesfully" << endl;
}
else {
cout << "Name Already In Use............." << endl;
}
break;
case '2':
cout << "Enter Your Name: ";
cin >> name;
vl = 0;
counter = 0;
char_matched = 0;
myFile = fopen("all_names.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-'){
if (name[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(name)){
vl = 1;
user_id = id_no;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
else {
cout << "account does not exists..." << endl;
}
if (vl == 0){
cout << "account does not exists..." << endl;
}
else {
cout << "Please enter your password" << endl;
do{
temp = _getch();
cout << "*";
password[counter] = temp;
counter++;
} while (temp != 13 && counter < 10);
counter--;
password[counter] = '\0';
counter = 0;
cout << endl;
vl = 0;
counter = 0;
char_matched = 0;
id_no = 0;
myFile = fopen("all_passwords.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-' && id_no == user_id){
if (password[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(password)){
vl = 1;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
if (vl == 1){
cout << "ACESS GURANTED***" << endl
<< "Enter Amount to withdraw: ";
cin >> acc_bal;
vl = 0;
counter = 0;
myFile = fopen("all_accounts.txt", "r");
if (myFile){
while (counter <= user_id){
counter++;
fscanf(myFile, "%d", &bal);
}
myFile = fopen("atm_balance.txt", "r");
if (myFile){
fscanf(myFile, "%d", &atm_bal);
} else {
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
}
fclose(myFile);
if(bal < acc_bal){
cout << "You Have only: " << bal << "$" << endl;
}
if(acc_bal > atm_bal && bal >= acc_bal) {
cout << "Machine Have only: " << atm_bal << "$" << endl;
}else if(acc_bal <= atm_bal && bal >= acc_bal){
myFile = fopen("all_accounts.txt", "r");
counter = 0;
while (fscanf(myFile, "%d", &all_bal[counter]) == 1){
counter++;
}
fclose(myFile);
all_bal[user_id] -= acc_bal;
myFile = fopen("all_accounts.txt", "w");
id_no = 0;
while (counter != 0){
fprintf(myFile, "%d", all_bal[id_no]);
fprintf(myFile, "%s", "\n");
counter--;
id_no++;
}
fclose(myFile);
atm_bal -= acc_bal;
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
fclose(myFile);
cout << "Take your money. THankx.." << endl;
}
counter = 0;
char_matched = 0;
}
} else{
cout << "Acess Denied.." << endl;
}
}
id_no = 0;
user_id = 0;
break;
case '3':
cout << "Enter Your Name: ";
cin >> name;
vl = 0;
counter = 0;
char_matched = 0;
myFile = fopen("all_names.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-'){
if (name[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(name)){
vl = 1;
user_id = id_no;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
else {
cout << "account does not exists..." << endl;
}
if (vl == 0){
cout << "account does not exists..." << endl;
}
else {
cout << "Please enter your password" << endl;
do{
temp = _getch();
cout << "*";
password[counter] = temp;
counter++;
} while (temp != 13 && counter < 10);
counter--;
password[counter] = '\0';
counter = 0;
cout << endl;
vl = 0;
counter = 0;
char_matched = 0;
id_no = 0;
myFile = fopen("all_passwords.txt", "r");
if (myFile){
while (fscanf(myFile, "%c", &temp) == 1){
if (temp != '-' && id_no == user_id){
if (password[counter] == temp){
char_matched++;
counter++;
}
else {
char_matched = 0;
counter = 0;
}
}
if (temp == '-'){
if (char_matched == strlen(password)){
vl = 1;
}
id_no++;
counter = 0;
char_matched = 0;
}
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
if (vl == 1){
cout << "ACESS GURANTED***" << endl
<< "Enter Amount to deposit: ";
cin >> acc_bal;
vl = 0;
counter = 0;
myFile = fopen("all_accounts.txt", "r");
if (myFile){
while (counter <= user_id){
counter++;
fscanf(myFile, "%d", &bal);
}
myFile = fopen("atm_balance.txt", "r");
if (myFile){
fscanf(myFile, "%d", &atm_bal);
} else {
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
}
fclose(myFile);
if(acc_bal <0){
myFile = fopen("all_accounts.txt", "r");
counter = 0;
while (fscanf(myFile, "%d", &all_bal[counter]) == 1){
counter++;
}
fclose(myFile);
all_bal[user_id] += acc_bal;
myFile = fopen("all_accounts.txt", "w");
id_no = 0;
while (counter != 0){
fprintf(myFile, "%d", all_bal[id_no]);
fprintf(myFile, "%s", "\n");
counter--;
id_no++;
}
fclose(myFile);
cout << "CASH DEPOSITED. THankx.." << endl;
}
fclose(myFile);
counter = 0;
char_matched = 0;
}
} else{
cout << "Acess Denied.." << endl;
}
}
id_no = 0;
user_id = 0;
break;
case '4':
cout << "Enter Amount to add: ";
cin >> bal;
myFile = fopen("atm_balance.txt", "r");
if (myFile){
fscanf(myFile, "%d", &atm_bal);
} else {
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
}
fclose(myFile);
atm_bal += bal;
myFile = fopen("atm_balance.txt", "w");
fprintf(myFile, "%d", atm_bal);
fclose(myFile);
break;
default:
break;
}
cout << "Press any key to continue....";
_getch();
system("cls");
}
}
Thursday, 29 January 2015
Posted by Unknown
An assignment of nust on pointer in C++. copying, concatinating and searching string using pointers
1.
Create a menu
with following options
a.
Give Input
i.
Takes input from
user and saves it in the first array
b.
Copy a string
i.
User will enter a
string and it will be copied in both the arrays
c.
Concat string
i.
When concat
string option is selected, concatenate the string in the first array to second
array
d.
Search String
i.
Search first
array for the string entered by the user and only print true/false if found/not
found
e.
Display String
i.
Display the
second array to user when this option is selected.
2.
For above options
your program will have six functions in total including main.
3.
Create two
character arrays of size 1000 in main function.
4.
Every time user
selects an option pass both the arrays to functions in the form of pointers.
5.
Use pointers to
traverse and manipulate the arrays.
6.
Create a
temporary array inside any function is allowed.
//C++ program to copy cancatenate search and display string through pointers
#include<iostream>
#include<conio.h>
using namespace std;
const int array_size = 1000;
void input(char *a);
void copy(char *a, char *b);
void concat(char *a, char *b);
int search(char *a, char *b);
void display(char *a, char *b);
void main(){
char arr1[array_size];
char arr2[array_size];
char a;
int b = 0;
//initialinzing array
arr1[0] = '\0';
arr2[0] = '\0';
for (int i = 1; i < array_size; i++){
arr1[i] = '-';
arr2[i] = '-';
}
while (true){
cout << "Enter the respective key to perform function:" << endl << endl
<< "1.\tInput String." << endl
<< "2.\tCopy String." << endl
<< "3.\tConcat String." << endl
<< "4.\tSearch String." << endl
<< "5.\tDisplay String." << endl;
a = _getch();
system("cls");
switch (a)
{
case '1':
input(arr1);
break;
case '2':
copy(arr1, arr2);
break;
case '3':
concat(arr1, arr2);
break;
case '4':
b = search(arr1, arr2);
if(b == 0){
cout << arr1 <<" String not founded";
} else{
cout << arr1 << " founded " << b << " times.";
}
break;
case '5':
display(arr1, arr2);
break;
default:
system("cls");
cout << "Invalid Key" << endl;
break;
}
cout << "\n\nPress any key to continue.....";
_getch();
system("cls");
}
}
void input(char *a) {
cout << "Enter string:";
cin >> a;
cout << "Successfully Entered...";
}
void copy(char *a, char *b){
cout << "Copying string....";
while (*a != '\0'){//lastpoint of copiying string
if (*b == '-' || *b == '\0'){//last point of 2nd string or its empty slots
*b = *a; //copy value
a++; //move to next value
}
b++;
if (*a == '\0'){ //end of copying string
*b = '+';
b++;
*b = '\0';
}
}
}
void concat(char *a, char *b){
char *c = a;
cout << "Concatinating string....";
while (*c != '\0'){
if (*b == '-' || *b == '\0'){
*b = *c;
c++;
}
b++;
if (*c == '\0'){
*b = '+';
b++;
*b = '\0';
}
}
*a = '\0';
}
int search(char *a, char *b){
char *c = a;
int counter = 0;
cout << "Searching: " << c << endl << endl;
c= a;
while(*b != '\0'){
while(*b != '+'){
if(*c == *b){
c++;
b++;
} else{
c = a;
b++;
}
if(*c == '\0' && *b == '+'){
counter++;
}
}
b++;
c = a;
}
if(counter > 0){return counter;}
return 0;
}
void display(char *a, char *b){
cout << "String A: " << a << endl;
cout << "String B: " << b << endl;
}
Posted by Unknown
Tag :
array,
c++,
code,
coding,
concatinate,
copying,
pointers,
program,
programming,
project,
project c++(basic),
string
An assingment of nust. Bill payment solution.
Problem:
You have to serve 20 people in all.
#include<iostream>
You have to serve 20 people in all.
- space available in queue is for 10 people.
- first person entering the queue will be served first.
- people coming to pay the bill will be added
- if yes (ask for amount)
- if no move next
- In every iteration you will ask the secrity guard if any person is served or not
- if yes ask number of user served and process them
- when every one is served print the total amount of payment recieved
#include<iostream>
Tuesday, 27 January 2015
Posted by Unknown
Program to tell whether entered number is +ive,-ive or Zero
//Program to tell whether entered number is +ive,-ive or Zero
#include<stdio.h>
#include<conio.h>
//main function
void main()
{
int x;
printf("Enter x:");
scanf("%d",&x);
if(x==0)
{
printf("The number is Zero");
}
else if(x>0)
{
printf("The number is Positive");
}
else if(x<0)
{
printf("The number is Negative");
}
getch();
}
#include<stdio.h>
#include<conio.h>
//main function
void main()
{
int x;
printf("Enter x:");
scanf("%d",&x);
if(x==0)
{
printf("The number is Zero");
}
else if(x>0)
{
printf("The number is Positive");
}
else if(x<0)
{
printf("The number is Negative");
}
getch();
}
Saturday, 17 January 2015
Posted by Unknown
Program/code to swap two numbers in C language
This code demonstrate the swaping of two numbers.
//program to swap two numbers input by user
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,a;
printf("Enter x:");
scanf("%d",&x);
printf("Enter y:");
scanf("%d",&y);
a=x;
x=y;
y=a;
printf("x is %d\n",x);
printf("y is %d",y);
getch();
}
//program to swap two numbers input by user
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,a;
printf("Enter x:");
scanf("%d",&x);
printf("Enter y:");
scanf("%d",&y);
a=x;
x=y;
y=a;
printf("x is %d\n",x);
printf("y is %d",y);
getch();
}
Posted by Unknown
Program Coding to save user_name, age in array make sure no user_name repeat twice. C++
This program save the user name and age of person in two separate array and index them so that the data could be retrieved them when required. The program also ensures that no user name could be repeated the program structure uses function and 1-D array only.
For any query about code comment below
#include<iostream>
#include<conio.h>
using namespace std;
int enter_name(char name[]) {
int i = 0;
cout << "Please Enter your user name.(Max chars 10. Enter \" - \" at end of your name)" << endl;
while (i < 10 && name[i-1] != '-'){
cin >> name[i];
i++;
}
cout << "Checking UserName...." << endl;
return --i;
}
void check_username(char user_name[], int start_name[], int &total_names, char name[], int &chars_input, int &end_point_last_name, int age[]) {
int start_point;
int end_point;
int chars_matched = 0;
int counter = 0;
bool name_found = 0;
int start_save = 0;
int name_found_index = 0;
for(int i = 0; i < total_names; i++){
start_point = start_name[i];
end_point = start_name[i+1];
chars_matched =0;
if(chars_input == (end_point - start_point)){
for (int j = start_point; j < end_point; j++){
if(user_name[j] == name[chars_matched]){
chars_matched++;
}
}
if (chars_input == chars_matched){
cout << "UserName is already in use" << endl;
name_found = 1;
cout << "User Name:\t";
for (int k = start_point; k < end_point; k++){
cout << user_name[k];
}
cout << endl << "Age:\t\t" << age[i];
}
}
}
start_point = start_name[total_names];
chars_matched = 0;
if(chars_input == (start_point)){
for (int j = start_point; j < (start_point + end_point_last_name); j++){
if(user_name[j] == name[chars_matched]){
chars_matched++;
}
}
if (chars_input == chars_matched){
cout << "UserName is already in use"<< endl;
name_found = 1;
cout << "User Name:\t";
for (int k = start_point; k < (start_point + end_point_last_name); k++){
cout << user_name[k];
}
cout << endl << "Age:\t\t" << age[total_names];
}
}
chars_matched = 0;
if (name_found == 0){
start_save = start_name[total_names];
cout << "User name is not in use" << endl;
cout << "Enter Your Age: ";
cin >> age[total_names];
for (int i = 0; i < chars_input; i++){
user_name[start_save] = name[i];
start_save++;
}
cout << "User name and age entered" << endl;
start_name[total_names+1] = start_name[total_names] + chars_input;
end_point_last_name = chars_input;
total_names++;
}
for (int i = 0; i < 10; i++){
name[i] = '+';
}
}
void main(){
char user_name[400] = { '+' };
int age[40] = {'0'};
int start_name[40] = { 0 };
int total_names = 0;
char name[10] = { '+' };
int chars_input = 0;
int end_point_last_name = 0;
while (true){
chars_input = enter_name(name);
check_username(user_name, start_name, total_names, name, chars_input, end_point_last_name, age);
/*
cout << total_names << endl;
for (int i = 0; i < total_names; i++){
cout << start_name[i] << "start_name i value" << endl;
}
cout<< "chars input" << chars_input << endl << "endpointlastname" << end_point_last_name << endl;
for (int i = 0; i < start_name[total_names]+ end_point_last_name; i++){
cout << user_name[i];
}
cout<< endl << "Check Array" << endl << endl;
*/
chars_input = 0;
cout << "Press any key to enter new user name";
_getch();
cout << endl << endl;
}
}
For any query about code comment below
#include<iostream>
#include<conio.h>
using namespace std;
int enter_name(char name[]) {
int i = 0;
cout << "Please Enter your user name.(Max chars 10. Enter \" - \" at end of your name)" << endl;
while (i < 10 && name[i-1] != '-'){
cin >> name[i];
i++;
}
cout << "Checking UserName...." << endl;
return --i;
}
void check_username(char user_name[], int start_name[], int &total_names, char name[], int &chars_input, int &end_point_last_name, int age[]) {
int start_point;
int end_point;
int chars_matched = 0;
int counter = 0;
bool name_found = 0;
int start_save = 0;
int name_found_index = 0;
for(int i = 0; i < total_names; i++){
start_point = start_name[i];
end_point = start_name[i+1];
chars_matched =0;
if(chars_input == (end_point - start_point)){
for (int j = start_point; j < end_point; j++){
if(user_name[j] == name[chars_matched]){
chars_matched++;
}
}
if (chars_input == chars_matched){
cout << "UserName is already in use" << endl;
name_found = 1;
cout << "User Name:\t";
for (int k = start_point; k < end_point; k++){
cout << user_name[k];
}
cout << endl << "Age:\t\t" << age[i];
}
}
}
start_point = start_name[total_names];
chars_matched = 0;
if(chars_input == (start_point)){
for (int j = start_point; j < (start_point + end_point_last_name); j++){
if(user_name[j] == name[chars_matched]){
chars_matched++;
}
}
if (chars_input == chars_matched){
cout << "UserName is already in use"<< endl;
name_found = 1;
cout << "User Name:\t";
for (int k = start_point; k < (start_point + end_point_last_name); k++){
cout << user_name[k];
}
cout << endl << "Age:\t\t" << age[total_names];
}
}
chars_matched = 0;
if (name_found == 0){
start_save = start_name[total_names];
cout << "User name is not in use" << endl;
cout << "Enter Your Age: ";
cin >> age[total_names];
for (int i = 0; i < chars_input; i++){
user_name[start_save] = name[i];
start_save++;
}
cout << "User name and age entered" << endl;
start_name[total_names+1] = start_name[total_names] + chars_input;
end_point_last_name = chars_input;
total_names++;
}
for (int i = 0; i < 10; i++){
name[i] = '+';
}
}
void main(){
char user_name[400] = { '+' };
int age[40] = {'0'};
int start_name[40] = { 0 };
int total_names = 0;
char name[10] = { '+' };
int chars_input = 0;
int end_point_last_name = 0;
while (true){
chars_input = enter_name(name);
check_username(user_name, start_name, total_names, name, chars_input, end_point_last_name, age);
/*
cout << total_names << endl;
for (int i = 0; i < total_names; i++){
cout << start_name[i] << "start_name i value" << endl;
}
cout<< "chars input" << chars_input << endl << "endpointlastname" << end_point_last_name << endl;
for (int i = 0; i < start_name[total_names]+ end_point_last_name; i++){
cout << user_name[i];
}
cout<< endl << "Check Array" << endl << endl;
*/
chars_input = 0;
cout << "Press any key to enter new user name";
_getch();
cout << endl << endl;
}
}
Posted by Unknown