End of file function + Array

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #11
Mar 22nd, 2009
OK, Here we go.

The input file is:

2 40
3 80
6 50
1 15
2 70

And here's the result (WTH?):

=> The Attachment

And the codes:

  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8.  
  9. int main()
  10.  
  11. {
  12.  
  13. ifstream infile("TEMPER.dat");
  14. ofstream outfile("RESULT.dat");
  15.  
  16. int i=0;
  17. double x = 0.549;
  18. double P[1000], V[1000], T[1000];
  19.  
  20. cout<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl
  21. <<"----\t"<<"------\t"<<"-----\t"<<"----"<<endl;
  22. outfile<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl
  23. <<"----\t"<<"------\t"<<"-----\t"<<"----"<<endl;
  24.  
  25. for (i=0; infile>>P[i]>>V[i]; i++)
  26. {
  27. infile>>P[i]>>V[i];
  28.  
  29. T[i]=((P[i]*V[i])/x);
  30.  
  31.  
  32. cout<<i+1<<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl;
  33.  
  34. outfile<<i+1<<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl;
  35.  
  36. }
  37. return 0;
  38. }
Attached Thumbnails
resultsss.jpg  
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 320
Reputation: NicAx64 will become famous soon enough NicAx64 will become famous soon enough 
Solved Threads: 19
NicAx64's Avatar
NicAx64 NicAx64 is offline Offline
Posting Whiz

Re: End of file function + Array

 
0
  #12
Mar 22nd, 2009
  1. double P[1000], V[1000], T[1000];


Next time try to use a STL vector for this . you can make a struct that contains int P , int V and int T and make a vector of that struct. And Feel free to use the STL libraries , they are implemented on many plactforms.

and you better using x like this

  1. const double x = 0.549;
Last edited by NicAx64; Mar 22nd, 2009 at 5:59 am.
Nothing like a kernel pannic !
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #13
Mar 22nd, 2009
Originally Posted by NicAx64 View Post
  1. double P[1000], V[1000], T[1000];


Next time try to use a STL vector for this . you can make a struct that contains int P , int V and int T and make a vector of that struct. And Feel free to use the STL libraries , they are implemented on many plactforms.

and you better using x like this

  1. const double x = 0.549;
Using "const double" doesn't make any difference in this case.
And as I said already, our tutor is a bit strict. For this assignment, we can't use STL library, dynmaic memory or ...

Any suggestion about the output? why is it only 3 and why random?
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: End of file function + Array

 
0
  #14
Mar 22nd, 2009
From the code you posted in #11 above:

line 27: delete that line because it is causing the program to read twice. The data was already read on line 25 in the for loop.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #15
Mar 23rd, 2009
Great! Works like a charm. Thanks AncientDragon.

Now let's look at this one which is almost the same, but I'm getting only the first line.

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8.  
  9. {
  10.  
  11. ifstream infile("Data.dat");
  12.  
  13.  
  14. int t[1000], v[1000], x[1000];
  15. int i=0;
  16.  
  17.  
  18. for (i=0; infile>>t[i]>>v[i]; i++)
  19. {
  20. x[i]=v[i]*t[i];
  21.  
  22. cout<<"The Displacement for t= "<<t[i]<<" and v= "<<v[i]<<" is: "<<x[i]<<endl<<endl;
  23.  
  24. }
  25. return 0;
  26. }

This is Data.dat:

0 0
0.2 0.1
0.4 0.3
0.6 0.6
0.8 0.9
1.0 1.4
1.2 1.9
1.4 2.6
1.6 3.5
1.8 4.5
2.0 5.7
2.2 7.0
2.4 8.5
2.6 9.9
2.8 11.5
3.0 12.8
3.2 14.2
3.4 16.4
3.6 18.4
3.8 20.1
4.0 19.5
4.2 18.7
4.4 16.6
4.6 15.5
4.8 14.1
5.0 12.9
5.2 11.5
5.4 9.6
5.6 8.0
5.8 6.4
6.0 5.2
6.2 3.9
6.4 3.2
6.6 2.6
6.8 2.1
7.0 1.7
7.2 1.2
7.4 0.9
7.6 0.5
7.8 0.1

And the result:

=> The attachment
Attached Thumbnails
results22.jpg  
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: End of file function + Array

 
0
  #16
Mar 23rd, 2009
make the arrays float instead of int. ifstream is producing an error attempting to convert that decimal point to an int.
Last edited by Ancient Dragon; Mar 23rd, 2009 at 8:52 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 38
Reputation: ARYT is an unknown quantity at this point 
Solved Threads: 0
ARYT's Avatar
ARYT ARYT is offline Offline
Light Poster

Re: End of file function + Array

 
0
  #17
Mar 23rd, 2009
Another thread solved by Ancient Dragon.
I wish you were our lecturer/ tutor.
Religion is a way of austerity and a mental self-tranquilizer which is associated with numerous cultures. One cannot deny that Moses predominate Jewish over other believers, while Mohammad calls Muslims to be the ones who excel in humanity and still they are the messengers sent from one God, postulating the impacts of religion and ideology on genetics?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,618
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1491
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: End of file function + Array

 
0
  #18
Mar 23rd, 2009
>>I wish you were our ... tutor

I am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC