本人写的一个C++小银行系统,很多错误,牛牛们给指正一下。

intruder 发布于 2012/04/10 08:32
阅读 1K+
收藏 0

1.BANK.H --This is head file.

// bink.h -- a bank-system program
#ifndef BANK_H_
#define BANK_H_

class Bank
{
    private:
	enum {LENGTH = 40};
	char name[LENGTH];
	char account_number[LENGTH];
	unsigned long int share;
	unsigned long long sum_shares;
    public:
	Bank(char *, char *, int);
	~Bank();
	unsigned long int into_money(unsigned long int money);
	unsigned long int out_money(unsigned long int money);
	void show_info()const;
};

#endif

2.BANK.CPP ---This is the define of head file

// bank.cpp -- to decaleration the bank.h define
#include <iostream>
#include "bank.h"

Bank::Bank(char *, char *, int)
{
    share = 5;
    sum_shares = share;
}

Bank::~Bank()
{
}

unsigned long int Bank::into_money(unsigned long int money)
{
    using std::cout;
    if (money < 0)
    {
	cout << "You can't do this!\n"
	    << "Please try again!\n";
    }
    else
    {
    	share = money;
    	sum_shares += share;
    }
    show_info();
    return sum_shares;
}

unsigned long int Bank::out_money(unsigned long int money)
{
    using std::cout;
    if (money < 0)
    {
	cout << "You can't do this!\n"
	    << "Please try again!\n";
    }
    else if (money > sum_shares)
    {
	cout << "You don't have enough money to out!\n"
	    << "Please try again!\n";
    }
    else
    {
    	share = money;
    	sum_shares -= share;
    }
    show_info();
    return sum_shares;
}
void Bank::show_info()const
{
    using std::cout;
    using std::endl;

    cout << "You information:" << endl;
    cout << "Account Name:# " << name << endl
	<< "Account Number:# " << account_number << endl
	<< "You have : $" << sum_shares << " int the bank." 
	<< endl;
    cout << "Thank for your use" << endl << "Bye.\n";
}

3.Main function -- Running with here

// banker.cpp -- the main file in the bank-system program
#include <iostream>
#include "bank.h"

int main()
{
    using std::cout;
    using std::cin;
    using std::endl;
    cout << "Please login your bank information about:\n \"Accout Name\"-\"Accout Number\"and How much money do you want to in:";
    cout << endl;
    cout << "Of course ,you must create a Accout before you do this ...." << endl;
    Bank person = Bank("Zhouc", "1234567",100);
    unsigned long int number;
    int which;
    cout << "Please choose your want:" << endl;
    cout << "1.into the money" << endl;
    cout << "2.out the money" << endl;
    cout << "3.show your have!" << endl;
    cout << "4.And 'Q' or 'q' to quit! " << endl;
    while (cin >> which && (cin.get() != 'Q' || cin.get() != 'q'))
    {
	while (cin.get() != '\n')
	    continue;
	switch(which)
	{
	   case "1":cout << "Please input the shares of money:\n";
		  cin >> number;
		  person.into_money(number);
		  break;
	   case "2":cout << "Please input the shares of money:\n";
		  cin >> number;
		  person.out_money(number);
		  break;
	   case "3":cout << "Now ,please looking your information at down:\n";
		  person.show_info();
		  break;
	   default:cout << "Your choose is error.please try again!\n";
		   break;
	}
    	cout << "Please choose your want:" << endl;
    	cout << "1.into the money" << endl;
    	cout << "2.out the money" << endl;
    	cout << "3.show your have!" << endl;
    	cout << "4.And 'Q' or 'q' to quit! " << endl;
	cin >> which;
    }
    cout << "Bye.\n";

    return 0;
}

每一次写这么多行的程序,有很多错误,实在不知道该怎么解决了。请牛们分享点写代码时候排错和注意事项的经验。

加载中
0
zplswf
zplswf
我认为应该写完一个小功能的时候测完以后看是否有问题再继续,并且给以后留下接口!!!
intruder
intruder
嗯,谢谢老师指导。
0
中山野鬼
中山野鬼
各人建议楼主,我估计你的代码是抄的老书的。个人建议前期不要如此。这写老书的案例是老外的,案例中的很多技术对后期你的工作没什么帮助。同时对于前期学习,会把你的问题点全部铺开,增加你学习的难度。
intruder
intruder
回复 @中山野鬼 : 第一次写啊。所以才这样子了。那个书上有这个题目嘛,要求是写这个bank这种的,我就这样写了。第一次OOP,经验严重不足啊。
中山野鬼
中山野鬼
回复 @intruder : 如果自己写的,那得先规划系统。怎么会想起来这个BANK的例子?
intruder
intruder
没有啊。这个是我看完C++ primer《对方和类》那一章的时候后面的编程习题。用书上的知识纯自己写的,没有抄的。~~>_<~~
0
0
l
liucong
你这个问法,让人很难回答。你应该学习自己找错误,再向别人请教。
0
RainJ
RainJ
问题在于,程序关掉之后用户的钱还有么?
intruder
intruder
暂时就是写出来那个意思嘛。等到以后学到文件了,再用文件来也行。
0
Monkey
Monkey
调试,当年C++课程设计写了一个几百行代码的分数计算器。不知道调试多少遍了,最后还有警告。新手写代码肯定有错,要检查错误只能是调试,一句一句调试下去。
intruder
intruder
嗯。好的。我用的G++,看来得多学学GDB了。
0
x
intruder
intruder
回复 @xiaoshula : 哥们你这话说得就不对了。我没有这样想。我只是想知道怎么找出错误,然后写出优秀简洁,运行没有问题的代码。
x
xiaoshula
回复 @intruder : 你倒是有自知之明。以后整点有技术含量的,别整了个helloword就以为自己老牛B了
intruder
intruder
………………这可是有很多错误的。再多的代码没有实用也是废柴呀。
返回顶部
顶部