Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,230 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,084 people online right now. Registration is fast and FREE... Join Now!




STRUCTURE help

 
Reply to this topicStart new topic

STRUCTURE help

sumlani
post 15 Mar, 2008 - 03:18 PM
Post #1


New D.I.C Head

*
Joined: 26 Jan, 2008
Posts: 26



Thanked 3 times
My Contributions


CODE

#include <iostream>
#include <iomanip>
struct PART_STRUCT
{
    char part_no[8];
    int quantity_on_hand;
    double unit_price;
};
struct 2PART_STRUCT
{
    char part_no[8];
    int quantity_on_hand;
    double unit_price;
};
struct 3PART_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;
}


I'm trying to get 3 structures to show, of cours this isn't working. I was wondering if it should be in a table?
User is offlineProfile CardPM

Go to the top of the page

Topher84
post 15 Mar, 2008 - 06:17 PM
Post #2


D.I.C Head

Group Icon
Joined: 4 Jun, 2007
Posts: 232



Dream Kudos: 25
My Contributions


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
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 09:19PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month