How to compare price which is read from the file?

Hi. I'm trying to compare prices which are read from the file along with other information. The input file is as shown below:
The file data is something like this:

Date      Time,     Price,Volume, Value, Condition
7/5/2014 12:12:12 PM, 4.3, 2000, 450030, AB CD
7/5/2014 12:11:30 PM, 5.4, 32000, 65930, 
7/5/2014 12:09:32 PM, 3.9, 2899, 560030, CD
.
.
.
7/5/2014 10:50:04 AM 6.4, 8750, 123789, F


And, currently, I have these classes.
Shares.h and Shares.cpp, List.h (Vector), date.h and date.cpp, time.h and time.cpp, and finally MainTest.cpp.
In my MainTest.cpp, I have the following code to read the input file.
And under each case in the switch statement, I'm supposed to operate as written below. But, I'm not sure how to compare the prices. Do I create a "compare" function in the vector class and use it here? Or do I create it here?

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Program to test the class listType
#include <iostream>
#include <fstream>
#include <iomanip> 
#include "List.h"
#include "Shares.h"
using namespace std;

void ReadShares();
//void GetShares();
//void GetHighestPrice();
//void GetLowestPirce();

int main()
{
	int menu;

	ReadShares();

	cout << "The Menu Option available are as follows - " << endl;
	cout << "1: Date, highest price and Start time(s) of the highest share price" << endl;
	cout << "2: Date, Lowest price, Stat time(s) of the lowest share price" << endl;
	cout << "3: output.csv" << endl;
	cout << "4: Exit the program" << endl;
	cout << "Enter the option:";
	cin >> menu;
	cout << endl;

	switch (menu)
	{
	case '1':
		
		break;
	case '2':

		break;
	case'3':

		break;
	case'4':

		break;
	default:
		cout << "Invalid option!" << endl;
	}

	system("PAUSE");
	return 0;
}

void ReadShares()
{
	ifstream inFile("Couse_of_sales.txt");

	inFile.ignore(100, '\n');
	inFile.ignore(100, '\n');

	List<Shares> shareList;
	string dateTime;

	while (getline(inFile, dateTime, ','))
	{
		string d1 = dateTime.substr(0, 2);
		int d = atoi(d1.c_str());

		string m1 = dateTime.substr(3, 2);
		int m = atoi(m1.c_str());

		string y1 = dateTime.substr(6, 4);
		int y = atoi(y1.c_str());

		string hr1 = dateTime.substr(11, 2);
		int hr = atoi(hr1.c_str());

		string min1 = dateTime.substr(14, 2);
		int min = atoi(hr1.c_str());

		string sec1 = dateTime.substr(17, 2);
		int sec = atoi(sec1.c_str());

		string ap = dateTime.substr(20, 2);
		if (ap == "PM" && hr != 12)
		{
			hr = hr + 12;
		}

		string priceStr;
		getline(inFile, priceStr, ',');
		double tPrice = atof(priceStr.c_str());

		string volStr;
		getline(inFile, volStr, ',');
		int vol = atoi(volStr.c_str());

		string valStr;
		getline(inFile, valStr, ',');
		double val = atof(valStr.c_str());

		string cond;
		getline(inFile, cond, ',');

		date date1(d, m, y);
		time24 time1(hr, min, sec);
		
		Shares s1(d, m, y, hr, min, sec, tPrice, vol, val, cond);
		shareList.insert(s1);
	}
	
}
I started looking at this code, though without the various headers, much of the code is missing. Nevertheless I worked around that and started to test it, but ran into problem with the date/time parsing.

From the given data file, the first row of data looks like this:
7/5/2014 12:12:12 PM, 4.3, 2000, 450030, AB CD
after the getline() at line 61, dateTime contains this: "7/5/2014 12:12:12 PM" and when it is broken into substrings, we have:
1
2
3
d1   = "7/"
m1   = "/2"
y1   = "14 1"

1
2
3
hr1  = ":1"
min1 = ":1"
sec1 = " P"

looks pretty badly broken as far as i can tell.

I'd recommend an alternative approach using a stringstream to extract the components of the date and time.

edit:
suggested code, replace lines 63 to 81 with this :
63
64
65
66
67
68
        std::istringstream dttm(dateTime);
        char c1, c2, c3, c4;
        int d, m, y, hr, min, sec;
        std::string ap;
        dttm >> d >> c1 >> m >> c2 >> y;
        dttm >> hr >> c3 >> min >> c4 >> sec >> ap;


Also, the wrong delimiter is used at line 100,
 
    getline(inFile, cond, ',');

should be:
 
    getline(inFile, cond, '\n');


Question: is there a reason why you don't appear to be using std::vector or std::list as appropriate?
Last edited on
Hi, sorry. I wrote the input file wrong.
The date is in dd/mm/yyyy format, time is in hh:mm:ss AM/PM, price is double, volume is int, value is double and condition is string.
The input file is like this:


Date Time, Price,Volume, Value, Condition
07/05/2014 12:12:12 PM, 4.3, 2000, 450030, AB CD
07/05/2014 12:11:30 PM, 5.4, 32000, 65930,
07/05/2014 12:09:32 PM, 3.9, 2899, 560030, CD
.
.
.
07/05/2014 10:50:04 AM 6.4, 8750, 123789, F
Last edited on
Thanks for the update. Fortunately my suggested changes still work just as well with your modified data. My current version:
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <sstream>
#include <list>

void ReadShares();
//void GetShares();
//void GetHighestPrice();
//void GetLowestPirce();

using std::cout;
using std::endl;
using std::cin;

int main()
{
    char menu = ' ';

    ReadShares();
    do {
        cout << "The Menu Options available are as follows - " 
            "\n1: Date, highest price and Start time(s) of the highest share price"
            "\n2: Date, Lowest price, Stat time(s) of the lowest share price"
            "\n3: output.csv"
            "\n4: Exit the program"
            "\n\nEnter option: ";
        cin >> menu;
        cout << endl;
    
        switch (menu)
        {
            case '1':
                break;
                
            case '2':
                break;
                
            case'3':
                break;
                
            case'4':
                cout << "Exit, thank you" << endl;
                break;
                
            default:
                cout << "Sorry, invalid option" << endl;
        }
    }
    while (menu != '4');
    
    return 0;
}

class Date {
    int dd;
    int mm;
    int yyyy;
public:
    Date() : dd(1), mm(1), yyyy(1900) { }
    Date(int d, int m, int y) : dd(d), mm(m), yyyy(y) { }
    friend std::ostream & operator<<(std::ostream & os, const Date & dt)
    {
        return os << dt.dd << '/' << dt.mm << '/' << dt.yyyy;
    }
};

class Time24 {
    int hh;
    int mm;
    int ss;
public:
    Time24() : hh(0), mm(0), ss(0) { }
    Time24(int h, int m, int s) : hh(h), mm(m), ss(s) { }

    friend std::ostream & operator<<(std::ostream & os, const Time24 & tm)
    {
        char f = os.fill('0');
        os << std::setw(2) << tm.hh << ':'
           << std::setw(2) << tm.mm << ':'
           << std::setw(2) << tm.ss;
        os.fill(f);
        return os;
    }
};

class Shares {
    Date        date;
    Time24      time;
    double      price;
    int         volume;
    double      value;
    std::string cond;

public:
    Shares() : price(0), volume(0), value(0) { }

    Shares(Date dt, Time24 tm, double prc, int vol, double val, std::string cnd)
        : date(dt), time(tm), price(prc), volume(vol), value(val) , cond(cnd) { }

    friend std::ostream & operator<<(std::ostream & os, const Shares & sh)
    {
        return os << sh.date << ' '
                  << sh.time << ' '
                  << std::setw(10) << sh.price
                  << std::setw(10) << sh.volume
                  << std::setw(10) << sh.value
                  << ' ' << std::left  << sh.cond << std::right ;
    }
};

void ReadShares()
{
    std::ifstream inFile("Couse_of_sales.txt");

    inFile.ignore(100, '\n');
    inFile.ignore(100, '\n');

#define List std::list

    List<Shares> shareList;

    
    std::string line;

    while (std::getline(inFile, line))
    {    
        std::istringstream ss(line);
        
        std::string dateTime;
        std::getline(ss, dateTime, ',');
        std::istringstream dttm(dateTime);
        char c1, c2, c3, c4;
        int d, m, y, hr, min, sec;
        std::string ap;
        dttm >> d >> c1 >> m >> c2 >> y;
        dttm >> hr >> c3 >> min >> c4 >> sec >> ap;

        if (ap == "PM" && hr != 12)
        {
            hr += 12;
        }
        
        Date date1(d, m, y);
        Time24 time1(hr, min, sec);
        
        double tPrice, val;
        int vol;
        std::string cond;   
             
        ss >> tPrice >> c1;
        ss >> vol    >> c2;
        ss >> val    >> c3;       
        getline(ss, cond);

        Shares s1(date1, time1, tPrice, vol, val, cond);
        cout << s1 << endl;
        shareList.push_back(s1);
    }
}
Last edited on
... and I realise that none of this answers your original question, "How to compare price which is read from the file?".

If all you need is to identify the hugest or lowest share price, it's just a matter of iterating through the list, and comparing the price (use a member function to access it) with a value which you will store. Save either the Shares object or an iterator or subscript to the highest/lowest found so far.
bro can i see your work?
i need a reference for my assignment
i'm new to C++
i have no idea how to do my assignment
i will really appreciate your help
thanks for your help
Topic archived. No new replies allowed.