用于初始化和返回客户的 c++ 程序草稿出现问题
Trouble with draft c++ program made to initialise and return a customer
我正在创建一个 c++ 程序,其中 A 客户能够被初始化并返回......是的,你猜对了它的拼贴,下面是我用Visual Studio 2013编写的代码。我已经编译了许多错误(列出许多错误),这些错误超出了我作为入门级堆栈成员的知识范围。我需要你知道代码的某些部分丢失了,但不是大量的部分,只是一些输出信息......这仅仅是因为它不是完整的程序,我已经完成了一半,并且想测试我必须测试的内容,以确保我远程接近让它工作并实际将其打印到屏幕上......我已经知道谷歌提供的教程来帮助我们,但我昨晚花了 6 个小时到凌晨看这些,它们往往不适用于我做得那么好。
我会感谢有人拼凑了我在下面拥有的东西,我发现很难从描述中应用东西。 再次感谢堆栈上的精彩人士!
#include <iostream>
#include <iomanip>
#include <string> using namespace std;
#include "Customer.h"
#define Customer
Customer* CreateCustomer (const string& name, const string& id, const string& pin=0);
Customer* CreateCustomer(const string& name, const string& id, const string& pin) {
Customer* customer = new Customer();
customer->Name = name;
customer->ID = id;
customer->Pin = pin;
customer->List[0].Name = "Mary Jones";
customer->List[0].Id = "235718";
customer->List[0].Pin = "5074";
customer->List[1].Name = "John Smith";
customer->List[1].Id = "375864";
customer->List[1].Pin = "3251";
customer->Enrolled = unit;
return customer;
}
Customer* CreateCustomer(const string& name, const string& id, const string& pin = 1);
Customer* CreateCustomer(const string& name, const string& id, const string& pin) {
Customer* customer = new Customer();
customer->Name = name;
customer->Id = id;
customer->Id = pin;
customer->List[1].Name = "John Smith";
customer->List[1].Id = "375864";
customer->List[1].Pin = "3251";
customer->List = CreateCustomer;
return customer; {
}
int main() {
//test
Customer* Mary = CreateCustomer("Mary Jones", "235718", "5074");
return 0;
}
请允许我也向您展示我的 #include 头文件...
#include <iostream>
#include <iomanip>
#include <string> using namespace std;
#ifndef CUSTOMER_H
#define CUSTOMER_H
struct Customer {
string Name;
string Id;
string Pin;
CreateCustomer [10];
int Enrolled;
};
#endif
当我完成所有操作时,我需要它来执行以下操作,
int main()
{
Customer* Mary = CreateCustomer("Mary Jones", "235718", "5074");
Customer* John = CreateCustomer("John Smith", "375864", "3251");
Account* MaryAccount = CreateAccount(*Mary, "06-3121-10212357", "01/03/2014", 100);
Account* JohnAccount = CreateAccount(*John, "06-3121-10213758", "10/03/2014");
RecordWithdraw(MaryAccount, CreateTransaction("01/03/2014", "ATM Withdrawal", 50) );
RecordDeposit(MaryAccount, CreateTransaction("02/03/2014", "Deposit", 90) );
RecordWithdraw(MaryAccount, CreateTransaction("04/03/2014", "ATM Withdrawal", 150) );
RecordDeposit(MaryAccount, CreateTransaction("05/03/2014", "Deposit", 20) );
RecordWithdraw(MaryAccount, CreateTransaction("05/03/2014", "Withdraw", 100) );
RecordWithdraw(MaryAccount, CreateTransaction("05/03/2014", "Withdraw", 50) );
RecordDeposit(JohnAccount, CreateTransaction("11/03/2014", "Deposit", 20) );
RecordDeposit(JohnAccount, CreateTransaction("12/03/2014", "Deposit", 80) );
RecordWithdraw(JohnAccount, CreateTransaction("12/03/2014", "Withdraw", 50) );
PrintReport(MaryAccount);
PrintReport(JohnAccount);
return 0;
}
总结:
- 如果使用
#define Customer
预处理器将删除客户标识符字符串的所有实例...因此,编译器将找到的代码将像格鲁耶尔奶酪一样。 - 不能使用不同的默认值多次声明相同的函数
- 不能多次定义相同的函数
- 用数字初始化 std::string 是个坏主意。这将使程序崩溃
- 行中有一个额外的括号
return customer; {
- 将
using namespace std;
放在#include
指令的中间是一个糟糕的(风格)主意......因为它会影响之后解析的代码(从标头),可能会导致令人惊讶(且难以调试)的问题。 - 客户结构包含不带类型的属性或不带属性
CreateCustomer[10]
的类型...有趣的是,它与您的重复函数同名......
我相信还有更多...
这将是迈向程序的第一步。
顺便说一句,这实际上不是应该在C++中完成的方式,而是在 C 中完成的方式。在C++中,您将使用类和成员函数,但那是另一回事了。
我更改了以下行:
Account* MaryAccount = CreateAccount(*Mary, "06-3121-10212357", "01/03/2014", 100);
到
Account* MaryAccount = CreateAccount(Mary, "06-3121-10212357", "01/03/2014", 100);
因为在这种情况下,将Mary
客户的完整副本存储在帐户中是一个坏主意,而存储指针更简单、更合适。
#include <iostream>
#include <iomanip>
#include <string>
using namespace std ;
struct Customer {
string Name;
string Id;
string Pin;
};
struct Account {
Customer *Customer ;
string AccountNumber ;
string AccountCreationDate ;
int Balance ;
};
Customer* CreateCustomer(const string & name, const string & id, const string & pin)
{
Customer *newcustomer = new Customer ;
newcustomer->Id = id ;
newcustomer->Name = name ;
newcustomer->Pin = pin ;
return newcustomer ;
}
Account *CreateAccount(Customer *customer, const string & accountnumber, const string & creationdate, int initialbalance)
{
Account *newaccount = new Account ;
newaccount->Customer = customer ;
newaccount->AccountNumber = accountnumber ;
newaccount->AccountCreationDate = creationdate ;
newaccount->Balance = initialbalance ;
return newaccount ;
}
int main()
{
Customer* Mary = CreateCustomer("Mary Jones", "235718", "5074");
Customer* John = CreateCustomer("John Smith", "375864", "3251");
Account* MaryAccount = CreateAccount(Mary, "06-3121-10212357", "01/03/2014", 100);
// ...
return 0;
}
相关文章:
- 基于boost的程序的静态链接——zlib问题
- 使用mongocxx驱动程序时包含头文件问题
- 运行程序时出现问题
- C++问题:用户认为数字1-100,程序提出问题不超过6次即可得到答案。无法正确
- 将应用程序从32位移植到64位时出现问题
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- C++ 每次运行程序时我都会"nan"输出的问题
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 如何解决在负数的情况下程序以相同方式工作的问题?
- 在树莓派上用libtorch构建程序时的链接问题
- 覆盖应用程序的低级别键盘挂钩问题
- 运行长度编码程序 c++ 的问题
- 编译 Cilk Plus 程序时遇到问题
- 我的堆栈和库存清单程序的结构有什么问题?
- 系统错误:程序无法启动,因为您的计算机中缺少MSVCP140D.DLL。尝试重新安装该程序以解决此问题
- wxWidgets 拖放文件事件处理程序初始化问题(无效static_cast)
- 桌面计算器出现问题.C++程序设计语言
- 内存泄漏问题c++程序
- 写函数的基本问题.程序随机跳到最后