Passing string array problem and accessing class declaration

Hi to c++ guru~~

I am having the following probles;

1. I keep getting an error. I believe it is related to the pointer to the string array Date.
By step by step debugging, the code crashes at "int size = Load_Price_Data(file_name, sort_method, sort_preference, Date, Price);" in the main.
If I pass int array instad of string array, I do not get an error.

2. How can I make my class declaration accessible to any other cpp files.
I tried to create a header file but I get an error and if I put the declaration code in the independent cpp file, other cpp files can't access the class.


Code:
<main.cpp>
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
using namespace std;

int Load_Price_Data(string file_name, string sort_method, string sort_preference, string* Date, double* Price);

int main()
{
	ofstream OutPut("OutPut.txt");

	//Load data and sort them
	string file_name = "SPXPricesFromCBOE.txt";
	string sort_method = "BubbleSort";
	string sort_preference = "ascending";
	string* Date = new string[251]; double* Price = new double[251];

	int size = Load_Price_Data(file_name, sort_method, sort_preference, Date, Price);

	//test
	for(int i = 0; i < size; i++)
	{
		cout << Date[i] << "," << Price[i] << endl;
	}
	system("pause");[code]

}[/code]


<Load.cpp>
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
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>

using namespace std;

class DATE
{
	public:
	string Date;
	int DateValue()
	{
		int m, d, y; string temp;

		//Breakdown the date string into day, month, and year
		temp = Date.substr(0,2);
		m = atoi(temp.c_str());
		temp = Date.substr(3,2);
		d = atoi(temp.c_str());
		temp = Date.substr(6,4);
		y = atoi(temp.c_str());

		return ((y*10000) + (m*100) + d); 
	}
};

int Load_Price_Data(string file_name, string sort_method, string sort_preference, string* Date, double* Price)
{
	ifstream File(file_name);
	if(!File)
	{
		cout << "ERROR" << endl;
		system("pause");
		return -1;
	}

	string temp;
	vector<string> DATA;
	while(!File.eof())
	{
		File >> temp;
		DATA.push_back(temp);
	}
	int size = DATA.size();


	//Splitting DATA into Date and Price vectors
	string* DD = new string[size]; double* PP = new double[size];
	for(int i = 0; i < size; i++)
	{
		DD[i] = DATA[i].substr(0,10);
		temp = DATA[i].substr(11);
		PP[i] = atof(temp.c_str());
	}

	//Sort
	if(sort_method == "QuickSort")
	{
	}
	else
	{
		int swap = 1;
		string date; double price; DATE A, B;
		if(sort_preference == "ascending")
		{
			while(swap != 0)
			{
				swap = 0;
				for(int k = 0; k < size; k++)
				{
					A.Date = DD[k]; B.Date = DD[k+1];
					if(A.DateValue() > B.DateValue())
					{
						date = DD[k]; price = PP[k];
						DD[k] = DD[k+1]; PP[k] = PP[k+1];
						DD[k+1] = date; PP[k+1] = price;
						swap++;
					}
				}
			}
			
		}
		else
		{

		}
	}

	Date = new string[size]; Price = new double[size];
	for(int h = 0; h < size; h++)
	{
		Date[h] = DD[h]; Price[h] = PP[h];
	}
	return size;
}


Hello,

2.In order to use a predefined name(identificator) it must be in a preceding file with
its extension name *.h or *.cpp depending on a compiler. Some do not accept other than
these and an OS may hide them to disrupt the searching.
You can not make one file that uses all. One after another only.

1. I am not sure what is this string type consist of but in C++ a type denote is not quite
the type. Probable it is already a pointer or something addressing to.
Topic archived. No new replies allowed.