为什么输出是地址?

时间:2019-03-10 00:56:18

标签: c++ arrays linked-list

考虑这是我的代码:

 void SportShoe::AddSportShoe() {   

    SportShoe MySepatu[SIZE];
    int numOfShoe = 0;
    nodeSport *tempShoe1; 

    while (numOfShoe < SIZE){
        cout << "Please enter the Shoe ID : (eg. 23210) " << endl;
        cin >> SportShoe::MySepatu[numOfShoe].ShoeID;

        cout << "Please enter the Shoe Brand: (eg. Adidas) " << endl;
        cin.sync();
        cin.getline(SportShoe::MySepatu[numOfShoe].BrandShoe,100); 

        cout << "Please enter the price of the Shoe : (eg. RM123.22) " << endl;
        cin >> SportShoe::MySepatu[numOfShoe].PriceShoe;
        cout << endl;

    //passing the value from 'MySepatu' to 'tempShoe'
        SportShoe::MySepatu[numOfShoe].ShoeID = (tempShoe1->ShoeIDList);
        SportShoe::MySepatu[numOfShoe].BrandShoe[100] = (tempShoe1->BrandShoeList[100]);
        SportShoe::MySepatu[numOfShoe].PriceShoe = (tempShoe1->PriceShoeList);

   //i do some dummy to see what stored in tempShoe1
       cout << "ID =>> " << tempShoe1->ShoeIDList << endl;
       cout << "Brand =>> " << tempShoe1->BrandShoeList << endl;
       cout << "Price =>> " << tempShoe1->PriceShoeList << endl;
    }

    while (numOfShoe >= SIZE-1){
       cout << ">> List is already full !! <<"; 
       system("PAUSE");
       MenuSportShoe();
    }
}

该代码可以照常运行,并且在编译器上不显示任何错误。但是,当到达“ cout”区域时,它将打印出类似指针地址的内容。没有价值。 实际上,我认为我使用了错误的方法来传递值。我不知道如何分配它们。

//这是我的课堂宣言

class SportShoe  {
private:
    struct nodeSport {
        int ShoeIDList;
        char BrandShoeList[100]; 
        float PriceShoeList; 
        nodeSport *last;
    };
    nodeSport *first = NULL; 

    struct Shoe  {
        int ShoeID;
        char BrandShoe[100]; 
        float PriceShoe; 
    }MySepatu[SIZE];

public:
    void AddSportShoe();

};

*我使用数组,因为我想设置节点的限制。链接列表对于其他功能(如“删除”,“显示”等)很有用。

有解决此问题并升级我的代码的意见吗?

1 个答案:

答案 0 :(得分:1)

不要使用c字符串,请使用std :: string。不要使用数组,请使用std :: array或std :: vector或类似的容器。这肯定会解决您的问题。

class SportShoe  {
private:
    struct nodeSport {
        int ShoeIDList;
        std::string BrandShoeList; 
        float PriceShoeList; 
        nodeSport *last;
    };
    nodeSport *first = NULL; 

    struct Shoe  {
        int ShoeID;
        std::string BrandShoe; 
        float PriceShoe; 
    };
    std::array<Shoe, SIZE> MySepatu;

public:
    void AddSportShoe();
};

void SportShoe::AddSportShoe() {   
    std::array<SportShoe, SIZE> MySepatu;
    int numOfShoe = 0;
    nodeSport *tempShoe1; 

    while (numOfShoe < SIZE){
        cout << "Please enter the Shoe ID : (eg. 23210) " << endl;
        cin >> SportShoe::MySepatu[numOfShoe].ShoeID;

        cout << "Please enter the Shoe Brand: (eg. Adidas) " << endl;
        cin.sync();
        std::getline(cin, SportShoe::MySepatu[numOfShoe].BrandShoe); 

        cout << "Please enter the price of the Shoe : (eg. RM123.22) " << endl;
        cin >> SportShoe::MySepatu[numOfShoe].PriceShoe;
        cout << endl;

    //passing the value from 'MySepatu' to 'tempShoe'
        SportShoe::MySepatu[numOfShoe].ShoeID = tempShoe1->ShoeIDList;
        SportShoe::MySepatu[numOfShoe].BrandShoe = tempShoe1->BrandShoeList;
        SportShoe::MySepatu[numOfShoe].PriceShoe = tempShoe1->PriceShoeList;

   //i do some dummy to see what stored in tempShoe1
       cout << "ID =>> " << tempShoe1->ShoeIDList << endl;
       cout << "Brand =>> " << tempShoe1->BrandShoeList << endl;
       cout << "Price =>> " << tempShoe1->PriceShoeList << endl;
    }

    while (numOfShoe >= SIZE-1){
       cout << ">> List is already full !! <<"; 
       system("PAUSE");
       MenuSportShoe();
    }
}