I need to read a file which looks like this:
model=binomial
steps=1
time horizon=1
S=101
u=1.2
d=0.95
r=0.03

number of instruments in portfolio=2

instrument=option
exercise=european
position=short
type=put
strike=98
expiry=1

instrument=option
exercise=european
position=long
type=put
strike=10
expiry=1

and i just need to read everything what is on the right site of '=' into a structure, because i will need to use this numbers and strings later.
all i have written is just a structure:
struct input
{
string model;
int steps;
int timehorizon;
int S;
int u;
int d;
int r;
int noiip;
std::vector<string> instrument;
std::vector<string> exercise;
std::vector<string> position;
std::vector<string> type;
std::vector<double> strike;
std::vector<double> expiry;
};

please, i really need help. :(

input in;
string temp;
double d = 0.0;

while(infile >> in.model)
{
     infile >> in.steps;
     infile >> in.timehorizon;
     infile >> in.S;
     infile >> in.u;
     infile >> in.d;
     infile >> in.r;

     infile >> temp;
     in.instrument.push_back(temp);

     infile >> temp;
     in.exercise.push_back(temp);

     infile >> temp;
     in.position.push_back(temp);

     infile >> temp;
     in.type.push_back(temp);

     infile >> d;
     in.strike.push_back(d);

     infile >> d;
     in.expiry.push_back(d);
}

hm think i will need 2 structures. but whatever. well, my problem is, that i want to read just the right part of a line, everything what is to the right of '=', for example i want to get string 'binomial' or 'european' or just a number like '1' or '0.03'.

you will need to read in the entire line into a string object, then parse the line into desired substrings:

int pos = 0;
string line;
string right_side;

getline(infile, line);

pos = line.find('=');

right_side = line.substr(pos+1);

i modified it a bit, i created new structure, because one part of input file is repeated. this is what i have so far:

struct input1
{
    string model;
    int steps;
    int timehorizon;
    int S;
    int u;
    int d;
    int r;
    int noiip;
};

struct input2
{
    string instrument;
    string exercise;
    string position;
    string type;
    double strike;
    double expiry;
};

struct output
{
    string timezero;
    double price;
    double delta;
    string expirytime;
    vector<double> stockprice;
    vector<double> payoff;
    vector<double> profit;
};

void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    int i;
    std::vector<input2> b;
    string temp;
    double d = 0.0;
    while(infile >> a.model)
{
     infile >> a.steps;
     infile >> a.timehorizon;
     infile >> a.S;
     infile >> a.u;
     infile >> a.d;
     infile >> a.r;
     infile >> a.noiip;
     for(int i=0; i<a.noiip; i++) // i know it's wrong but it's visible right know what i'm trying to reach... so i want the vector b to have 'a.noiip' (it's a number) elements... and every element is a structure which contains different types; how to fix it?
     {
         infile >> b.instrument;
         infile >> b.exercise;
         infile >> b.position;
         infile >> b.type;
         infile >> b.strike;
         infile >> b.expiry;
         b.pushback();

     }
}
    cout<<a.model; // just for try to check if it's workin'
    cout<<a.steps;
    cout<<a.timehorizon;


}

i am sorry to mess up the wall, i corrected a bit my code, but it isn't working :(

void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    int i, j;
    std::vector<input2> b;
    input2 c;
    string temp;
    double d = 0.0;
    while(infile >> a.model)
{
     infile >> a.steps;
     infile >> a.timehorizon;
     infile >> a.S;
     infile >> a.u;
     infile >> a.d;
     infile >> a.r;
     infile >> a.noiip;
     j=a.noiip;
     for(int i=0; i<a.noiip; i++)
     {
         infile >> c.instrument;
         infile >> c.exercise;
         infile >> c.position;
         infile >> c.type;
         infile >> c.strike;
         infile >> c.expiry;
         b.push_back(c);

     }
     infile.ignore();
}
    cout<<a.model<<"\n";
    cout<<a.steps;
    cout<<a.timehorizon;
    cout<<a.S;
    cout<<b[0].instrument;
    cout<<b[1].instrument;


}

i would edit my previous post, but i the time is over. so anyway, now my problem is, that i can't fine a mistake in my reading function. i does not work properly. input file is still the same and my code for reading function looks like this:

void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    int i, j;
    std::vector<input2> b(2);
    input2 c;
    string temp;
    char byleco;
    double d = 0.0;
    while(infile)
{
     infile >> a.model;
     infile >> a.steps;
     infile >> a.timehorizon;
     infile >> a.S;
     infile >> a.u;
     infile >> a.d;
     infile >> a.r;
     infile >> a.noiip;
     infile.ignore();
     for(int i=0; i<2; i++)
     {
         infile >> c.instrument;
         infile >> c.exercise;
         infile >> c.position;
         infile >> c.type;
         infile >> c.strike;
         infile >> c.expiry;
         b.push_back(c);
         infile.ignore();

     }



}
    //cout<<a.model;
    //cout<<a.steps;
    //cout<<a.noiip;
    cout<<a.model;
    cout<<b[0].instrument;


}

this things that I 'cout' don't match my input file, which you can see above.
i need help so badly. please....

void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    int i, j;
    std::vector<input2> b(2);
    input2 c;
    string temp;
    char byleco;
    double d = 0.0;
    while(infile)
    {
        infile >> a.model;
        infile >> a.steps;
        infile >> a.timehorizon;
        infile >> a.S;
        infile >> a.u;
        infile >> a.d;
        infile >> a.r;
        infile >> a.noiip;
        infile.ignore();
        for(int i=0; i<2; i++)
        {
            infile >> c.instrument;
            infile >> c.exercise;
            infile >> c.position;
            infile >> c.type;
            infile >> c.strike;
            infile >> c.expiry;
            b.push_back(c);
            infile.ignore();
        }
    }

    //cout<<a.model;
    //cout<<a.steps;
    //cout<<a.noiip;
    cout<<a.model;
    cout<<b[0].instrument;
}

What precisely is the problem? The couts don't match. What exactly doesn't match?

for example a.model should stand for 'model=binomial', and a.steps should stand for 'steps=1' and a.noiip should stand for 'number of instruments in portfolio=2' and so on, and later b[0].instrument should stand for 'instrument=option' or b[1].strike should stand for strike=10.... and so on, and so on :)

>> for example a.model should stand for 'model=binomial', and a.steps should stand for 'steps=1' and a.noiip should stand for 'number of instruments in portfolio=2' and so on, and later b[0].instrument should stand for 'instrument=option' or b[1].strike should stand for strike=10.... and so on, and so on :)


What happened to reading in a line, searching for the equals sign, throwing away everything before that, and storing what's after?

You can't read "steps=1" directly into a.steps with the >> operator. a.steps is an integer. Everything gets messed up when you try to read a string into an integer. You have to read the entire line into a string, find the equals sign, extract what's after it, then convert the string to an integer, and store THAT in a.steps.

Lines like this won't work:

infile >> a.steps;
struct input1
{
    string model;
    int steps;
    int timehorizon;
    int S;
    int u;
    int d;
    int r;
    int noiip;
};

struct input2
{
    string instrument;
    string exercise;
    string position;
    string type;
    double strike;
    double expiry;
};

struct output
{
    string timezero;
    double price;
    double delta;
    string expirytime;
    vector<double> stockprice;
    vector<double> payoff;
    vector<double> profit;
};

oh i forgot to add that i have already corrected it into strings... i'm sorry. but it still doesn't work. so my previous post is still actual.

>>oh i forgot to add that i have already corrected it into strings... i'm sorry. but it still doesn't work. so my previous post is still actual.

We can only comment on what is posted. People need the following in order to help.

  1. The current code.
  2. The text file, or a relevant snippet (you've provided that).
  3. The expected output.
  4. The actual output.
  5. Some explanation of how the two differ or any other problems.

i am sorry. the input file looks like that:

model=binomial
steps=1
time horizon=1
S=101
u=1.2
d=0.95
r=0.03

number of instruments in portfolio=2

instrument=option
exercise=european
position=short
type=put
strike=98
expiry=1

instrument=option
exercise=european
position=long
type=put
strike=10
expiry=1

and i have to read it into the structure. actually i just need everything what is to the right of '='. and this is my code so far:

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cassert>
using namespace std;

struct input1
{
    string model;
    string steps;
    string timehorizon;
    string S;
    string u;
    string d;
    string r;
    string noiip;
};

struct input2
{
    string instrument;
    string exercise;
    string position;
    string type;
    string strike;
    string expiry;
};


void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    int i, j;
    std::vector<input2> b(2);
    input2 c;
    double d = 0.0;
    while(infile)
{
     infile >> a.model;
     infile >> a.steps;
     infile >> a.timehorizon;
     infile >> a.S;
     infile >> a.u;
     infile >> a.d;
     infile >> a.r;
     infile >> a.noiip;
     infile.ignore();
     for(int i=0; i<2; i++)
     {
         infile >> c.instrument;
         infile >> c.exercise;
         infile >> c.position;
         infile >> c.type;
         infile >> c.strike;
         infile >> c.expiry;
         b.push_back(c);
         infile.ignore();

     }
}
    //cout<<a.model;
    //cout<<a.steps;
    //cout<<a.noiip;
    cout<<b[0].instrument;


}

