程序运行可以,但达不到预期,只能读取默认值,不能存也不能取
程序运行可以,但达不到预期,只能读取默认值,不能存也不能取头文件
#ifndef STACK_H_
#define STACK_H_
namespace bank
{
class Stack
{
private:
enum{Max=40};
char fullname[Max];
char account[Max];
unsigned long deposit;
unsigned long deposit_temp;
void set_tot() { deposit = deposit + deposit_temp; }
public:
void save(Stack& n,char* name_user,char*name_acount,unsigned long money);
void withdraw(Stack& n, char* name_user, char* name_acount, unsigned long money);
bool verify_name(Stack& n, char* name_name);
bool verify_acount(Stack& n, char* name_acount);
void show(const Stack& n)const;
Stack();
Stack(const char* name_user, const char* name_acount, unsigned long money);
~Stack();
};
}
#endif
源代码1
#include<iostream>
#include"未命名2.h"
#include<cstring>
#include<cstdlib>
void bank::Stack::save(Stack& n, char* name_user, char* name_acount, unsigned long money)
{
if (!verify_name(n, name_user))
{
std::cout << "Error! validation error!";
exit(-1);
}
else if (!verify_acount(n, name_acount))
{
std::cout << "Error! validation error!";
exit(-1);
}
else
n.deposit_temp = money;
set_tot();
}
void bank::Stack::withdraw(Stack& n, char* name_user, char* name_acount, unsigned long money)
{
if (!verify_name(n,name_user))
{
std::cout << "Error! validation error!";
exit(-1);
}
else if (!verify_acount(n,name_acount))
{
std::cout << "Error! validation error!";
exit(-1);
}
else
n.deposit_temp = -money;
set_tot();
}
bool bank::Stack::verify_name(Stack& n, char* name_name)
{
if (strcmp(n.fullname, name_name) == 0)
return true;
else
return false;
}
bool bank::Stack::verify_acount(Stack& n, char* name_acount)
{
if (strcmp(n.account, name_acount) == 0)
return true;
else
return false;
}
void bank::Stack::show(const Stack& n)const
{
using std::cout;
using std::endl;
cout << "Fullname: " << n.fullname << endl;
cout << "Acount: " << n.account << endl;
cout << "Deposit: " << n.deposit << endl;
}
bank::Stack::Stack()
{
}
bank::Stack::Stack(const char* name_user, const char* name_acount, unsigned long money)
{
strcmp(fullname, "no name");
strcmp(account, "no acount");
deposit = 0;
}
bank::Stack::~Stack()
{
}
程序文件
#include<iostream>
#include"未命名2.h"
int main()
{
using namespace bank;
using namespace std;
Stack stock1("dsadda", "safa", 5465);
cout << "Enter s(save mony) w(withdraw money) o(show the acount) q(to quit):";
char ch;
char temp_name[40];
char temp_acount[40];
unsigned long temp_money;
while (cin >> ch && ch != 'q'&&ch!='Q')
{
if (ch == 'q' || ch == 'Q')
break;
else
{
switch (ch)
{
case's':
case'S':cout << "Enter the fullname:";
cin.get();
cin.getline(temp_name, 40);
cout << "Enter the acount:";
cin.getline(temp_acount, 40);
cout << "Enter the money:";
cin >> temp_money;
cin.get();
stock1.save(stock1, temp_name, temp_acount, temp_money);
break;
case'W':
case'w':cout << "Enter the fullname:";
cin.get();
cin.getline(temp_name, 40);
cout << "Enter the acount:";
cin.getline(temp_acount, 40);
cout << "Enter the money:";
cin >> temp_money;
cin.get();
stock1.withdraw(stock1, temp_name, temp_acount,temp_money);
break;
case'o':
case'O':stock1.show(stock1);
}
}
}
cout << "Bye!";
return 0;
}