Hi All,

Well I've been stuck for a while here. I'm trying tp write a function called TransfertoSwingID which will transfer a line of data held in a struct contained in a vector to a new vector of structs.

Basically as I'm iterating through the vector of structs called "prices", if the conditions are true, then I need to transfer/copy the data held in the struct that is being pointed to by *(it-1).

There are 2 sets of conditions, if the 1st set is true, then I pass an integer, 0, to the function so that the data transferred would be identified as a "SL". If the 2nd set of conditions is true, then I pass int 1 so that the data transferred would be identified as "SH". The idea is then to print out the data which would look like this:

Date Time Open High Low Close Volume Identifier

(where the last string is the identifier, either SH or SL)

The original vector "prices" contains the first 7 variables above in the structs it contains.

Any help would be greatly appreciated. Thanks.

1.    int main(int argc, char* argv[]) 
2.    { 
3.
4.    if (argc != 2) 
5.    { 
6.
7.    std::cerr << 
8.
9.    "Usage: " << argv[0] << " filename " << std::endl; 
10.
11.    return EXIT_FAILURE; 
12.    } 
13.
14.
15.    PriceList prices; 
16.
17.    ReadPricesFromFile(argv[1], prices); 
18.    if (prices.size() < 2)
19.        std::cout << "Vector size less than 2!" << std::endl;
20.    else
21.        std::cout << "Vector has 2 or more elements" << std::endl;
22.
23.
24.
25.    std::vector<PriceInfo>::iterator it; 
26.    it = prices.begin();
27.    advance (it, 2);
28.
29.
30.
31.    for (;it!= prices.end();) 
32.    { 
33.
34.        if // 1st condition
35.
36.         { 
37.
38.            if // another condition
39.
40.             TransferToSwingID(0, *(it-1)); //( *(it-1) is likely wrong syntax)
41.
42.
43.            }
44.
45.
46.        {
47.            if // 1st condition for 2nd possibility
48.
49.
50.            {
51.                if // 2nd condition
52.                TransferToSwingID(1, *(it-1)); 
                    // (how does one pass the contents pointed to by iterator it?)
53.
54.            }
55.        }
56.
57.
58.    ++it;
59.    } 
60.
61.    int TransferToSwingID (int type, struct PriceInfo&)
62.
63.    {
64.        struct SwingIdentified
65.        {
66.        double Id_Open; 
67.
68.        double Id_High; 
69.
70.        double Id_Low; 
71.
72.        double Id_Close; 
73.
74.        unsigned int Id_Volume; 
75.
76.        unsigned int Id_Time; 
77.        std::string Id_Date; 
78.
79.        std::string Identifier; 
80.        };
81.
82.
83.        // some code here to transfer the contents of *(it-1) 
84.        // to a new vector of struct of type SwingIdentified
85.
86.        if (int type=0)
87.
88.                std::string Identifier = "SL";
89.
90.        else if (int type=1)
91.
92.                std::string Identifier = "SH";
93.
94.
95.
96. 
97.
98.    }
99.
100.
101.
102.    char quitKey;
103.
104.    std::cout << "Press q to quit" << std::endl;
105.
106.    std::cin >> quitKey;
107.
108.    return EXIT_SUCCESS; 
109.    } 

Sorry you couldn't understand my previous code. Perhaps seeing it translated to your actual structures will make it easier for you to understand. Basically I made SwingIdentified inherit from PriceInfo. This way all the common properties are easy to transfer over. I set up an equals operator so that in your loop a simple SwingIdentified = PriceInfo will transfer the data. I set up a simple setter for Identifier according to the parameters you set down.

So what my code does is when the conditions are right declares a new SwingIdentified object, transfers the data from the relevant PriceInfo object, sets the Identifier and add this new object to a vector of type SwingIdentified

Hope this helps

#include<iostream>
#include<vector>
using namespace std;


struct PriceInfo
{
    double Id_Open;
    double Id_High;
    double Id_Low;
    double Id_Close;
    unsigned int Id_Volume;
    unsigned int Id_Time;
    std::string Id_Date;
};
struct SwingIdentified : PriceInfo
{
    std::string Identifier;
    SwingIdentified SwingIdentified::operator= (PriceInfo &pi)
    {
        Id_Open = pi.Id_Open;
        Id_High = pi.Id_High;
        Id_Low = pi.Id_Low;
        Id_Close = pi.Id_Close;
        Id_Volume = pi.Id_Volume;
        Id_Time = pi.Id_Time;
        Id_Date = pi.Id_Date;
        Identifier = "";
        return *this;
    }
    void SetIdentifier(int type)
    {
        if (type=0)
            Identifier = "SL";
        else if (type=1)
            Identifier = "SH";
    }
};

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        std::cerr <<
            "Usage: " << argv[0] << " filename " << std::endl;
        return EXIT_FAILURE;
    }
    vector<PriceInfo> prices;
    ReadPricesFromFile(argv[1], prices);

    if (prices.size() < 2)
        std::cout << "Vector size less than 2!" << std::endl;
    else
        std::cout << "Vector has 2 or more elements" << std::endl;
    vector<SwingIdentified> si;

    for (int it = 0;it< prices.size();it++)
    {
        if(true) // 1st condition
        {
            if(true) // another condition
            {
                //Declare a temp object of type SwingIdentified
                SwingIdentified temp;
                //Transfer the data from the relevant PriceInfo object in prices
                temp = prices[it];
                //Set the Identifier property in the temp object
                temp.SetIdentifier(0);
                //Add the temp object to the SwingIdentified vector
                si.push_back(temp);
            }
        }
        {
            if (true)// 1st condition for 2nd possibility
            {
                if(true) // 2nd condition
                {
                    SwingIdentified temp;
                    temp = prices[it];
                    temp.SetIdentifier(1);
                    si.push_back(temp);
                }
            }
        }
    }

    char quitKey;
    std::cout << "Press q to quit" << std::endl;
    std::cin >> quitKey;
    return EXIT_SUCCESS;
} 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.