首页 > 解决方案 > 在 for 循环的输出中,它显示为零并且不接受输入我无法弄清楚我在这里做错了什么

问题描述

#include <iostream>
#include <exception>
using namespace std;
class Inventory{
    friend istream& operator>>(istream&, Inventory);
    friend ostream& operator<<(ostream&, Inventory);


private:
    int stock;
    int quant;
    int price;
public:
    Inventory(int=0,int=0,double=0);
};
Inventory::Inventory(int s, int q, double p){
    stock=s;
    quant=q;
    price=p;
}
istream& operator>>(istream& in, Inventory){
    int s;
    int q;
    double p;
    cout << "Enter stock number: ";
    in >> s;
    cout << "Enter quantity: ";
    in >> q;
    cout << "Enter price: ";
    in >>p;
    Inventory inv(s,q,p);

    return in;

}
ostream& operator<<(ostream& out, Inventory inv){

    out << "The stock number is: " << inv.stock << endl << "The quantity is: " <<
    inv.quant << endl << "The price is: " << inv.price << endl;
    return out;
}
int main()
{
    Inventory inv[5]={};
    for(int i = 0; i <5; i++){
        cin >> inv[i];
    }
    cout<<"------------------------" << endl;
    for(int i=0;i<5;i++){
        cout<<inv[i];
        cout<<"------------------------" << endl;
    }



    return 0;
}

我无法弄清楚为什么当我在 for 循环中使用重载的 << 运算符时它显示为零,我是否没有将价格、数量和价格正确存储到对象数组中?我尝试过不使用构造函数并使用它没有任何区别,任何想法为什么整数和双精度不存储到数组中?

标签: loopsfor-loopostreamistream

解决方案


你很接近!在下面的代码中查看我的// change here注释标记(有几个)。

0 与 << 重载无关,而是与 >> 重载有关。

交易是您可以在 >> 重载中使用“in”istream 直接输入类变量(无需尝试调用其他函数或需要额外的变量),但您需要将 Inventory 引用传递给这样做。

我很惊讶在构造函数参数中尝试将原语设置为 0 没有错误??(我可能对较新的 C++ 处理非常“生疏”,对我来说已经超过 15 年了),但无论如何:

#include <iostream>
#include <exception>
using namespace std;
class Inventory{
    friend istream& operator>>(istream&, Inventory&); // change here (Inventory reference)
    friend ostream& operator<<(ostream&, Inventory);


private:
    int stock;
    int quant;
    double price;                     // change here
public:
    Inventory(int,int,double);        // change here
};
Inventory::Inventory(int s = 0, int q = 0, double p = 0){ // change here
    stock=s;
    quant=q;
    price=p;
}
istream& operator>>(istream& in, Inventory& inv){  // change here (inventory reference)
                                      // change here (removed variables)
    cout << "Enter stock number: ";
    in >> inv.stock;                  // change here
    cout << "Enter quantity: ";
    in >> inv.quant;                  // change here
    cout << "Enter price: ";
    in >> inv.price;                  // change here
                                      // change here (removed function call)

    return in;
}
ostream& operator<<(ostream& out, Inventory inv){

    out << "The stock number is: " << inv.stock << endl << "The quantity is: " <<
    inv.quant << endl << "The price is: " << inv.price << endl;
    return out;
}
int main()
{
    const int ITEMS = 5;              // change here (constant to replace hard-coded value)
    Inventory inv[ITEMS]={};          // change here (use the constant)
    for(int i = 0; i <ITEMS; i++){    // change here (use the constant)
        cin >> inv[i];
    }
    cout<<"------------------------" << endl;
    for(int i=0;i<ITEMS;i++){         // change here (use the constant)
        cout<<inv[i];
        cout<<"------------------------" << endl;
    }

    return 0;
}

试一试,看看结果如何:

这是我将常量(我在主函数中的更改)更改为 2 以进行基本测试运行时的示例输出:

Enter stock number: 123
Enter quantity: 4
Enter price: 2.1
Enter stock number: 467
Enter quantity: 1
Enter price: 1.2
------------------------
The stock number is: 123
The quantity is: 4
The price is: 2.1
------------------------
The stock number is: 467
The quantity is: 1
The price is: 1.2
------------------------

推荐阅读