Write files from TXT into 2D array

Discussion in 'C++' started by kemnet, Nov 15, 2010.

  1. kemnet

    kemnet New Member

    Joined:
    Nov 15, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    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");
    		}
      }
    
    
    -------------------------------
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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?
     
  3. kemnet

    kemnet New Member

    Joined:
    Nov 15, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Changed it to what?
     
  5. kemnet

    kemnet New Member

    Joined:
    Nov 15, 2010
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    I actually found the solution with some help
    change it too
    Code:
    cout << result << endl
    thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice