Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Write files from TXT into 2D array (http://www.go4expert.com/forums/write-files-txt-2d-array-t23876/)

kemnet 15Nov2010 17:46

Write files from TXT into 2D array
Hello ALl im new just wanted to ask a quick question thanks.

so im writing a program that reads in data from a ".txt" file and im supposed to use it to do computations

so I figured i should write the values into an array(or 2D array)
so i wrote the code below:

So this works except that my file "example.txt." looks like this
5.5 2.1
2.7 4.3

but when i write it into my array and executes it appears as

one big value

is there a better way to write it into my array so ill be able to reference one value at a time? or a different way i should go about with this problem?
Ive read a few issues similar to mine on forums and got helped up too this far just not as far as my problem


#include "stdafx.h"
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
#define newline '\n'
using namespace std;

float result = 0;
int main()
// Read Data File into a 2-D Array
ifstream inFile ("example.txt");
inFile.setf(ios::fixed, ios::showpoint);
  // Declare Variables to Store Array Information
  int parcel [3] [3];
float dis [3];
  int id [3];
  int A=0;
  int B=0;
  int c=0;

//  Text File
          ifstream inStream;
          if (inStream.fail()) {
          cout<< "Can't open file!\n";
          else {
                  while (!inStream.eof()) {
 // write into Array
                  for (int i = 0; i <2; i++) {
                  inStream >> dis[i];
                result =dis[i];

                cout << result;"\n";
          // Close File



xpi0t0s 16Nov2010 12:06

Re: Write files from TXT into 2D array
You probably get a compiler warning at the line

cout << result;"\n";
You shouldn't ignore it. As a semicolon is a statement separator let me rewrite that slightly:

cout << result;

which is exactly the same. Can you see the problem now?

kemnet 16Nov2010 16:06

Re: Write files from TXT into 2D array
Thanks for the tip but i changed it and the output and problem remains the same...i need to find a way to separate the output.

xpi0t0s 16Nov2010 20:10

Re: Write files from TXT into 2D array
Changed it to what?

kemnet 17Nov2010 17:42

Re: Write files from TXT into 2D array
I actually found the solution with some help
change it too

cout << result << endl

All times are GMT +5.5. The time now is 08:55.