Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

importing data into matrix (c++)

Posted on 2011-03-23
10
Medium Priority
?
445 Views
Last Modified: 2012-06-22
Hi experts.

novice c++ programmer here trying to do a pretty simple task.

I would like to read data in from a .txt and save it in a matrix that holds the same form.

I have tried working on this but is not pretty. attached is the code and the txt file.

the output should be the txt file without the -666666


#include <iostream>   // for input/output via screen
using namespace std;
#include <fstream>    // for input/output via files
#include <string>     // to enable data-type string
#include <cmath>          // for mathematical functions
#include <cstdlib>        //  -"-
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;

// This program reads values from the file 'example.txt'


int X[4][10];

int main()
{
   ifstream indata; // indata is like cin
   int num; // variable for input value

  indata.open("example.txt"); // opens the file
   if(!indata) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }

int k = 0;
// Put it in the matrix X
  for(int i = 0; i < 11;i++){
	indata >> num;	
	if(num == -666666){
		k++;
		i=-1;
	}
	else{
		X[k][i] = num;
	}
	cout << num << endl;
   }
// Print the matrix out;

cout << "The data read in is: " << endl;
for(int l = 0; l < 4;l++){
	for(int j = 0; j<10;j++){
	cout << X[l][j] << " ";
	}
cout << endl;
}


  return 0;
}

Open in new window

1 2 3 4 5 6 7 8 9 4 -666666 
1 1 1 1 1 1 1 1 1 1 -666666 
2 2 2 2 2 2 2 2 2 2 -666666 
3 3 3 3 3 3 3 3 3 3 -666666

Open in new window

0
Comment
Question by:NAB015
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35201250
Are you sure you showed us the code that generates this output ?
0
 

Author Comment

by:NAB015
ID: 35201272
It does not yet generate the correct output. That is my problem.
Thanks for the quick response
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35201324
Ah, I get it, the numbers are your txt file, not the output you get.

Looking at your code, there is the potential for an endless loop in the first for loop, because of this line :

>>             i=-1;

and the fact that you don't check for errors when reading from the file.

A minimal change to make your code work (better) would be to check for eof right after the indata >> num line :

        if (indata.eof()) break;

ie. end the loop if the end of the file is reached.


A better solution would be to use a nested loop (as well as proper error checking), rather than resetting the i loop counter.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:NAB015
ID: 35201399
Infinity,
I set i = -1 because the for loop then adds 1 bring it to 0 which is where I want it to store the next value X 0  i.

I know there is a really easy way to do this I just don't know C++ well enough to do it.
in pseudo code this would be ideal

for each line in document{ for each new line i++
     until the end of current line (counter for each value on line  j++
        save value into X
}
}







0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 1200 total points
ID: 35201473
>> I set i = -1 because the for loop then adds 1 bring it to 0 which is where I want it to store the next value X 0  i.

I understand that.


>> I know there is a really easy way to do this I just don't know C++ well enough to do it.

I gave you an easy way of doing it by adding this one line :

      indata >> num;
        if (indata.eof()) break;          // <--- this line right here

Just to see if that fixes your problem.

If you can confirm it fixes your problem, below is something that would do what your pseudo code suggests. Note that you probably want to get rid of the -666666 values in the file, because you don't need them with the below code. Note also that the code doesn't check the boundaries of the X array, which is something that you should do to ensure there's no buffer overflow.
size_t i = 0, j = 0;

std::string line;
while (getline(indata, line)) {     // read the next line of data from the file, into a string
    j = 0;
    std::stringstream ss(line);     // a stringstream used for parsing the line
    int num = 0;
    while (ss >> num) {             // get the next int value from the line
        X[i][j] = num;              // and store it in the array
        ++j;
    }
    ++i;
}

Open in new window

0
 

Author Comment

by:NAB015
ID: 35202114
infinity thanks for you continued help.

now im getting this error and unfortunatly I don't know c++ well enough to try debugging this.

33: error: variable ‘std::stringstream ss’ has initializer but incomplete type

-NB
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 1200 total points
ID: 35202137
You'll need to add the include for std::stringstream :

        #include <sstream>
0
 

Author Comment

by:NAB015
ID: 35202167
Just tried that. I attached my code.
#include <iostream>   // for input/output via screen
#include <fstream>    // for input/output via files
#include <string>     // to enable data-type string
#include <cmath>          // for mathematical functions
#include <cstdlib>        //  -"-
#include <sstream>
using namespace std;
using std::cerr;
using std::cout;
using std::endl;
using std::ifstream;
using std::stingstream;

// This program reads values from the file 'example.txt'


int X[4][10];

int main()
{
   ifstream indata; // indata is like cin
   int num; // variable for input value

  indata.open("example.txt"); // opens the file
   if(!indata) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }

size_t i = 0, j = 0;

std::string line;
while (getline(indata, line)) {     // read the next line of data from the file, into a string
    j = 0;
    std::stringstream ss(line);     // a stringstream used for parsing the line
    int num = 0;
    while (ss >> num) {             // get the next int value from the line
        X[i][j] = num;              // and store it in the array
        ++j;
    }
    ++i;
}


cout << "The data read in is: " << endl;
for(int l = 0; l < 4;l++){
	for(int j = 0; j<10;j++){
	cout << X[l][j] << " ";
	}
cout << endl;
}

  return 0;
}

Open in new window

0
 

Author Closing Comment

by:NAB015
ID: 35202304
Thanks for the help!
You learn so much just by doing this.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35202643
Glad to have been of assistance :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question