This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Hey everyone, I wrote a small program that loads a file with some decimal numbers in it like 2.333,433.222,343 etc... Then stores the file in a vector and prints it. It works when I try just one file but when I try to load to it crashs. Please help.

#include <vector>

#include <string>

#include <fstream>

#include <sstream> // for stringstream

#include <iterator> // for ostream_iterator

#include <algorithm> // for for_each

#include <iostream> // for cout

#include <limits> // for int max

using namespace std;

typedef vector<float> v; // the numbers from the current line

vector<float> v2;

typedef vector< v > theVectors; // vectors from all lines

typedef vector<int> numofthings;

theVectors::iterator myIter;

numofthings num(10000);

v test(10000);

theVectors hold(10000);

int i=0;

int i2=-1;

float x;

struct matrix { float x1;float y1;float z1;float w1;

float x2;float y2;float z2;float w2;

float x3;float y3;float z3;float w3;

float x4;float y4;float z4;float w4;}mat;

matrix nulls={1,0,0,0,

0,1,0,0,

0,0,1,0,

0,0,0,1};

void openraw(char raw[])

{

fstream in(raw);

string line;

i2++;

const int l = numeric_limits<int>::max();

while (getline(in, line)) {

for (stringstream ss(line); ss >> x; ss.ignore(l, ','))

{

test[0]=x;

hold[i]=test;

num[i2]=i;

i++;

}

num[i2]+=1;

}

}

int numofploc(int loc)

{

int ret=0;

for(int q=-1;q++;q<loc)

{

ret+=num[q];

}

return ret;

}

void renderfile(int loc)

{

int start=numofploc(loc);

cout<<start;

start-=num[loc];

int f0=0;

int f1=0;

int f2=0;

float holdv1;

float holdv2;

float holdv3;

float holdv4;

for(myIter = hold.begin()+start; myIter != hold.begin()+start+18; myIter++)

{

float x,y,z;

z=0;

float abc;

v2= *myIter;

abc=v2[0];

f0++;

if(f0==1)

{

x=abc;

}

if(f0==2)

{

y=abc;

}

if(f0==3)

{

z=abc;

f0=0;

}

f1++;

if(f1==3)

{

f1=0;

f2++;

if(f2==1)

{

holdv1=nulls.x1;

holdv2=nulls.y1;

holdv3=nulls.z1;

holdv4=nulls.w1;

x=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

holdv1=nulls.x2;

holdv2=nulls.y2;

holdv3=nulls.z2;

holdv4=nulls.w2;

y=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

holdv1=nulls.x3;

holdv2=nulls.y3;

holdv3=nulls.z3;

holdv4=nulls.w3;

z=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

cout<<x<<" "<<y<<" "<<z<<" "<<endl;

}

if(f2==2)

{

holdv1=nulls.x1;

holdv2=nulls.y1;

holdv3=nulls.z1;

holdv4=nulls.w1;

x=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

holdv1=nulls.x2;

holdv2=nulls.y2;

holdv3=nulls.z2;

holdv4=nulls.w2;

y=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

holdv1=nulls.x3;

holdv2=nulls.y3;

holdv3=nulls.z3;

holdv4=nulls.w3;

z=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

cout<<x<<" "<<y<<" "<<z<<" "<<endl;

}

if(f2==3)

{

holdv1=nulls.x1;

holdv2=nulls.y1;

holdv3=nulls.z1;

holdv4=nulls.w1;

x=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

holdv1=nulls.x2;

holdv2=nulls.y2;

holdv3=nulls.z2;

holdv4=nulls.w2;

y=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

holdv1=nulls.x3;

holdv2=nulls.y3;

holdv3=nulls.z3;

holdv4=nulls.w3;

z=(x*holdv1)+(y*holdv2)+(z*holdv3)+(holdv4);

cout<<x<<" "<<y<<" "<<z<<" "<<endl;

f2=0;

}

}

}

myIter=NULL;

}

void main()

{

openraw("test.RAW");

openraw("test2.RAW");

int pass=numofploc(0);

renderfile(0);

renderfile(1);

int a;

cin>>a;

}

Here is the code. For completness sake I am also including the files I am loading:

50,0,0

0,0,0

250,50,0

50,0,0

0,0,0

25,50,0

file2:

250,20,0

0,30,0

2530,250,0

50,0,0

0,0,0

25,50.3,0

#include <vector>

#include <string>

#include <fstream>

#include <sstream> // for stringstream

#include <iterator> // for ostream_iterator

#include <algorithm> // for for_each

#include <iostream> // for cout

#include <limits> // for int max

using namespace std;

typedef vector<float> v; // the numbers from the current line

vector<float> v2;

typedef vector< v > theVectors; // vectors from all lines

typedef vector<int> numofthings;

theVectors::iterator myIter;

numofthings num(10000);

v test(10000);

theVectors hold(10000);

int i=0;

int i2=-1;

float x;

struct matrix { float x1;float y1;float z1;float w1;

float x2;float y2;float z2;float w2;

float x3;float y3;float z3;float w3;

float x4;float y4;float z4;float w4;}mat;

