File to string to array

I was able to cout both files; however i am conflicted on the splitting of each string and how to place it into an array. Our professor hinted that we should use "find_first_of" and substr however she never really went into detail about how to insert it into my function, therefore i have made a giant tangle out of this program.

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
 #include <iostream> //helpfull because you do not have to space std:: before using fructrions such as cout of in 
#include <string> //i cant now use the string data type 
#include <iomanip> //allows me to use setw() and other similar functions that help manipulate the display of the data.
#include <fstream> //lets me use both input file sreams and output file streams. 
#include <sstream>
//user defined defenitions
void mainMenu();

//The following are globalizeid variables that can be accessed by any function//
int taskSelection;
int task1YearSelection;
int i = 0;
//file variables//

//****************************************************************************//
using namespace std;
int main()
{
	ifstream summary2014, summary2015;
	string usableSummary2014, usableSummary2015;
	string stringArray[5];
	do//shows statement before the condition
	{
		mainMenu(); //a void function is useful if there does not need to be a return statement or have to return more than one variable
		switch (taskSelection)
		{
		case 1://if the input is 2014 the following will happen (THIS IS TASK ONE)
			system("cls");
			cout << "You have selected Task 1 (Summary Sale)\n";
			cout << endl;
			cout << "Please enter the year you wish to view (currently there are two available 2014 and 2015): ";
			cin >> task1YearSelection;

			switch (task1YearSelection)
			{
			case 2014:
				summary2014.open("Model_X_Sale_yyyy.txt");//opens this file.

				if (summary2014.is_open())
				{
					while (getline(summary2014, usableSummary2014))
					{

						cout << usableSummary2014 << '\n';
						while (i != -1)
						{
							i = usableSummary2014.find_first_of(' ');
							cout << usableSummary2014.substr(0, i);
							usableSummary2014 = usableSummary2014(i) +1;
						}
					}
					
					summary2014.close();
				}
				else cout << "Unable to open file" << endl;
				//splitting the string into 4 sections

				cout << usableSummary2014.find(" ") << endl;
				break;

			case 2015:
				summary2015.open("Model_X_Sale_xxxx.txt");//opens this file.

				if (summary2015.is_open())
				{
					while (getline(summary2015, usableSummary2015))
					{
						cout << usableSummary2015 << '\n';
					}
					summary2015.close();
				}
				else cout << "Unable to open file" << endl;
				
				break;
			}
		}
	} 
	while (taskSelection == 9);

	system("pause");
	return 0;
}

void mainMenu()
{
	
	cout << setfill('*') << setw(47) << "*" << endl; 

	cout << endl;
	cout << setfill(' ');
	cout << setw(28) << "Welcome User!" << setw(33) << " " << endl;
	cout << endl;
	cout << endl;

	cout << "For Summary Sale in one year...Enter 1" << endl;
	cout << endl;

	cout << "For Sale Comparison between two years...Enter 2" << endl;
	cout << endl;
	cout << "To exit, enter 9" << endl;
	cout << endl;
	cout << endl;
	cout << setfill('*') << setw(47) << "*" << endl;//creative boarder
	cout << endl;
	cout << "Enter your choice: ";
	cin >> taskSelection; 
}
Last edited on
What are you trying to solve?

If you want to read data separated by white space use the operator >>

Line 49 doesn not work since you need to add 1 to i (if and only if it is >= 0).
I know about adding i to 1 however when i do such a thing an error pops up saying that the function does not take the two arguments. Not only do i want to read the data i need to split each words in the string and place it into an array. I thought i posted my data but here is what iam trying to split. All seperated by white space


Jan 2777 1.99 3223 2.19
Feb 2431 1.99 2612 2.19
Mar 2540 1.99 2729 2.19
Apr 2557 1.99 2892 2.29
May 2515 2.19 2249 2.29
Jun 2639 2.19 3190 2.29
Jul 2610 1.89 2839 2.09
Aug 2595 1.89 2828 2.09
Sep 2456 1.89 2828 2.09
Oct 2454 1.99 2803 2.19
Nov 2707 1.99 3295 2.19
Dec 2893 1.99 3522 2.19
If you intend to use find_first_of() and substr() then you need to keep track of two separate positions within the line, in effect marking the beginning and end of each substring. And then use the difference between the two as the length passed to the substr() function. After each item is found, the current end position becomes the new start position and so on. (with possible adjustment by 1 as required)

But if you have a free choice, then use a stringstream and extract each portion as previously suggested using the << operator. That would be simpler and easier.












1
2
3
4
5
6
7
8
9
10
11
12
13
14

int space = 0;

while (space != -1)

{

     space = usableSummary2014.find_first_of(' ');//look for a first space, result is 3

     cout << usableSummary2014.substr(0,space) << endl;

     usableSummary2014  = usableSummary2014.substr(space +1);
}
 


this is what i came up with. Something like this?
I do think you will need two separate markers. At the moment you have only int space.

Try something like
1
2
3
4
5
6
    int from  = 0;  // start of substring
    int to    = 0;  // end of substring (plus 1)
		
    while (to != -1)
    {
        to = usableSummary2014.find_first_of(' ', from);
and so on.

Each time through the loop, you will change from based on the current value of to
and the length of the substring will be something like to - from + 1

Last edited on
Topic archived. No new replies allowed.