GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 11-May-2005, 07:59
CT++ CT++ is offline
New Member
 
Join Date: May 2005
Posts: 7
CT++ is on a distinguished road

Having trouble reading in floats from a text file into a 2-dim array


Hello,

I have a text file that contains data similar to this table -http://www.itl.nist.gov/div898/handbook/eda/section3/eda3673.htm#ONE-05-1-10

I've stripped off any leading whitespaces, so it is just a continuous stream of decimal numbers separated by blank characters.

I want to be able to pass in two numbers to a function, use those two numbers as arguments to the array, and return a float value.

To do this, I wrote a function that reads the whole string of numbers in, and assigns them as row and column locations to a two dimensional array.

CPP / C++ / C Code:
float ShowF(int dfW, int dfB)
{
	const int numRows = 100;
	const int numCols = 10;
	float fCrit;
	float fVals[numRows][numCols];
	fstream inFile;								//	Sets file object
	inFile.open("new_values.txt", ios::in);		//	Opens file

	for (int rows=0; rows < numRows; rows++)
	{
		for (int cols=0; cols < numCols; cols++)
		{
			inFile >> fVals[rows][cols];
			cout << fVals[rows][cols] << " ";
		}
	cout << endl;
	}
	inFile.clear();	
	inFile.close();	
	fCrit = fVals[dfW][dfB];
	return fCrit;
}

This works perfectly except for one thing - it converts all the floats to ints!

This is a sample line of data;
161.448 199.500 215.707 224.583 230.162 233.986 236.768 238.882 240.543

This is what I get for output;
161 200 216 225 230 234 237 239 241

All my variables are floats, the array is a float....I don't get it. The '.' isn't a delimiter in this case, so I don't think that's the problem. Besides, it's not just dropping the decimal, it's actually rounding up or down to an int.

I thought this would be a snap, but it's driving me nuts I'm thinking of scrapping the array and trying to do this with a vector of strings, and slice up the string each time I call the function, but I understand that even less.

Any help would be tremendously appreciated! Thanks!
  #2  
Old 11-May-2005, 08:11
CT++ CT++ is offline
New Member
 
Join Date: May 2005
Posts: 7
CT++ is on a distinguished road
I found this http://h30097.www3.hp.com/cplus/clu006.htm but I am not sure how to set up the syntax right. It looks like it could read in just floats with an istream, but not sure how to work this into my code.

http://www.cs.niu.edu/~abyrnes/csci241/iostream.htm
  #3  
Old 23-Jun-2006, 18:21
chhandana chhandana is offline
New Member
 
Join Date: Jun 2006
Posts: 1
chhandana is on a distinguished road

Re: Having trouble reading in floats from a text file into a 2-dim array


Hi ,

Following code read the data with decimal points.

Thanks

Chandana

CPP / C++ / C Code:
#include < stdafx.h>
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  const int numrows=8;
  const int numcols=5;
  float eleval[numrows][numcols];

	//string line;
  fstream myfile ("data.dat", ios::in);
  if (myfile.is_open())
	  for (int rows=1;rows<numrows;rows++){
		  for (int cols=0;cols<numcols;cols++){
			  myfile >> eleval[rows][cols];
			  cout<<eleval[rows][cols]<<" ";
		  }
		  cout<<endl;
	  }
	  myfile.clear();
	  myfile.close();

		  

//  else cout << "Unable to open file"; 

  return 0;
}
Last edited by admin : 23-Jun-2006 at 18:37. Reason: Please insert your C code between [cpp] & [/cpp] tags
  #4  
Old 24-Jun-2006, 00:01
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Having trouble reading in floats from a text file into a 2-dim array


Guideline #1 -- how to post code
Guideline #2 -- what does the code do wrong, what does the file look like -- basically a detailed explanation of the problem you're having. Any answer to what you gave is a complete guess.
__________________

Age is unimportant -- except in cheese
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 3) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
CD Burner Help - Power Calibration Error.... JonBoy420 Computer Hardware Forum 110 05-Feb-2008 18:34
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.