标题:程序运行可以,但达不到预期,只能读取默认值,不能存也不能取
只看楼主
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
结帖率:100%
 问题点数:0 回复次数:3 
程序运行可以,但达不到预期,只能读取默认值,不能存也不能取
程序运行可以,但达不到预期,只能读取默认值,不能存也不能取
头文件
#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;
}
搜索更多相关主题的帖子: Stack cout char cin money 
2021-05-22 17:06
C00000001
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2021-5-8
得分:0 
不用visual studio 编译 strcmp()不能用我这个编译
我用的Dev-c++能编译
2021-05-22 17:12
ml232528
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:367
专家分:879
注 册:2007-7-23
得分:0 
strcmp是比较两个字符串。
拷贝字符串应该用strcpy
比如构造函数,用strcpy拷贝name_user到fullname中,应该这样写:
程序代码:
bank::Stack::Stack(const char* name_user, const char* name_acount, unsigned long money)
{
    strcpy(fullname, name_user);
    strcpy(account, name_acount);
    deposit_temp = money;
}

-︻┻┳═一 ☆ 悲伤的代价就是让自己明白什么是最重要的和应该珍惜的
2021-06-03 14:33
李知恩iu
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2021-6-15
得分:0 
2021-06-15 10:10



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-505808-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.121426 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved