read file small problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef INVENTORY_H
#define	INVENTORY_H

#include<iostream>
#include<string>
#include<fstream>

using namespace std;

class Inventory {
public:
    Inventory();
    
    void setItemName(string name);
    void setModel(string model);
    void setPrice(double price);
    void setQuantity(int quantity);
    
    string getItemName();
    string getModel();
    double getPrice();
    int getQuantity();
    
    
private:
    string name;
    string model;
    double price;
    int quantity;
};

void recordStore();

#endif	/* INVENTORY_H */




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "Inventory.h"

Inventory::Inventory() {
    name = "";
    model = "";
    price = 0.00;
    quantity = 0;
}

void Inventory::setItemName(string name){
    this->name = name;
}

void Inventory::setModel(string model){
    this->model = model;
}

void Inventory::setPrice(double price){
    this->price = price;
}

void Inventory::setQuantity(int quantity){
    this->quantity = quantity;
}

string Inventory::getItemName(){
    return name;
}

string Inventory::getModel(){
    return model;
}

double Inventory::getPrice(){
    return price;
}

int Inventory::getQuantity(){
    return quantity;
}

void recordStore(){
    Inventory *inven;
    int index = 0;
    
    string filename = "";
    cout << "Enter fileName : ";
    getline( cin , filename );
    
    ifstream inFile;
    inFile.open(filename.c_str());
    if( inFile.is_open() ){
        cin.ignore();
        cout << "Read data-ing..." << endl;

        inFile >> index;

        inven = new Inventory[index];

        string itemname  = "";
        string modelname = "";
        double pricing   = 0.00;
        int quantity     = 0;

        for( int i = 0 ; i < index ; i++ ){
            getline( inFile , itemname );
            inven[index].setItemName(itemname);
            getline( inFile , modelname );
            inven[index].setModel(modelname);
            inFile >> pricing;
            inven[index].setPrice(pricing);
            inFile >> quantity;
            inven[index].setQuantity(quantity);
        }
        for( int i = 0 ; i < index ; i++ ){
            cout << "Item Name  : " << inven[index].getItemName() << endl;
            cout << "Model Name : " << inven[index].getModel()    << endl;
            cout << "Price      : " << inven[index].getPrice()    << endl;
            cout << "Quantity   : " << inven[index].getQuantity() << endl;
        }
    }
    else{
        cout << "Fail's open fail due empty " << endl;
        }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//txt file
4
Hitachi
HTC2011-HDD-500TB
500
30
Samsung
ST-56/2-PLASMA
4500
70
LG
LG-ST-244/TV
5999
55
Toshiba
XXT-46SV
1999
100


can i ask what thing that i might be wrong?
i know it's not a big deal right
┬┐what's the problem?
i cannot show out the inFile data .
i dont know when i read file error or display out error
`inven[index]' is out of bounds.

Also, http://www.cplusplus.com/forum/beginner/14595/#msg74555
Last edited on
Topic archived. No new replies allowed.