Defines and uses a struct. : Struct « Data Type « C++






Defines and uses a struct.

Defines and uses a struct.
 
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct Product 
{
  string name;        
  double sales;       
};
inline void print( const Product& v)
{
   cout << fixed << setprecision(2)
        << left  << setw(20) << v.name
        << right << setw(10) << v.sales << endl;
}
int main()
{
   Product productObject, productObject2;
   productObject.name    = "one";
   productObject.sales   = 37.37;
   productObject2.name   = "two";
   productObject2.sales  = 231.23;
   productObject.sales   += 17.11;
   cout << "  Product            Sales\n"
        << endl;
   print( productObject);
   print( productObject2);
   cout << "\nTotal of sales: "
        << productObject.sales + productObject2.sales << endl;
   Product *ptr = &productObject2;         
   if( productObject2.sales < productObject.sales)         
     ptr = &productObject;
   cout << "\nSalesman of the month: " << ptr->name << endl;   
                                
   return 0;
}


           
         
  








Related examples in the same category

1.example about structures
2.pointers to structures
3.array of structures
4.uses parts inventory to demonstrate structures
5.demonstrates nested structures
6.demonstrates passing structure by reference
7.demonstrates passing structure as argument