1

You're missing a break in the if statement on line 57. Compare what you have with the example given previously. Without the break the while loop will continue to execute until it reaches the end of the file -- not what you want!

Also, you still require some code to move from the line where the target scan is found (that is, the line with '#S' on it) to the start of the data. This should go between the end of the part where you look for the correct scan (line 64) and the start of the bit where you start to read the data (line 67). This is also previously mentioned in this thread (see here).

0

ok, ok, is the break! hehe

it always have 12 lines between the #S and tha datas, so i do this:?

for(unsigned i = 0; i < 12; ++i) getline(inputFile, lineFromFile);

but i don't understand the getline?

0

other way, is the use of if condition, once every line i don't want to cath starts with "#", so i think in put this:

if(lineFromFile.substr(0,1) != "#")

and than make the stringstream, but don't work

2

then make the stringstream, but don't work

What doesn't work? How?

Remember:

  1. if it's a compiling problem: post the errors and the part of the code that the errors refer to
  2. if it's a run-time problem, print out the values of things until you can find what is going wrong.
  3. Then post if you can't figure it out.
Votes + Comments
Rep for helping on this monster thread
0

no no, it's work, i just put it into wrong place!

thank's man, the program is doing what i wan't, a question, now, each line is splited an each value is in a position of the big vector, wrigth? how i print this any of that position?

I tried:

std::cout << data[1][1] << "" << std::endl;

but give a os error...

0

but give a os error...

what is the error exactly? And when does it happen?

If it is something like "segmentation fault", then check the size of the data matrix etc. and then you might find the problem. This type of error is common when you try to access elements of a vector/array that don't exist. You can find the size of a std::vector using the std::vector::size() method:

std::cout << "number of rows    = " << data.size() << std::endl;
std::cout << "number of columsn = " << data.at(0).size() << std::endl;

You can read more about std::vector here

0

The program run, they print the values of the first column of the scan,because this code:

