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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int ReadFile (ifstream&, string);
int main ()
{
ifstream inFile;
string filename = "Shares.txt";
bool IsItOpen;
string StockName;
int NUMofShares;
float PurchasePrice, SalePrice, PurchaseCommission, SaleCommission ;
cout << "Please open the file associated with the Multiple Stock Sales.\n" << endl;
inFile.open(filename);
IsItOpen = ReadFile(inFile, filename);
cout << " Stock, Shares, P Price, S Price, P Commission, S Commission" << endl;
while (!inFile.eof())
{
getline (inFile, StockName);
inFile >> NUMofShares;
inFile >> PurchasePrice;
inFile >> SalePrice;
inFile >> PurchaseCommission;
inFile >> SaleCommission;
cout << StockName << NUMofShares << PurchasePrice << SalePrice << PurchaseCommission << SaleCommission << endl;
}
inFile.close();
system("pause");
return 0;
}
int ReadFile (ifstream& myFile, string NameOfFile)
{
cout << "Type the name of the file." << endl;
cout << "Example Format: StockPrices.txt" << endl;
cout << "\nPlease Enter Here: ";
cin >> NameOfFile; //prompting user to enter the filename
if (myFile.is_open() && NameOfFile == "Shares.txt")//validating the filename that was entered
{
cout << NameOfFile << " has opened successfully.\n" << endl;
return true;
}
else //if an error occurred opening the file, the program will close
{
cout << NameOfFile << " couldn't be opened...\n" << endl;
cout << "Closing down the program due to file error..." << endl;
return -1;
}
}
|