C ++类银行账户问题 [英] C++ class bank account problem

查看:117
本文介绍了C ++类银行账户问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解决以下程序: -



实施班级银行。这个银行有两个对象,检查和储蓄,

类型的帐户。实现以下会员功能:



无效存款(双倍金额,帐号)

无效取款(双倍金额,帐号)

void print_balances()

这里的帐户字符串是S或C。对于存款或取款,它表明哪个帐户受到影响。



我尝试过:



I tried to solve the following program:-

Implement a class Bank. This bank has two objects, checking and savings,
of the type Account. Implement the following member functions:

void deposit(double amount, char account)
void withdraw(double amount, char account)
void print_balances()
Here the account string is "S" or "C". For the deposit or withdrawal, it indicates which account is affected.

What I have tried:

#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
class bank
{
private:
double a1,a2,sa,cu;
char ac,c,s;
public:
bank()
{
sa=0;
cu=0;
}
void deposit(double amount, char account);
void withdraw(double amount, char account);
void print_balances();
};
void bank::deposit(double amount1, char account)
{
a1=amount1,
ac=account;
if (ac=='s')
{
sa=sa+a1;

}
else if(ac=='c')
{
cu=cu+a1;
}
}
void bank::withdraw(double amount2, char account)
{
a2=amount2,
ac=account;
if (ac=='s')
{
sa=sa-a2;
}
else if(ac=='c')
{
cu=cu-a2;
}
}
void bank::print_balances()
{
cout<<"\nthe money in the savings account is: "<<sa;
cout<<"\nthe money in the current  account is: "<<cu;
}
main()
{
char account;
double amount1,amount2;
int l;
clrscr();
bank b;
do
{

cout<<"\nenter your choice";
cout<<"\n1.deposit";
cout<<"\n2. withdraw";
cout<<"\n3.print balances";
cout<<"\n4.exit";
cout<<"\n";
cin>>l;
switch(l)
{
case 1:
cout<<"\nenter account type: ";
cin>>account;
cout<<"\nenter amount to be deposited :" ;
cin>>amount1;
b.deposit(amount1, account);
break;
case 2:
cout<<"\nenter account type: ";
cin>>account;
cout<<"\nenter amount to be withdrawn :" ;
cin>>amount2;
b.deposit(amount2, account);
break;
case 3:
b.print_balances();
break;
case 4:
cout<<"\nthanx ";

}
}while(l!=4);
getch(); 
}

/* now, when I use the void withdraw(double amount, char account); function and withdraw the money, instead of decreasing, the money increases. suppose I have deposited 5 in my savings account. After that I withdraw 5 from my account, the money in the savings account should be 0 but it is 10, i.e instead of decreasing 5 in my account, it increases. */

推荐答案

当你不理解你的代码在做什么或为什么它做它的作用时,答案是 debugger

使用调试器查看代码正在执行的操作。它允许你逐行执行第1行并在执行时检查变量,它是一个令人难以置信的学习工具。



调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做的比较。

调试器中没有魔法,它没有找到错误,它只是帮助你。当代码没有达到预期效果时,你就接近了一个错误。
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.


只是为了从未回答的列表中取出这个

看看你的代码

cout<<\ nnn金额撤回:;

cin>> amount2;

b。存款(金额2,帐户);



了解如何使用调试器,您将能够找到并修复这些类型的问题你自己。
Just to get this off the un-answered list
Look at your code
cout<<"\nenter amount to be withdrawn :" ;
cin>>amount2;
b.deposit(amount2, account);

Learn how to use the debugger and you will be able to find and fix these types of problems yourself.


这篇关于C ++类银行账户问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