while(i < 14 && inStream.good() ) /* Try and split the line into 14 chunks */
                     {                                  
                     inStream >> dataLine[i];  /* Store the value of each chunk in the temp. vector */                                  
                     std::cout << dataLine[i] << "" << endl;
                     ++i;

but after this print, they windows show "the program find a problem and must be closed", i tried this code:

cout << "Myvector contains:";
  for (unsigned i=0; i<data.max_size(); i++)
  {
    for (unsigned j=0; j<data.max_size(); j++)
    
    cout << " " << data[i][j];
    cout << endl;
 }

and i try to print the vector size, with:

cout << "" << data.size();

they return 0

Edited by jshoot: n/a

1

Since when you do std::cout << data.size(); the result is 0 . You need to put some statements in to find out why you're data is not being stored. My guess would be that each line is not being pushed into data for some reason. Have a look at this bit:

if(i == 13){ 
    data.push_back(dataLine);// Add this line of data to the rest if there is
}

Check if the values of i are what you expect them to be. Hint: the row will only get added to the data vector if i == 13 at the moment. What is the value of i just before this check is made? Is it what you'd expect?

After you fix that, you will have another problem because in your loops to print the data you have used std::vector::max_size() instead of std::vector::size() . The method max_size() returns the biggest size that data could be on your system, not the size of the vector, use size() for this. Also, you have (tried) to use data.size() for both the inner and outer loop. This will not work, you have to remember that data is a vector in which each element is one row of your data. So, to find the number of rows, you can indeed use data.size() . However, to find the size of, say, row i you have to use data.at(i).size() .

0

ok ok, i change the condition and work, i put this code to print all the values:

int k=0;
while(k < data.size())
{
  for (unsigned i=0; i<data.size(); i++)
    {
    for (unsigned j=0; j<=data.at(0).size() - 1; j++)
        {
        std::cout << "A [" << i << "]  [" << j << "]= " << data[i][j] << std::endl;
        k++;
        }
    }
  break;
}

but now the last value of each line is the value of the last value of the line before, the program is "adding" +1 to thelast column, like the lastcolunm is in a posterior line, you understannd?

thanks

1

but now the last value of each line is the value of the last value of the line before, the program is "adding" +1 to thelast column, like the lastcolunm is in a posterior line, you understand?

I don't really understand what you mean, but it sounds like it's almost working like you expect. I would make a small sample set of data that is very simple, like:

1  2  3  4
 5  6  7  8
 9 10 11 12
13 14 15 26

Save it to a text file. Then make a small program (completely from the beginning) to read in and print out this data. Once you can get code to work on this, then it should also work on your full data set. A simple program to read this data and print it out might look like

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <fstream>

int main(){
    /* set the name of the test file and open it for reading */
    std::string filename("testData.txt");
    std::ifstream inFile(filename.c_str(), std::ios::in);

    /* Make some places to store the data */
    std::vector< std::vector< double > > data;
    std::vector< double > rowData(4);
    std::string lineFromFile;

    /* Go through the file */
    while(inFile.fail() != true){
        /* Get a line and check there's some data on it */
        getline(inFile, lineFromFile);
        if(lineFromFile.size() == 0)    break;

        /* Break the line into pieces */
        std::stringstream sstr(lineFromFile);
        unsigned i = 0;
        while(i < 4 && sstr.good()){
            sstr >> rowData[i];
            ++i;
        }

        /* Check that the line has the correct number of pieces */
        if(i == 4) data.push_back(rowData);
        else break;
    }
    /* Finished reading the data now */
    
    /* Print the data to the console */
    for(unsigned i = 0; i < data.size(); ++i){
        for(unsigned j = 0; j < data[i].size(); ++j)
            std::cout << data[i][j] << '\t';
        std::cout << std::endl;
    }

    /* Done */
    return 0;
}
0

yes, the program is almost done what I want, there are some implemetations that I have to do yet.

My problem is:

1. The reading is normal
2. The last column of the file , just the last line, is wrong.
3. The wrong is in the order of the column,for example, the value "23" was to be in the line 1, but is in line 2,sucessivily.

Another question:

How i find the max value in a column?

1

yes, the program is almost done what I want, there are some implemetations that I have to do yet.

My problem is:

1. The reading is normal
2. The last column of the file , just the last line, is wrong.
3. The wrong is in the order of the column,for example, the value "23" was to be in the line 1, but is in line 2,sucessivily.

Another question:

It's hard to know what the problem is specifically, without access to your code and data (I don't want access to it either, I'm just saying that you are the best person to find the answer to this problem from here). I think you will have to investigate further to find a solution, but it will be possible, if you just keep looking at the values that you are trying to add to data at different points.

How i find the max value in a column?

There's no special trick to it, you just have to look at each of the elements and find the biggest one. I think you can figure this out with a little thought and some internet searches! There are some posts on Daniweb about this already, you can search for those.

0

ok ravenous, i think and discorver a way to correct that error... and find to a way to get the max value, but you know a way to know the position of the max value?

like "maxvalue = 20000, 20000 is in postion a[40]".

I'm thinking a way to do this, but you have some tip?

And again, thanks a lot for your help, i move from a 50 line program to 300 lines and growning!

0

// what is problem in that array it cant show min number
# include<constream.h>
struct zeeshan
{
int a,b,c;
char e;};
int my (int [3][3]);// function decleratio;
//.....................................
void main()
{ zeeshan zee;
cout<<"enter the rows and columns";
int array[3][3];
my(array);
getche();
}
////////////////////////////////////////////
int my (int array[3][3])
{ zeeshan zee; int min;

for(zee.a=0;zee.a<3;zee.a++)
{
for(zee.b=0;zee.b<3;zee.b++)

cin>>array[zee.a][zee.b];
min=array[0][0]; }


for(zee.a=0;zee.a<3;zee.a++)
{
for(zee.b=0;zee.b<3;zee.b++)

{

cout<<array[zee.a][zee.b];

}
cout<<endl;
}
for(zee.a=0;zee.a<3;zee.a++)
{
for(zee.b=0;zee.b<3;zee.b++)
{
min=array[zee.a][zee.b];
if(array[0][0]<min)
{
min=array[0][0];

cout<<min; }
}cout<<endl;}

return (min);
}

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.