Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用逗号和空格分隔的C++读取器_C++_Arrays_Vector_Arraylist_Readfile - Fatal编程技术网

用逗号和空格分隔的C++读取器

用逗号和空格分隔的C++读取器,c++,arrays,vector,arraylist,readfile,C++,Arrays,Vector,Arraylist,Readfile,我需要在主测试类中读取包含此格式日期、ID、活动、数量、价格数据的文件。我应该将日期值读取并存储到日期类型的int day、month、year中,然后是ID、Activity、Qty、Price of stock class 02/08/2011, ABC, BUY, 100, 20.00 05/08/2011, ABC, BUY, 20, 24.00 06/08/2011, ABC, BUY, 200, 36.00 我将相应的值存储到Stock构造函数中,并通过push_将所有数据存储到

我需要在主测试类中读取包含此格式日期、ID、活动、数量、价格数据的文件。我应该将日期值读取并存储到日期类型的int day、month、year中,然后是ID、Activity、Qty、Price of stock class

02/08/2011, ABC, BUY, 100, 20.00 
05/08/2011, ABC, BUY, 20, 24.00
06/08/2011, ABC, BUY, 200, 36.00
我将相应的值存储到Stock构造函数中,并通过push_将所有数据存储到我自己的Vector类中。我必须添加/编辑什么,以便检索Vector和getDate、ID、Activity、Qty、Price中的数据行,并计算Qty和Price

#include"Vector.h"
#include"Date.h"
#include"Stock.h"
#include<iomanip>
#include<fstream>
#include<iostream>
#include<string>
using namespace std;

int main(){

    string stockcode;
    string act;
    int qty;
    double p;
    int d,m,y;
    char x;

    //declare file stream variables
    ifstream inFile;
    ofstream outFile;

    //open the file
    inFile.open("share-data.txt");

    //code for data manipulation
    cout << fixed << showpoint << setprecision(2);

    while(!inFile.eof()){
    inFile>> d >> x >> m >> x >> y >> x
    >> stockcode >> act >> qty >> x >> p;

    Stock s1(d,m,y,stockcode,act,qty,p);

    stockcode = stockcode.substr(0, stockcode.length()-1);
act = act.substr(0, act.length()-1);

Stock s1(d,m,y,stockcode,act,qty,p);
stockList.push_back(s1);

    }

    inFile.close();

    system("PAUSE");
    return 0;
}
使用字符串::substr

您可以对其他字符串变量执行相同的操作

#include<iostream>

using namespace std;

template <class T>
class Vector
{

public:

    Vector();               // default constructor
    virtual ~Vector() {};   //destructor
    bool isEmpty() const;
    bool isFull() const;
    void print() const;
    void push_back(T);
    T pop_back();
    T at(int i); 
    int Size();
    //Vector<T> operator +=(T);

private:
    int size;
    T list[100];
};

template <class T>
Vector<T>::Vector()
{
    size = 0;
}

template <class T>
Vector<T> Vector<T>::operator +=(T i)
{
    this->push_back(i);
    return *this;
}

template <class T>
bool Vector<T>::isEmpty() const
{
    return (size == 0);
}

template <class T>
bool Vector<T>::isFull() const
{
    return (size == 100);
}

template <class T>
void Vector<T>::push_back(T x)
{
    list[size] = x;
    size++;
}

template <class T>
T Vector<T>::operator[](T i)
{
    return list[i];
}

template <class T>
T Vector<T>::at(int i)
{
    if(i<size)
        return list[i];
    throw 10;
}

template <class T>
T Vector<T>::pop_back()
{
    T y;
    size--;
    y= list[size];
    return y;
}

template <class T>
void Vector<T>::print() const
{
    for (int i = 0; i < size; i++)
        cout << list[i] << " ";
    cout << endl;
}

template <class T>
int Vector<T>::Size() 
{
    return size;
}
 #include"Date.h"

   Stock::Stock()
{
    stockID="";
    act="";
    qty=0;
    price=0.0;
}

Stock::Stock(int d, int m , int y, string id,string a, int q, double p)
:date(d,m,y){
    stockID = id;
    act = a;
    qty = q;
    price = p;

}
.
.
.
act = act.substr(0, act.length()-1);