int main()
{
wczytaj();
return 0;
}

and my problem is that for example a.model should stand for model=binomial, b[1].expiry should stand for expiry=1, but they do not. i'm desperate. please.

model=binomial
steps=1
time horizon=1
S=101
u=1.2
d=0.95
r=0.03

number of instruments in portfolio=2

The >> operator won't work. You have an input file with spaces. You need to grab the entire line with getline. Change all the lines from using the >> operator to getline. See example below.

getline(cin, a.timehorizon);

Ok, so this is what i wrote, but something is wrong with the FOR loop:

struct input1
{
    string model;
    string steps;
    string timehorizon;
    string S;
    string u;
    string d;
    string r;
    string noiip;
};

struct input2
{
    string instrument;
    string exercise;
    string position;
    string type;
    string strike;
    string expiry;
};


void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    int i, j;
    std::vector<input2> b(2);
    input2 c;
    double d = 0.0;
    while(infile)
{
     getline(cin, a.model);
     getline(cin, a.steps);
     getline(cin, a.timehorizon);
     getline(cin, a.S);
     getline(cin, a.u);
     getline(cin, a.d);
     getline(cin, a.r);
     getline(cin, a.noiip);
     infile.ignore();
     for(int i=0; i<a.noiip; i++) // i need this loop to be repeated 'a.noiip' (that's a number) times
     {
         getline(cin, c.instrument);
         getline(cin, c.exercise);
         getline(cin, c.position);
         getline(cin, c.type);
         getline(cin, c.strike);
         getline(cin, c.expiry);
         b.push_back(c);
         infile.ignore();

     }
}
    cout<<a.model;
    //cout<<a.steps;
    //cout<<a.noiip;
    //cout<<b[0].instrument;


}


int main( int argc, char ** argv )
{
   wczytaj();
   return 0;
}

Again, code tags. Please use them.

for(int i=0; i<a.noiip; i++)

a.nolip is a string, correct? What does it mean for an integer to be less than a string? You can't compare the two. You're back to where you were earlier. I don't know why you re-defined everything in your struct as a string, but you should redefine them back to the way they were.

Go way back in the thread. Read in the entire line as a string. Find the equals sign. Throw away everything after the equals sign. Then convert what's left from a string to an integer. I suggest writing a few functions to help:

string ThrowAwayFirstPart(string line);
int stringToInt(string aString);

The second one is easy since the functions atoi and strtol already exist. you already have a string variable called temp set up (or at least you did at some time). Use it.

You end up with this:

getline(cin,temp);
a.noiip = stringToInt(ThrowAwayFirstPart(temp));

i corrected my code again, but it isn't working properly yet.... this is my code so far

#include<iostream>
#include<vector>
#include<string>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cassert>
using namespace std;

struct input1
{
    string model;
    string steps;
    int timehorizon;
    int S;
    double u;
    double d;
    double r;
    double noiip;
};

struct input2
{
    string instrument;
    string exercise;
    string position;
    string type;
    double strike;
    double expiry;
};

string ThrowAwayFirstPart(string line)
{
    string result;
    string::iterator it;
    int k;
    size_t found;
    found=line.find('=');
    k=int(found)+1;
    result=line.erase(0,k);
    return result;
}


int stringToInt(string aString)
{
    int i;
    istringstream iss(aString);
    iss>>i;
    return i;
}

double stringToDouble(string bString)
{
    double x;
    istringstream iss(bString);
    iss>>x;
    return x;
}

