如何从循环中的结构打印数据

时间:2015-05-04 02:22:35

标签: c++ for-loop

我想要一个循环来打印每个苏打水的值。 当我尝试打印machine[j].name时,会抛出一个错误,指出没有找到操作符或者没有可接受的转换。

struct Soda
{
    string name;
    double price;
    int quantity;
} a, b, c, d, e;

int main(int argc, char** argv)
{
    Soda machine[5];
    a.name = "Cola";
    a.price = .80;
    a.quantity = 20;
    b.name = "Root Beer";
    b.price = .75;
    b.quantity = 20;
    c.name = "Lemon-Lime";
    c.price = .90;
    c.quantity = 20;
    d.name = "Grape - Mango";
    d.price = .80;
    d.quantity = 20;
    e.name = "Cream";
    e.price = .80;
    e.quantity = 20;

    cout << setw(5) << "  Name" << setw(15) << " Cost"
        << setw(15) << " Stock" << setw(14) << "";
    for (int i = 0; i < 5; i++)
        for (int j = 0; j < 3; j++){
            cout << left << setw(5) << machine[j].name
                 << setw(15) << " Cost" << setw(15) << " Stock"
                 << setw(14);
    return 0;
}

3 个答案:

答案 0 :(得分:0)

看起来您正在将数组与单独变量的声明混合在一起。您已在非数组变量上设置了值,然后尝试遍历数组。

相反,您应该在数组的实际元素上设置值。

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;

struct Soda
{
    string name;
    double price;
    int quantity;
};

int main(){

    Soda machine[5];
    machine[0].name = "Cola";
    machine[0].price = .80;
    machine[0].quantity = 20;
    machine[1].name = "Root Beer";
    machine[1].price = .75;
    machine[1].quantity = 20;
    machine[2].name = "Lemon-Lime";
    machine[2].price = .90;
    machine[2].quantity = 20;
    machine[3].name = "Grape - Mango";
    machine[3].price = .80;
    machine[3].quantity = 20;
    machine[4].name = "Cream";
    machine[4].price = .80;
    machine[4].quantity = 20;


    cout << "Name Cost Stock" << endl;
    for (int i = 0; i < 5; i++)
        cout << machine[i].name << " " << machine[i].price << " " << machine[i].quantity << endl; 
}

答案 1 :(得分:0)

您正在尝试打印出对象(对象数组)machine[i]的内容,而是为某些不同的对象指定了值:ab,{{1 },cd
解决方案:
1.打印ea.name等 2.或代替

b.name

写道:

a.name = "Cola";
a.price = .80;

答案 2 :(得分:0)

由于您将此标记为C ++,我建议您尝试使用矢量(用于机器)。

int main()
{
    vector<Soda> machine;

    Soda tmpSoda;

    {
       tmpSoda.name = "Cola";
       tmpSoda.price = .80;
       tmpSoda.quantity = 20;
       machine.push_back(tmpSoda);
    }

    {
       tmpSoda.name = "Root Beer";
       tmpSoda.price = .75;
       tmpSoda.quantity = 20;
       machine.push_back(tmpSoda);
    }

    // ....
    {
       tmpSoda.name = "Cream";
       tmpSoda.price = .80;
       tmpSoda.quantity = 20;
       machine.push_back(tmpSoda); 
    }

现在所有苏打水都在向量中(命名为机器),因此您现在可以在循环中报告每个元素。

    cout << "Name Cost Stock" << endl;
    for (int i=0; i<machine.size(); ++i)
    {
        cout << machine[i].name << " " 
             << machine[i].price << " " 
             << machine[i].quantity << endl; 
    }

由于您将此标记为C ++,我建议您尝试使用类实例的向量。注意ctor如何填写每个Soda的值,“show”方法使用每个实例的私有数据属性。

class Soda
{
public:
   Soda(std::string aName,
        double      aPrice,
        int         aQuantity) :
      name(aName),
      price(aPrice),
      quantity(aQuantity)
      {
      }

   ~Soda() {
      name.erase();
      quantity = 0;
   }

   std::string show() {
      std::stringstream ss;
      ss << std::right << std::setw(14) << name  << "   "
         << std::left  << std::setw(8)  << price << " "
         << std::setw(7) << quantity;
      return (ss.str());
   }

   static std::string showHdr() {
      std::stringstream ss;
      ss << std::right << std::setw(14) << "Name"  << "   "
         << std::left  << std::setw(8)  << "Price" << " "
         << std::left  << std::setw(7)  << "Stock";
      return (ss.str());
   }

private:
   std::string name;
   double      price;
   int         quantity;
};

Main现在缩减为:

   std::vector<Soda> machine;

   // load the machine with soda's
   machine.push_back(Soda("Cola", .80, 20));
   machine.push_back(Soda("Root Beer", .75, 20));
   machine.push_back(Soda("Lemon-Lime", .90, 20));
   machine.push_back(Soda("Grape - Mango", .80, 20));
   machine.push_back(Soda("Cream", .80, 20));

   // show machine inventory
   {
      // hdr
      std::cout << Soda::showHdr() << std::endl;

      for (auto i : machine)
         std::cout << i.show() << std::endl;
   }