CODE
#include <iostream>
#include <iomanip>
using namespace std;
struct PART_STRUCT
{
char part_no[8];
int quantity_on_hand;
double unit_price;
};
int main()
{
PART_STRUCT part;
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
cout << "Enter the sever-character part number: ";
cin.getline(part.part_no, 8);
cout << endl;
cout <<"Enter the quantity on hand: ";
cin >> part.quantity_on_hand;
cout << endl;
cout << "Enter the unit price: ";
cin >> part.unit_price;
cout << endl;
cout << "You entered the following information:" << endl << endl;
cout << "Part Number: " << part.part_no << endl;
cout << "Quantity On Hand: " << setw(7) << part.quantity_on_hand
<< endl;
cout << "Unit Price: " << setw(7) << part.unit_price << endl;
return 0;
}
1. You need using namespace std
2. When you make an object from a struct, it is just like using a class.. therefore where you said part_struct it should be PART_STRUCT.
3. You can just declare 3 parts like
CODE
PART_STRUCT Part_ONE;
PART_STRUCT Part_TWO;
PART_STRUCT PART_THREE;
THis will make 3 part objects.. you can then store them in a vector (or array) by doing:
CODE
vector<PART_STRUCT> StoredParts;
StoredParts.push_back(Part_ONE);
StoredParts.push_back(Part_TWO);
StoredParts.push_back(Part_THREE);
You can then access each one by using a for loop like:
CODE
vector<PART_STRUCT>::iterator ITV = StoredParts.begin(); ITV != StoredParts.end(); ITV++)
{
cout << ITV->.part_no << endl;
cout << ITV->quantity_on_hand << endl;
cout << ITV->unit_price << endl;
}
Resulting in:
CODE
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
struct PART_STRUCT
{
char part_no[8];
int quantity_on_hand;
double unit_price;
};
const int NUMBER_OF_PARTS = 3;
int main()
{
vector<PART_STRUCT> StoredParts;
//Get your part information for each part
for(int Index = 0; Index < NUMBER_OF_PARTS; Index++)
{
PART_STRUCT tempPart;
cout << "Enter the sever-character part number: ";
cin.getline(tempPart.part_no, 8);
cout <<"\nEnter the quantity on hand: ";
cin >> tempPart.quantity_on_hand;
cout << endl;
cout << "\nEnter the unit price: ";
cin >> tempPart.unit_price;
cout << endl << endl;
StoredParts.push_back(tempPart);
}//end for
//Display part information
for(vector<PART_STRUCT>::iterator ITV = StoredParts.begin(); ITV != StoredParts.end(); ITV++)
{
cout << "You entered the following information:" << endl << endl;
cout << "Part Number: " << ITV->part_no << endl;
cout << "Quantity On Hand: " << setw(7) << ITV->quantity_on_hand << endl;
cout << "Unit Price: " << setw(7) << ITV->unit_price << endl << endl << endl;
}//end for
return 0;
}
by using a for lop and declaring a new part each time.. you can avoid having to individually declare parts!
Fix your getline problem and you'll be fine.... Unless it has to be a character array Iw ould make your part number a string.. if it must be 8 charcaters you can do something like
CODE
string part_no;
cout << "Enter part Number: ";
cin >> part_no;
while(part_no.size() > 8)
{
cout << "Part no must be 8 chars or less" << endl;
cout << "\nEnter Part Number: ";
cin >> part_no;
}
This post has been edited by Topher84: 15 Mar, 2008 - 06:21 PM