7 Replies - 525 Views - Last Post: 17 September 2013 - 10:34 PM Rate Topic: -----

#1 smilesthunder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-October 12

Reading in doubles Error

Posted 17 September 2013 - 08:06 PM

Hey guys!

I'm having some trouble reading in a simple file and getting it to display properly (I need to use the values later for calculations).

This is a sample file...
312.1398
6.500 3.000 1.000
3.000 1.500 1.000
5.000 1.500 2.000
0.500 0.500 0.100
0.500 1.500 0.100
0.500 2.500 0.100
4.000 0.500 0.100
4.000 2.500 0.100
6.000 0.500 0.100
6.000 2.500 0.100


Trouble I'm having is with the first number which represents a weight for a plate and the others represent dimensions for holes. It should essentially output that, but instead I get
Weight is (1) heavy.
Hole 1 is (312.14) wide, (6.5) high, and (3) thick.
Hole 2 is (3) wide, (1.5) high, and (1) thick.
Hole 3 is (1.5) wide, (2) high, and (0.5) thick.
Hole 4 is (0.1) wide, (0.5) high, and (1.5) thick.
Hole 5 is (0.5) wide, (2.5) high, and (0.1) thick.
Hole 6 is (0.5) wide, (0.1) high, and (4) thick.
Hole 7 is (0.1) wide, (6) high, and (0.5) thick.

when I run this function on it
void outputPlateList(vector<Plate> pList)
{
  int i = 0;
  cout << "Weight is (" << pList[i].getWeight() <<") heavy." << endl;
  for(i = 0; i < pList.size(); i++)
  {

    //cout << "Weight is (" << pList[i+1].getWeight() <<") heavy." << endl;
    cout << "Hole " << i + 1 << " is ("<< pList[i].getWidth() <<") wide, ("<< pList[i].getHeight() <<") high, and (" <<pList[i].getThick() <<") thick." << endl;
    
  }
}


And this is how I load the file

int main(int argc, char * argv[])
{
	if (argc < 2)
	{
		cout << "ERROR: You need to specify an input file on the command line"  << endl;
		return 0;
	}

	ifstream inFile (argv[1]);
	if (!inFile.is_open())
	{
		cout << "ERROR: Unable to open file" << endl;
		return 0;
	}
	
	vector<Plate> plateList;
	
	double widthIn;
	double heightIn;
	double thickIn; //thickness
	double weightIn;
	
	inFile>>widthIn;
	inFile>>heightIn;
	inFile>>thickIn;
	inFile>>weightIn;
	
	while(inFile.good())
	{
	  Plate myPlate(widthIn, heightIn, thickIn, weightIn);
	  plateList.push_back(myPlate);
	  
	  inFile>>widthIn;
	  inFile>>heightIn;
	  inFile>>thickIn;
	  //inFile>>weightIn;
	}
	
	inFile.close();


I feel like this is a simple fix, but I am stumped!
Thanks!

This post has been edited by smilesthunder: 17 September 2013 - 08:09 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Reading in doubles Error

#2 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Reading in doubles Error

Posted 17 September 2013 - 08:51 PM

Hi, you are reading the weight after width, height, thickness :

23	    inFile>>widthIn;
24	    inFile>>heightIn;
25	    inFile>>thickIn;
26	    inFile>>weightIn;



so you want to move line 26 to before line 23.
Was This Post Helpful? 0
  • +
  • -

#3 smilesthunder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-October 12

Re: Reading in doubles Error

Posted 17 September 2013 - 09:09 PM

Thanks! Definitely looks better! Now I'm just having trouble including the last line of text. Prints this...

Weight is (312.14) heavy.
Hole 1 is (6.5) wide, (3) high, and (1) thick.
Hole 2 is (3) wide, (1.5) high, and (1) thick.
Hole 3 is (5) wide, (1.5) high, and (2) thick.
Hole 4 is (0.5) wide, (0.5) high, and (0.1) thick.
Hole 5 is (0.5) wide, (1.5) high, and (0.1) thick.
Hole 6 is (0.5) wide, (2.5) high, and (0.1) thick.
Hole 7 is (4) wide, (0.5) high, and (0.1) thick.
Hole 8 is (4) wide, (2.5) high, and (0.1) thick.
Hole 9 is (6) wide, (0.5) high, and (0.1) thick.



Making it go until
for(i = 0; i <= pList.size(); i++)
spits out gibberish for the last row
Was This Post Helpful? 0
  • +
  • -

#4 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Reading in doubles Error

Posted 17 September 2013 - 09:31 PM

Hi.

const SIZE = 3;
int array[SIZE];



What are the valid indexes of the array of size 3? 0, 1, 2.

What is the problem here?

for(i = 0; i <= pList.size(); i++)

Was This Post Helpful? 0
  • +
  • -

#5 smilesthunder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-October 12

Re: Reading in doubles Error

Posted 17 September 2013 - 09:37 PM

Hmmm... I think since my last row is the same as the max size, it is not getting read properly . So I just tried
i <= pList.size() + 1
and the same w/o = sign. But maybe I'm misunderstanding the problem.
Was This Post Helpful? 0
  • +
  • -

#6 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Reading in doubles Error

Posted 17 September 2013 - 09:44 PM

If we say that the list/array size is 2, and the valid indexes are 0, 1, (because it starts at 0) then your for loop would look like:

for(i = 0; i <= 2; i++)
 cout << i << endl;


Was This Post Helpful? 0
  • +
  • -

#7 smilesthunder   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 15-October 12

Re: Reading in doubles Error

Posted 17 September 2013 - 09:49 PM

Right I understand that. That's why I'm confused my array stops before my last element since the loop is
for(i = 0; i <= pList.size(); i++)
Am I wrong that it should include my last row since I used the <= operator? It stops before the last row.

P.S. It is late, so maybe I'm not seeing something incredibly obvious.
Was This Post Helpful? 0
  • +
  • -

#8 #define   User is offline

  • Duke of Err
  • member icon

Reputation: 1853
  • View blog
  • Posts: 6,671
  • Joined: 19-February 09

Re: Reading in doubles Error

Posted 17 September 2013 - 10:34 PM

If size is 2, valid indexes are array[0] and array[1]
then the loop

i=0, i <= 2 yes, print 0 
i=1, i <= 2 yes, print 1 
i=2, i <= 2 yes, error - outside bounds of array 
i=3, i <= 2 no,  finish loop 



The for loop is commonly used with arrays, and the usual way it is written is:

for(i = 0; i < pList.size(); i++)


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1