用于初始化和返回客户的 c++ 程序草稿出现问题

Trouble with draft c++ program made to initialise and return a customer

本文关键字:问题 程序 c++ 初始化 返回 客户 用于      更新时间:2023-10-16

我正在创建一个 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;
}

总结:

  1. 如果使用#define Customer预处理器将删除客户标识符字符串的所有实例...因此,编译器将找到的代码将像格鲁耶尔奶酪一样。
  2. 不能使用不同的默认值多次声明相同的函数
  3. 不能多次定义相同的函数
  4. 用数字初始化 std::string 是个坏主意。这将使程序崩溃
  5. 行中有一个额外的括号return customer; {
  6. using namespace std;放在#include指令的中间是一个糟糕的(风格)主意......因为它会影响之后解析的代码(从标头),可能会导致令人惊讶(且难以调试)的问题。
  7. 客户结构包含不带类型的属性或不带属性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;
}