我是編程新手,所以我可以使用你的幫助與我的代碼。我想讀取一個數據格式像這樣一個現有的文件:我張貼了我所有的代碼C++:讀取分隔文件並存儲在結構中
2012-09-22|microsoft|0|102.36|100
2012-09-22|apple|0|203.12|100
2012-10-05|ibm|0|141.96|100
2012-10-05|boeing|0|123.65|100
2012-11-03|sears|0|23.36|100
2012-11-29|toyota|0|78.45|100
,但相關功能是無效loadData(交易allTransaction);功能。我從這裏找到的多個示例中共同破解了這些代碼,但它並不適合我。我正在嘗試讀取數據,然後填充allTransaction結構的變量。然後我想將該結構推送到數據向量。我想循環每一行,直到到達文件末尾。它看起來像我沒有得到填充結構的值。請幫忙!
我一直在做這個編程2個月現在爲學校,所以請保持您的響應noob級別。
感謝
#define _CRT_SECURE_NO_DEPRECATE // Declaration to use deprecated code in Visual Studio.
#include <iostream> // Include the iostream standard file.
#include <string> // Include the string library.
#include <fstream> // Include to manipulate files.
#include <vector> // Include to use vectors.
#include <sstream> // Include to use stringstream.
#include <ctime> // Include to calculate time.
using namespace std;
// Create transactions structure
struct transaction
{
/*int user = 0;*/
string date = "";
string stock = "";
bool sale = 0;
double price = 0.0;
int shares = 0;
};
// Create vector
vector <transaction> data;
// Function declarations
void loadData(transaction allTransaction);
void buyShares(transaction allTransaction);
void sellShares(transaction allTransaction);
void storeData(transaction allTransaction);
int main()
{
// Declare variables
transaction allTransaction; // Declaring a structure.
loadData(allTransaction);
cout << "Date: " << allTransaction.date <<
"Name: " << allTransaction.stock <<
"Sale?: " << allTransaction.sale <<
"Price: " << allTransaction.price <<
"Shares: " << allTransaction.shares;
//Pause program
cout << "Press Enter To Continue..." << endl;
cin.ignore();
cin.get();
return 0;
}
// Load text file function
void loadData(transaction allTransaction)
{
string line;
ifstream myfile("data.txt");
if (myfile.is_open())
{
while (getline(myfile, line))
{
istringstream f(line);
string s;
while (getline(f, s, '|')) {
myfile >> allTransaction.date >>
allTransaction.stock >>
allTransaction.sale >>
allTransaction.price >>
allTransaction.shares;
data.push_back(allTransaction);
}
myfile.close();
}
}
else std::cout << "Unable to open file";
}
// Buy stock function
void buyShares(transaction allTransaction)
{
allTransaction.sale = 0; // Set sale boolean to 0, indicating a buy.
storeData(allTransaction); // Call storeData function to store the data.
}
// Sell stock function
void sellShares(transaction allTransaction)
{
allTransaction.sale = 1; // Set sale boolean to 1, indicating a sell.
// Need function to check shares on hand.
storeData(allTransaction); // Call storeData function to store the data.
}
void storeData(transaction allTransaction)
{
ofstream myfile;
myfile.open("data.txt", ios::out | ios::app);
myfile << allTransaction.date << "|" << allTransaction.stock << "|" << allTransaction.sale << "|" <<
allTransaction.price << "|" << allTransaction.shares << '\n';
myfile.close();
data.push_back(allTransaction);
}
請考慮在你的問題是什麼更具體一些。你有什麼嘗試,什麼不工作。在我的頭頂上,當你的交易通過引用傳遞時,你的許多例程都會按照價值交易你的交易。否則,您正在修改事務的_copy_,而不是原始事務本身。 – fbrereto 2014-11-21 07:22:35