Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <fstream> // библиотека за файлове
- #include <string>
- #include <iostream>
- using namespace std;
- struct account
- {
- string name;
- double sum;
- };
- void convertFromFileToArray(account accountsData[100], int &accountsCount);
- void investing(account accountsData[100], int accountsCount);
- void withdrawing(account accountsData[100], int accountsCount);
- void checkBalance(account accountsData[100], int accountsCount);
- void transfer(account accountsData[100], int accountsCount);
- void updateFile(account accountsData[100], int accountsCount);
- void main()
- {
- account accountsData[100];
- int accountsCount = 0;
- convertFromFileToArray(accountsData, accountsCount);
- while (true)
- {
- int choice;
- cout << "Choose 1 for investing." << endl; //внасяне
- cout << "Choose 2 for withdrawing." << endl; //теглене
- cout << "Choose 3 for balance check." << endl; //проверка на сметка
- cout << "Choose 4 for transfering" << endl; //прехвърляне
- cout << "Your choice: ";
- cin >> choice;
- switch (choice)
- {
- case 1:
- investing(accountsData, accountsCount);
- break;
- case 2:
- withdrawing(accountsData, accountsCount);
- break;
- case 3:
- checkBalance(accountsData, accountsCount);
- break;
- case 4:
- transfer(accountsData, accountsCount);
- break;
- }
- updateFile(accountsData, accountsCount); //след операция с пари update-ни файла
- }
- }
- void convertFromFileToArray(account accountsData[100], int &accountsCount)
- {
- ifstream readFile;
- readFile.open("accounts.txt");
- string name; //променлива, взимаща името от един ред
- string sum; // променлива за сумата от един ред. string, защото файла е текстов
- //чети докато файла има редове и присвоявай прочетенето от линията на двете променливи
- while (readFile >> name >> sum)
- {
- name = name.substr(0, name.size() - 1); //махни ','
- accountsData[accountsCount].name = name;
- accountsData[accountsCount].sum = stod(sum); //от стринг (защото файла е текстов) към double
- accountsCount++;
- }
- }
- void investing(account accountsData[100], int accountsCount)
- {
- string name;
- cout << "Account number: ";
- cin >> name;
- double money;
- cout << "Money: ";
- cin >> money;
- for (int i = 0; i < accountsCount; i++)
- {
- if (accountsData[i].name == name)
- {
- accountsData[i].sum += money;
- break;
- }
- }
- }
- void withdrawing(account accountsData[100], int accountsCount)
- {
- string name;
- cout << "Account number: ";
- cin >> name;
- double money;
- cout << "Money: ";
- cin >> money;
- for (int i = 0; i < accountsCount; i++)
- {
- if (accountsData[i].name == name)
- {
- accountsData[i].sum -= money;
- break;
- }
- }
- }
- void checkBalance(account accountsData[100], int accountsCount)
- {
- string name;
- cout << "Account number: ";
- cin >> name;
- for (int i = 0; i < accountsCount; i++)
- {
- if (accountsData[i].name == name)
- {
- cout << "Your current balance is: " << accountsData[i].sum << endl;
- break;
- }
- }
- }
- void transfer(account accountsData[100], int accountsCount)
- {
- string from;
- cout << "From (account number): ";
- cin >> from;
- string to;
- cout << "To (account number): ";
- cin >> to;
- double money;
- cout << "Money: ";
- cin >> money;
- for (int i = 0; i < accountsCount; i++)
- {
- if (accountsData[i].name == from)
- {
- accountsData[i].sum -= money;
- }
- if (accountsData[i].name == to)
- {
- accountsData[i].sum += money;
- }
- }
- }
- void updateFile(account accountsData[100], int accountsCount)
- {
- ofstream writeOnFile; //променлива за запис(писане) върху файла
- writeOnFile.open("accounts.txt"); //изтрива досегашното съдържание и позволява да пишем
- for (int i = 0; i < accountsCount; i++)
- {
- writeOnFile << accountsData[i].name << ", " << accountsData[i].sum << '\n';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement