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.
#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//
//****************************************************************************//
usingnamespace 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;
}
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.
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);
}