从文件中减去输出计算(c ++)

时间:2014-01-19 20:01:06

标签: c++ file

假设我有一个包含值1000的文本文件,并且用户输入他们想要从1000中减去多少,他们想要多少次。有没有办法输出实际的计算?

int  read_balance(void);
void write_balance(int balance);

using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{

int selection;
int total;
int attempts = 0;
string name;
string number;
int amount = 0;


do {


cout << "1. Transfer an amount" <<endl;
cout << "2. List recent transactions"<<endl;
cout << "3. Display account details and current balance"<<endl;
cout << "4. Quit" << endl;
cout << "Please enter menu number"<<endl;
cout << "" << endl;
cin >> selection;


int amount = 0;

switch(selection)
{
case 1: 
cout << "You have choosen to transfer an amount" << endl;
cout << "How much do you wish to transfer from the shop account?"<<endl;
cout << "transfer funds" << endl;


if (std::cin >> amount)
{
    std::cout << "Transferred Amount:" << amount << "\n";
    int balance = read_balance();

    if (amount <= 0)
    {
        std::cout << "Amount must be positive\n";
    }
    else if (balance < amount)
    {
        std::cout << "Insufficient funds\n";
    }
    else
    {
        int new_balance = balance - amount;

        write_balance(new_balance);
        std::cout << "New account balance: " << new_balance << std::endl;

    fstream infile("time.txt", ios::app);



    std::time_t result = std::time(nullptr);
    std::string timeresult = std::ctime(&result);


    infile << amount << std::endl;
    infile << timeresult << std::endl;
    }

}


break;


case 2:
cout << "Here are you're recent transactions" <<endl;
cout << "" << endl;
cout << "" << endl;

break;

case 3:
cout << "The account names is:" << endl;
cout << "The account number is:" << endl;
std::cout << "The current account balance is " << read_balance() << std::endl;
break;

case 4:
return 0;
break;

default:
cout << "Ooops, invalid selection!" << endl;
break;

}

}while(selection != 4);

system("pause");
return 0;
}

int read_balance(void)
{
std::ifstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop.txt");
int balance;
f >> balance;
f.close();
return balance;
}

void write_balance(int balance)
{
std::ofstream f;
f.exceptions(std::ios::failbit | std::ios::badbit);
f.open("shop.txt");
f << balance;
f.close();
}

0 个答案:

没有答案
相关问题