Assume that you have the following definition of a struct: struct partsType { St
ID: 3538102 • Letter: A
Question
Assume that you have the following definition of a struct:
struct partsType
{
String partName;
Int partNum;
double price;
int quantitiesInStock;
};
Declare an array, inventory, of 100 components of type partsType.
Now take above struct definition above and:
Here is what I have so far.
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
// Struct definition
struct partsType
{
string partName;
int partNum;
double price;
int quantitiesInStock;
};
// This is my main function. This is where my program starts.
int main()
{
partsType inventory[100] = {};
for (int i = 0; i < 100; i++)
{
inventory[i].partName = "";
inventory[i].partNum = -1;
inventory[i].price = 0.0;
inventory[i].quantitiesInStock = 0;
}
for (int i = 0; i < sizeof(inventory); i++)
{
cout << inventory[i].partName << endl;
cout << inventory[i].partNum << endl;
cout << inventory[i].price << endl;
cout << inventory[i].quantitiesInStock << endl;
}
getch ();
return 0;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
// Struct definition
struct partsType
{
string partName;
int partNum;
double price;
int quantitiesInStock;
};
// This is my main function. This is where my program starts.
int main()
{
struct partsType inventory[100] = {};
int length = 100;
for (int i = 0; i < 100; i++)
{
inventory[i].partName = "";
inventory[i].partNum = -1;
inventory[i].price = 0.0;
inventory[i].quantitiesInStock = 0;
}
for (int i = 0; i <length; i++)
{
cout << inventory[i].partName << endl;
cout << inventory[i].partNum << endl;
cout << inventory[i].price << endl;
cout << inventory[i].quantitiesInStock << endl;
}
getch ();
return 0;
}