简体   繁体   English

代码不显示也不运行

[英]the code doesn't display and doesn't run either

Below is a program that has class definitions for Item, Customer and Sales.下面是一个具有 Item、Customer 和 Sales 类定义的程序。 The main simply creates object object of each class and test its member functions. main 只是简单地创建每个类的 object 对象并测试其成员函数。 Modify the main program such that it provides a menu driven interface where user can create objects of Item, Customer and a complete a sales transaction with the sales object.The program should also have an option for display the records of items,customers and sales.To make your program more useful,include file handling such that when objects are created for Items,Customers and Transaction,the user will be prompted to save the recordon the file or not.修改主程序,使其提供一个菜单驱动的界面,用户可以在其中创建项目、客户对象以及与销售对象的完整销售交易。程序还应该有显示项目、客户和销售记录的选项。为了使您的程序更有用,包括文件处理,以便在为 Items、Customers 和 Transaction 创建对象时,将提示用户是否将记录保存在文件中。

here's the code it's not displaying anything pleasee help i'm running it by Dev c++这是它没有显示任何内容的代码请帮助我正在由 Dev c++ 运行它

#include <conio.h>
#include <iostream>
#include <string.h>
using namespace std;

class Item {
  int itemCode;

private:
  double price;
  double discount;

protected:
  int qtyOnStock;
  char *name;

public:
  Item() {
    itemCode = 0;
    strcpy(name, "UNKNOWN");
    price = 0;
    discount = 0;
    qtyOnStock = 100;
  }
  void setItemCode(int c) { itemCode = c; }
  int getItemCode() { return itemCode; }
  double getPrice() { return price; }
  void setPrice(double p) { price = p; }
  void setDiscount(double d) { discount = d; }
  double getDiscount(double d) { return ((d < 20 ? d : 20) / 100 * price); }
  void setName(char *n) { name = n; }
  char *getName() { return name; }

  void setQtyOnStock(int q) { qtyOnStock = q; }
  int getQtyOnStock() { return qtyOnStock; }
};
class Customer {
private:
  int id;
  char *name;
  char *contactNo;
  int type;

public:
  Customer() {
    id = 0;
    strcpy(contactNo, "No Num");
    strcpy(name, "No Name");
    type = 0;
  }
  void setId(int newId) { id = newId; }
  int getId() { return id; }
  void setName(char *n) { strcpy(name, n); }
  char *getName() { return name; }
  void setContactNo(char *c) { strcpy(contactNo, c); }
  char *getContactNo() { return name; }
};
class Sales {
private:
  Item item;
  Customer cust;
  char *date;
  int qtySold;

public:
  Sales() { date = "mm-dd-yyyy"; }
  void setItem(Item newItem) { item = newItem; }
  Item getItem() { return item; }
  void setCustomer(Customer newCust) { cust = newCust; }
  Customer getCustomer() { return cust; }
  void setDate(char *newDate) { strcpy(date, newDate); }
  char *getDate() { return date; }
  void setQtySold(int newQty) { qtySold = newQty; }
  int getQtySold() { return qtySold; }
};
int main() {
  Item item1;
  Customer cust1;
  Sales sales1;
  item1.setItemCode(143);
  item1.setName("Ballpen");
  item1.setPrice(12.5);
  item1.setQtyOnStock(250);
  cust1.setId(123);
  cust1.setName("Juan dela Cruz");
  sales1.setItem(item1);
  sales1.setCustomer(cust1);
  sales1.setDate("10-27-2018");
  sales1.setQtySold(98);
  item1.setQtyOnStock(item1.getQtyOnStock() - sales1.getQtySold());
  system("cls");
  cout << sales1.getItem().getName() << endl << item1.getQtyOnStock();
  getch();
  return 0;
}

In your Item class:在您的 Item 类中:

protected:
  int qtyOnStock;
  char *name;

public:
  Item() {
    itemCode = 0;
    strcpy(name, "UNKNOWN");
    price = 0;
    discount = 0;
    qtyOnStock = 100;
  }

name is an unitialized char pointer so copying to it will result in UB. name是一个未初始化的 char 指针,因此复制到它会导致 UB。

change char* name to std::string name , replace strcpy(...) name = "UNKNOWN" .char* name更改为std::string name ,替换strcpy(...) name = "UNKNOWN"

Normally though you initialize member variables like this:通常,尽管您像这样初始化成员变量:

  Item() 
  : itemCode(0), itemCode(0), name("UNKNOWN"), price(0), discount(0), qtyOnStrock(100) 
  {}

a newer compiler lets you initialize in other ways like when declared eg:较新的编译器允许您以其他方式进行初始化,例如在声明时:

protected:
  int qtyOnStock{100};
  std::string name{"UNKNOWN"};
...

The main and biggest proble is that you do C-style string handling with char* and even that in a wrong way.主要和最大的问题是您使用char*进行 C 风格的字符串处理,甚至以错误的方式处理。

If you would enable all warning in your compiler, it would already tell you the problems.如果您在编译器中启用所有警告,它就会告诉您问题所在。 My VS2019 gives 15 errors, 1 warning and 7 messages, when I try to compile your code.当我尝试编译您的代码时,我的 VS2019 给出了 15 个错误、1 个警告和 7 个消息。 Please see:请参见: 在此处输入图像描述

So, the main problem is that you are using char* that are not initialzed, meaning they point to somehwere, and that you do not allocate memory to store your strings.因此,主要问题是您使用的是未初始化的char* ,这意味着它们指向某个位置,并且您没有分配内存来存储字符串。

So all your strcpy functions will fail and probably crash your system.所以你所有的strcpy函数都会失败并且可能会导致你的系统崩溃。 Also the assignments to a char* will fail in most cases.在大多数情况下,对char*的分配也会失败。

You will overwrite some random memory.您将覆盖一些随机内存。

All this can be immediately fixed, without big problems, if you would use std::string instead of char* .如果您使用std::string而不是char* ,所有这些都可以立即修复,没有大问题。 Because char* are that error prone, C++ introduced the std::string , so, please use it.因为char*容易出错,所以 C++ 引入了std::string ,所以,请使用它。

Sometimes you have C++ teachers that want you to use char* .有时您的 C++ 老师希望您使用char* Those teachers should be fired.这些老师应该被解雇。 But if you really need to use char* .但是如果你真的需要使用char* Then you must allocate memory, before coping data.然后你必须在处理数据之前分配内存。

Let us assume that you have a string "myName" and you want to copy that.让我们假设您有一个字符串“myName”并且您想要复制它。

char* name{};
name = new char[strlen(myName)+1];  // +1 for the trailing '\0'
strcpy(name, myName);

// ...
// ...
// Do stuff
// ...
// ...

delete [] name;  // Release memory at the end

But as said.但正如所说。 Simply use std::string只需使用std::string

Your program as is, cannot work.您的程序按原样无法运行。 You need a major refactoring.您需要进行重大重构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM