Write files from TXT into 2D array

kemnet's Avatar, Join Date: Nov 2010
Go4Expert Member
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
5.52.12.74.3


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?
thanks.
Ive read a few issues similar to mine on forums and got helped up too this far just not as far as my problem

Code:
#include "stdafx.h"
#include<iostream>
#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.precision(2);
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;
  	inStream.open("example.txt");
 
  	if (inStream.fail()) {
  	cout<< "Can't open file!\n";
  	system("pause");
  	}
 
  	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

  	inStream.close();
	system("pause");
		}
  }
-------------------------------
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
You probably get a compiler warning at the line
Code:
cout << result;"\n";
You shouldn't ignore it. As a semicolon is a statement separator let me rewrite that slightly:
Code:
cout << result;
"\n";
which is exactly the same. Can you see the problem now?
0
kemnet's Avatar, Join Date: Nov 2010
Go4Expert Member
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.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Changed it to what?
0
kemnet's Avatar, Join Date: Nov 2010
Go4Expert Member
I actually found the solution with some help
change it too
Code:
cout << result << endl
thanks