i am currently writing a program that loads portfolio.txt when you type "L", displays portfolio.txt when you type "D", and exits when you type "Q". I was successful in writing a program that did this with the first set of data but it fails to finish loading after i added the arrays and eof while loop.
#include <iostream>
#include <iomanip>
#include <fstream>
usingnamespace std;
int main()
{
char answer;
fstream data;
string fundName[3];
int i = 0, numberOfShares[3];
double purchasePrice[3], currentValue[3];
cout << fixed << showpoint;
cout << left;
// Original menu that tells user to type "L" for "load from file," "D" for "display," and "Q" to "quit."
cout << setw(5) << " " << "Menu" << endl;
cout << setw(5) << "L" << "load from file" << endl;
cout << setw(5) << "D" << "display" << endl;
cout << setw(5) << "Q" << "quit" << endl;
// answer holds menu option
cin >> answer;
//run through the loop as long as answer is not Q for Quit
while (answer != 'Q')
{
cin.clear(); //restore input stream
cin.ignore(200,'\n'); //clear the buffer
// if you type "L" you load information from "funds.txt"
if (answer == 'L')
{
data.open("portfolio.txt",ios::in);
while (!data.eof() )
{
getline(data,fundName[i]);
data >> numberOfShares[i];
data >> purchasePrice[i];
data >> currentValue[i];
i++;
}
}
//if you type "D" you display the loaded data or output "no data"
elseif (answer == 'D')
{
// if "portfolio.txt is open then display "portfolio.txt"
if (data.is_open())
{
while (i<40)
{
cout << fundName[i] << endl;
cout << numberOfShares[i] << endl;
cout << setprecision(2);
cout << purchasePrice[i] << endl;
cout << setprecision(2);
cout << currentValue[i] << endl << endl;
i++;
}
}
//if "funds.txt" is not open then display "no data"
else
{
cout << "no data" << endl;
}
}
// display menu after option is input
cout << setw(5) << " " << "Menu" << endl;
cout << setw(5) << "L" << "load from file" << endl;
cout << setw(5) << "D" << "display" << endl;
cout << setw(5) << "Q" << "quit" << endl;
cin >> answer; //holds new menu in answer
}
data.close(); //closes data
system("pause");
return 0;
}
portfolio.txt:
Michael 1 $ Co
1
1.00
2.00
Michael 2 $ Co
2
1.00
2.00
Michael 3 $ Co
3
1.00
2.00
can someone please help?