matrix nulls={1,0,0,0,

0,1,0,0,

0,0,1,0,

0,0,0,1};

void openraw(char raw[])

{

fstream in(raw);

string line;

i2++;

const int l = numeric_limits<int>::max()

while (getline(in, line)) {

for (stringstream ss(line); ss >> x; ss.ignore(l, ','))

{

test[0]=x;

hold[i]=test;

num[i2]=i;

i++;

}

num[i2]+=1;

}

}

int numofploc(int loc)

{

int ret=0;

for(int q=-1;q++;q<loc)

{

ret+=num[q];

}

return ret;

}

void renderfile(int loc)

{

int start=numofploc(loc);

cout<<start;

start-=num[loc];

int f0=0;

int f1=0;

int f2=0;

float holdv1;

float holdv2;

float holdv3;

float holdv4;

for(myIter = hold.begin()+start; myIter != hold.begin()+start+18; myIter++)

{

float x,y,z;

z=0;

float abc;

v2= *myIter;

abc=v2[0];

f0++;

if(f0==1)

{

x=abc;

}

if(f0==2)

{

y=abc;

}

if(f0==3)

{

z=abc;

f0=0;

}

f1++;

if(f1==3)

{

f1=0;

f2++;

if(f2==1)

{

holdv1=nulls.x1;

holdv2=nulls.y1;

holdv3=nulls.z1;

holdv4=nulls.w1;

x=(x*holdv1)+(y*holdv2)+(z

holdv1=nulls.x2;

holdv2=nulls.y2;

holdv3=nulls.z2;

holdv4=nulls.w2;

y=(x*holdv1)+(y*holdv2)+(z

holdv1=nulls.x3;

holdv2=nulls.y3;

holdv3=nulls.z3;

holdv4=nulls.w3;

z=(x*holdv1)+(y*holdv2)+(z

cout<<x<<" "<<y<<" "<<z<<" "<<endl;

}

if(f2==2)

{

holdv1=nulls.x1;

holdv2=nulls.y1;

holdv3=nulls.z1;

holdv4=nulls.w1;

x=(x*holdv1)+(y*holdv2)+(z

holdv1=nulls.x2;

holdv2=nulls.y2;

holdv3=nulls.z2;

holdv4=nulls.w2;

y=(x*holdv1)+(y*holdv2)+(z

holdv1=nulls.x3;

holdv2=nulls.y3;

holdv3=nulls.z3;

holdv4=nulls.w3;

z=(x*holdv1)+(y*holdv2)+(z

cout<<x<<" "<<y<<" "<<z<<" "<<endl;

}

if(f2==3)

{

holdv1=nulls.x1;

holdv2=nulls.y1;

holdv3=nulls.z1;

holdv4=nulls.w1;

x=(x*holdv1)+(y*holdv2)+(z

holdv1=nulls.x2;

holdv2=nulls.y2;

holdv3=nulls.z2;

holdv4=nulls.w2;

y=(x*holdv1)+(y*holdv2)+(z

holdv1=nulls.x3;

holdv2=nulls.y3;

holdv3=nulls.z3;

holdv4=nulls.w3;

z=(x*holdv1)+(y*holdv2)+(z

cout<<x<<" "<<y<<" "<<z<<" "<<endl;

f2=0;

}

}

}

myIter=NULL;

}

void main()

{

openraw("test.RAW");

openraw("test2.RAW");

int pass=numofploc(0);

renderfile(0);

renderfile(1);

int a;

cin>>a;

}

Here is the code. For completness sake I am also including the files I am loading:

50,0,0

0,0,0

250,50,0

50,0,0

0,0,0

25,50,0

file2:

250,20,0

0,30,0

2530,250,0

50,0,0

0,0,0

25,50.3,0

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

>> when I try to load to it crashs

Huh? Does it work or does it crash?

Where is it crashing?

Your openraw() function is using global variables that it does not initialize, like i2 and num[]. These variables are going to have different values the 2nd time openraw is run. Tha may be a problem. Like if the two files are so large that together they overrun num[] you will get a crash

int start=numofploc(loc);

cout<<start;

start-=num[loc];

then use start in the for() loop. If start is set wrong that could cayuse a problem. We can't tell that because we don't know anything about the files you are reading.

I believe you problem is centered around these line:

int start=numofploc(loc);

start-=num[loc];

and this function:

int numofploc(int loc)

{

int ret=0;

for(int q=-1;q++;q<loc)

{

ret+=num[q];

}

return ret;

}

Check out the for loop. No matter what value of loc is passed in you always return num[0] ( == 19 ).

On pass one of the lines from renderfile() you end up with

start == 0 ( 19 - 19 )

Which is perfect.

After pass two you end up with

start == -17 ( 19 - 38 )

Which will eventually bomb when you access the wrong thing.

If what you want to do is start at the first index for the second file in the hold vector you want to flip the subtraction:

int start=numofploc(loc);

start = num[loc] - start;

This will make it work with your current logic.

Hope that helps,

-stephen

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
C++

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.