我有一个用于类的项目,但不确定该程序应使用哪种类型的数组。我必须制定一个股票市场计划,以便用户购买,出售和查看股票清单并检查其帐户余额。有两个包含以下数据的文本文件:
Leon1111 5000.00
Wise2222 10000.00
Woo3333 3000.00
White4444 7000.00
Head5555 4000.00
和
Apple AAPL 450.00
Boeing BA 75.50
Intel INTC 22.30
Rambus RMBS 5.55
Sirius SIRI 3.15
Skyworks SWKS 25.35
Xilinx XLNX 36.80
这是我到目前为止编写的代码:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
ofstream outStream;
int option;
do
{
cout << "1) Check Stock Listings " << endl;
cout << "2) Buy Stock " << endl;
cout << "3) Sell Stock" << endl;
cout << "4) Check Account Balance " << endl;
cout << "5) Quit " << endl << endl;
cout << "Please select an option : ";
cin >> option;
cout << endl;
if (option == 1)
{
fstream CompaniesFile;
CompaniesFile.open("Companies.txt");
if (CompaniesFile.is_open())
{
string s;
while (getline(CompaniesFile, s, '\n'))
{
cout << s << endl;
}
}
CompaniesFile.close();
}
else if (option == 2)
{
}
else if (option == 3)
{
}
else if (option == 4)
{
fstream AccountFile;
AccountFile.open("Account.txt");
if (AccountFile.is_open())
{
string t;
while (getline(AccountFile, t))
{
cout << t << endl;
}
}
AccountFile.close();
}
else if (option == 5)
{
cout << "Program Terminated. Have a nice day!" << endl << endl;
}
else
{
cout << "Invalid Option entered" << endl;
}
}
while (option != 5);
return 0;
}
最佳答案
class cCompany
{
std::string myName;
std::string mySymbol;
double myPrice;
public:
cCompany( const std::string& name,
const std::string& symbol,
double price )
: myName( name ), mySymbol( symbol ), myPrice( price )
{}
};
std::vector< cCompany > vCompany;
class cAccount
{
std::string myName
double myBalance;
public:
cAccount( const std:string& name, double balance )
: myName( name ), myBalance( balance )
{}
};
std:vector< cAccount > vAccount;
...
std::string name;
std::string symbol;
double price;
while ( CompaniesFile.good() )
{
CompaniesFile >> name;
CompaniesFile >> symbol;
CompaniesFile >> price;
vCompany.push_back( cCompany( name, symbol, price ));
}