943,729 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2088
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Mar 22nd, 2009
0

Re: End of file function + Array

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:

C++ Syntax (Toggle Plain Text)
  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
Click image for larger version

Name:	resultsss.jpg
Views:	6
Size:	6.5 KB
ID:	9543  
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 22nd, 2009
0

Re: End of file function + Array

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  1. const double x = 0.549;
Last edited by NicAx64; Mar 22nd, 2009 at 5:59 am.
Reputation Points: 86
Solved Threads: 43
Posting Pro
NicAx64 is offline Offline
532 posts
since Mar 2009
Mar 22nd, 2009
0

Re: End of file function + Array

Click to Expand / Collapse  Quote originally posted by NicAx64 ...
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 22nd, 2009
0

Re: End of file function + Array

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 23rd, 2009
0

Re: End of file function + Array

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.

C++ Syntax (Toggle Plain Text)
  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
Click image for larger version

Name:	results22.jpg
Views:	5
Size:	4.6 KB
ID:	9555  
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 23rd, 2009
0

Re: End of file function + Array

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Mar 23rd, 2009
0

Re: End of file function + Array

Another thread solved by Ancient Dragon.
I wish you were our lecturer/ tutor.
Reputation Points: 7
Solved Threads: 0
Light Poster
ARYT is offline Offline
38 posts
since Feb 2009
Mar 23rd, 2009
0

Re: End of file function + Array

>>I wish you were our ... tutor

I am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Questions about memmove, malloc and realloc - C/C++ - Urgent
Next Thread in C++ Forum Timeline: opengl won't map textures





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC