【问题标题】:Dynamic char array from class dont printing the proper result [closed]类中的动态字符数组未打印正确的结果[关闭]
【发布时间】:2016-08-20 21:12:10
【问题描述】:

我正在制作一个项目,用户将在其中添加汽车的信息,一个函数将显示所有可用的汽车。但我无法将汽车的名称从一个数组复制到另一个数组,这是代码。

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

const int SIZE=10;

class car
{
   public:
int car_no=0;
char car_name[SIZE][20];
char car_model[SIZE][20];
char car_colour[SIZE][20];
char reg_id[SIZE][20];
int rate_per_day[SIZE];
char cars_in_lot[SIZE][20];
};

addcar()
{
    char choice;
    do{

   car*newcar=new car;

    cout<<"\t\t\t\t(->)  Name : ";
    cin>>newcar->car_name[newcar->car_no];
    strcpy(newcar->cars_in_lot[newcar->car_no],newcar->car_name[newcar->car_no]);
    cout<<endl;
    cout<<"\t\t\t\t(->)  reg!str@t!0n number : ";
    cin>>newcar->reg_id[newcar->car_no];
    cout<<endl;
    cout<<"\t\t\t\t(->)  c0l0ur : ";
    cin>>newcar->car_colour[newcar->car_no];
    cout<<endl;
    cout<<"\t\t\t\t(->)  Model : ";
    cin>>newcar->car_model[newcar->car_no];
    cout<<endl;
    cout<<"\t\t\t\t(->)  Rate of Rent Per Day : ";
    cin>>newcar->rate_per_day[newcar->car_no];
    newcar->car_no++;
    cout<<"Want to add another car [y/n]"<<endl;
    cin>>choice;
    }while(choice == 'y' || choice == 'Y');

}
void show_cars_in_lot()
 {
      cout<<"Avaialable Cars in Lot are : "<<endl;
      car*newcar=new car;
      for(int i=0;i<SIZE;i++)
      cout<<i+1<<"\t"<<newcar->cars_in_lot[i]<<"\t"<<newcar->car_colour[i]     <<"\t"<<newcar->car_model[i]<<"\t"<<newcar->reg_id[i]<<endl;
getch();
}

void display1()
  {

cout<<"\t\t\t(1)  Show Cars in Lot"<<endl;
cout<<"\t\t\t(2)  Add Cars"<<endl;
 }// end of display1

int main()
 {
  char option;
  int desire_car;
do
{
    int choice;
    display1();
    cout<<"Enter Your Choice : ";
    cin>>choice;
    switch(choice)
    {
    case 1:
        show_cars_in_lot();break;
    case 2:
        addcar();break;
    default:
    cout<<"You Entered Wrong Input"<<endl;
    }
cout<<"Go To Main Menu [y/n]";cin>>option;
}while(option == 'y' || option == 'Y');

return 0;
  }

【问题讨论】:

  • 使用std::string 让您的生活更轻松。
  • 还可以使用调试器让您的生活更轻松。
  • 因为您将汽车添加到一个对象(car*newcar=new car; in addcar())但尝试从另一个对象显示(car*newcar=new car; in show_cars_in_lot())。仅使用 new 为新实例分配内存。可能你不需要在show_cars_in_lot()中使用new
  • @kay27 如果我不在 show_cars_in_lot 中编写新程序,则程序不会运行
  • 所以,这种方式程序运行,但不起作用...您需要使用show_cars_in_lot() 中的现有对象,您不必创建另一个。如何使用它:有很多方法。例如。仅使用一次new - 在 main() 中并将指针保存在某处。

标签: c++ class oop dynamic char


【解决方案1】:

我建议您将数据结构更改为汽车属性的容器,而不是单个数组结构。

struct Car_Properties
{
  public:
    int         number;
    std::string name;
    std::string model;
    std::string colour;
    std::string reg_id;
    int         rate_per_day;
    std::string cars_in_lot;
};
std::vector<Car_Properties> car_inventory;
// Or  
Car_Properties car_lot[SIZE];

一般来说,上述方法效率更高,因为汽车属性(对于单个汽车)在内存中是连续的。

注意:使用std::vector 时,不需要使用new 运算符。 std::vector 将复制一份。您将需要实现复制构造函数和赋值运算符。提醒:还要重载运算符==&lt;

编辑 1:动态创建 Car_Properties
最简单的方法就是声明一个变量的实例:

int main()
{
  Car_Properties das_auto; // This creates an instance in local memory.
  //...
  return EXIT_SUCCESS;
}

如果要从文件中读取汽车属性,可以重载对象以读取其成员:

struct Car_Properties
{
  public:  
    void load_by_prompting(std::istream& input,
                           std::ostream& output);
  //...
}

void
Car_Properties ::
load_by_prompting(std::istream& input,
                  std::ostream& output)
{
  output << "Enter car number: ";
  input >> number;
  output << "Enter car name: ";
  std::getline(input, name);
  // ...
}

int main()
{
  Car_Properties das_auto;

  // Input data into the variable
  das_auto.load_by_prompting(std::cin, std::cout);

  return EXIT_SUCCESS;
}

【讨论】:

  • 你能告诉我如何动态地制作汽车的对象
  • @AhsanAli:请参阅我的编辑 1。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多