void wczytaj(void)
{
    std::ifstream infile("input.txt");
    input1 a;
    input2 c;
    int i, j;
    std::vector<input2> b;
    double d = 0.0;
    while(!infile.eof())
{
     string temp, temp1;
     getline(infile, temp1);
     a.model=ThrowAwayFirstPart(temp1);
     getline(infile, a.steps);
     a.steps=ThrowAwayFirstPart(temp);
     getline(infile, temp);
     a.timehorizon=stringToInt(ThrowAwayFirstPart(temp));
     getline(infile, temp);
     a.S=stringToInt(ThrowAwayFirstPart(temp));
     getline(infile, temp);
     a.u=stringToDouble(ThrowAwayFirstPart(temp));
     getline(infile, temp);
     a.d=stringToDouble(ThrowAwayFirstPart(temp));
     getline(infile, temp);
     a.r=stringToDouble(ThrowAwayFirstPart(temp));
     getline(infile, temp);
     a.noiip=stringToDouble(ThrowAwayFirstPart(temp));
     infile.ignore();
     if(infile)
     {

     for(int i=0; i<a.noiip; i++)
     {
         getline(infile, temp);
         c.instrument=ThrowAwayFirstPart(temp);
         getline(infile, temp);
         c.exercise=ThrowAwayFirstPart(temp);
         getline(infile, temp);
         c.position=ThrowAwayFirstPart(temp);
         getline(infile, temp);
         c.type=ThrowAwayFirstPart(temp);
         getline(infile, temp);
         c.strike=stringToDouble(ThrowAwayFirstPart(temp));
         getline(infile, temp);
         c.expiry=stringToDouble(ThrowAwayFirstPart(temp));
         infile.ignore();

     }
     b.push_back(c);
     }
}
    cout<<a.model<<"\n";
    cout<<a.steps<<"\n";
    cout<<a.timehorizon<<"\n";
    cout<<a.noiip<<"\n";
    cout<<b[0].instrument<<"\n";


}

// program main function
int main()
{
    //string b;
    //int x;
    //double c;
    //string line("time horizon=1");
    //string line2("d=0.95");
    //c=stringToDouble(ThrowAwayFirstPart(line2));
    //b=ThrowAwayFirstPart(line);
    //x=stringToInt(b);
    //cout<<"hahaa "<<b<<" "<<x<<"\n";
    //cout<<c<<"\n"; // this is just to check if functions to help are working and they are.
   wczytaj();
   return 0;
}

it still does not match to input text file... a.model should be 'binomial', and so on, but it is not :( and this is my input.txt once again.. it should match to structures after reading from the file :(

model=binomial
steps=1
time horizon=1
S=101
u=1.2
d=0.95
r=0.03

number of instruments in portfolio=2

instrument=option
exercise=european
position=short
type=put
strike=98
expiry=1

instrument=option
exercise=european
position=long
type=put
strike=10
expiry=1

If it's failing, you need to find out exactly when it first fails. A few possibilities:

  • Fails at the very first record.
  • Fails at the very last record.
  • Fails after reading the first integer.
  • Fails after reading the first double.
  • Fails after reading the first string.
  • Fails after reading the second integer.
  • Fails after reading the second double.
  • Fails after reading the second string.
  • Fails somewhere else.

I see by some commented out code that you have tested your helper functions and that they are parsing the string correctly.

On the other hand, garbage in, garbage out. If the functions aren't given the proper parameter, they can be written perfectly, but they'll give bad results.

So...

For debugging purposes, inside the functions, display the input coming in. Make sure it's correct. Then display the return value. Make sure IT'S correct. Perhaps your code isn't matching up exactly with the file. Perhaps you are trying to read something in twice, read a double into an integer, skipping a line, processing a line twice, eetc. etc. You need to carefully determine the exact point where things go wrong.

Yes, I already made sure it is NOT correct and I do NOT know how to fix it, that's why I have asked here for help.

Ok I found all my mistakes and everything works properly!!! Thank You for your help and patience and I am sorry if my comments sounded rude, but I am really tired of programming... Thank You Mr Vernon and Mr Clinton, you're amazing and I wish you happy New Year 2011! :-) I'm so happy! Thank You!

>> Yes, I already made sure it is NOT correct and I do NOT know how to fix it, that's why I have asked here for help.

It sounds like you interpreted my post as not believing you that the code didn't work? That wasn't the intent. Obviously if it worked, you wouldn't post. I assumed you didn't know how to fix it or else you wouldn't be asking for help. The whole point is that when something breaks, you need to give DETAILED information of exactly where it doesn't work and how. Saying "it doesn't work" doesn't pinpoint the problem. That's what I was trying to get across, plus ways to help you pinpoint the problem and advice on what to focus on. I could have simply run the program myself and found the errors, but the whole idea is to give guidance not just on the precise solution, but general methodology too.


>> I am sorry if my comments sounded rude

It's cool. Apology accepted and I'm glad you were able to get it to work.

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.