| | |
End of file function + Array
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
It's me again.
This is the question: Write a computer program that computes the temperature of a gas that is originally at P=5 atm, =V30 Liters, T=273 K. Solve the problem using one dimensional array only. Assume number of array elements is unknown (use the end of file function).
And we have 10 errors:
for
3X error C2057: expected constant expression
3X error C2466: cannot allocate an array of constant size 0
error C2133: 'P/T/V' : unknown size
for
error C2106: '=' : left operand must be l-value
This is the question: Write a computer program that computes the temperature of a gas that is originally at P=5 atm, =V30 Liters, T=273 K. Solve the problem using one dimensional array only. Assume number of array elements is unknown (use the end of file function).
And we have 10 errors:
for
C++ Syntax (Toggle Plain Text)
int P[N], T[N], V[N];
3X error C2057: expected constant expression
3X error C2466: cannot allocate an array of constant size 0
error C2133: 'P/T/V' : unknown size
for
C++ Syntax (Toggle Plain Text)
((Pinit*Vinit)/Tinit)=((P[i]*V[i])/T[i]);
error C2106: '=' : left operand must be l-value
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std; ifstream infile("TEMPER.dat"); ofstream outfile("RESULT.dat"); int main() { int N; N=infile.eof(); cout<<"There are "<<N<<" elements in the input file"<<endl; int i=0; double Pinit=5, Vinit=30, Tinit=273; double P[N], T[N], V[N]; for (i=0; !infile.eof(); i++) { infile>>P[i]>>V[i]; ((Pinit*Vinit)/Tinit)=((P[i]*V[i])/T[i]); for (i=0; i<N; i--) {cout<<i; outfile<<i;}; cout<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl <<"----"<<"------"<<"-----"<<"----"<<endl <<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl; outfile<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl <<"----"<<"------"<<"-----"<<"----"<<endl <<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl; } return 0; }
You cannot asign an unknown value to the size of an array at compile time. You need to use dynamic memory.
For the second bit, you cannot do a mathematical equation on the left hand side of an assignment operator. It must be a variable etc.
Chris
C++ Syntax (Toggle Plain Text)
double *P = new double[N]; ... delete [] P;
For the second bit, you cannot do a mathematical equation on the left hand side of an assignment operator. It must be a variable etc.
Chris
Last edited by Freaky_Chris; Mar 20th, 2009 at 5:44 am.
Knowledge is power -- But experience is everything
So I have defined a variable:
And editted the equation:
And the error is gone. Thanks
Btw, we cannot use dynamic memory for this one. Any alternative?
Cheers
C++ Syntax (Toggle Plain Text)
double x=0.549;
C++ Syntax (Toggle Plain Text)
x= ((P[i]*V[i])/T[i])
And the error is gone. Thanks
Btw, we cannot use dynamic memory for this one. Any alternative?
Cheers
lines 8 and 9. Files can not be opened outside a function like you have it. Move both those lines down to within main() (line 14).
line 16: eof() will not give you the file size, it just tells you that the end-of-file has been reached and nothing more. To get the file size
line 22: Not only doesn't that work with most compilers (I think it will when the newest c++ standards are implemented), but its also wrong. The value of N (see above) will not tell you the number of integers in the file, but how many characters are in the file. For example the number 100 will be counted as 4 or 5 depending on the operating system (3 digits plus '\n').
To get the actual number of integers in the file you will have to read each line and count them as you go along.
line 25: eof() doesn't work like that either because it will cause your program to process the last line twice. A better solution is like this
line 16: eof() will not give you the file size, it just tells you that the end-of-file has been reached and nothing more. To get the file size
C++ Syntax (Toggle Plain Text)
// set file pointer to end-of-file infile.seekp(0, ios::end); // get file location N = infile.tellg();
line 22: Not only doesn't that work with most compilers (I think it will when the newest c++ standards are implemented), but its also wrong. The value of N (see above) will not tell you the number of integers in the file, but how many characters are in the file. For example the number 100 will be counted as 4 or 5 depending on the operating system (3 digits plus '\n').
To get the actual number of integers in the file you will have to read each line and count them as you go along.
line 25: eof() doesn't work like that either because it will cause your program to process the last line twice. A better solution is like this
C++ Syntax (Toggle Plain Text)
int i = 0; while( infile>>P[i]>>V[i] ) { // blabla ++i; }
Last edited by Ancient Dragon; Mar 20th, 2009 at 7:38 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.
[QUOTE] I won't use this code, because I'm sure, the tutor will kill me.
I told you to use "End of file" function ONLY, he'd say.
In that way, how the initial array definition should be?
e.g. int P[n], V[n], T[n]
How do we determine the value of n?
•
•
•
•
lines 8 and 9. Files can not be opened outside a function like you have it. Move both those lines down to within main() (line 14).
line 16: eof() will not give you the file size, it just tells you that the end-of-file has been reached and nothing more. To get the file size
C++ Syntax (Toggle Plain Text)
// set file pointer to end-of-file infile.seekp(0, ios::end); // get file location N = infile.tellg();
I told you to use "End of file" function ONLY, he'd say. •
•
•
•
line 22: Not only doesn't that work with most compilers (I think it will when the newest c++ standards are implemented), but its also wrong. The value of N (see above) will not tell you the number of integers in the file, but how many characters are in the file. For example the number 100 will be counted as 4 or 5 depending on the operating system (3 digits plus '\n').
To get the actual number of integers in the file you will have to read each line and count them as you go along.
line 25: eof() doesn't work like that either because it will cause your program to process the last line twice. A better solution is like this
C++ Syntax (Toggle Plain Text)
int i = 0; while( infile>>P[i]>>V[i] ) { // blabla ++i; }
e.g. int P[n], V[n], T[n]
How do we determine the value of n?
>How do we determine the value of n?
If you can't use dynamic memory (and I'm assuming any alternative that uses dynamic memory, like std::vector), your only option is to make the array large enough to handle any reasonable number of records:
Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:
If you can't use dynamic memory (and I'm assuming any alternative that uses dynamic memory, like std::vector), your only option is to make the array large enough to handle any reasonable number of records:
C++ Syntax (Toggle Plain Text)
int P[1000], T[1000], V[1000];
- You potentially waste a *lot* of space.
- You have little choice but to ignore any records beyond N unless you're able to get clever and manage chunks of the file instead of the whole thing all at once.
I'm here to prove you wrong.
•
•
•
•
>How do we determine the value of n?
If you can't use dynamic memory (and I'm assuming any alternative that uses dynamic memory, like std::vector), your only option is to make the array large enough to handle any reasonable number of records:
Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:C++ Syntax (Toggle Plain Text)
int P[1000], T[1000], V[1000];
- You potentially waste a *lot* of space.
- You have little choice but to ignore any records beyond N unless you're able to get clever and manage chunks of the file instead of the whole thing all at once.
I actually know that the input file contains 10 values only, but our tutor is such a psycho.
He wants to suffer us. 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?
>So, I cannot use "End of File" function at all. Right?
You can, but it would be more along the lines of stopping the input loop prematurely instead of determining the array size:
Which is quite pointless when you could do this instead:
You can, but it would be more along the lines of stopping the input loop prematurely instead of determining the array size:
C++ Syntax (Toggle Plain Text)
for ( int i = 0; i < 1000; i++ ) { // Don't overrun the array infile>> P[i] >> V[i]; if ( infile.eof() ) break; // The records were successfully read }
C++ Syntax (Toggle Plain Text)
for ( int i = 0; i < 1000 && infile>> P[i] >> V[i]; i++ ) { // The records were successfully read }
Last edited by Narue; Mar 20th, 2009 at 12:06 pm.
I'm here to prove you wrong.
How do we define the initial array for the second choice?
int P[?], V[?], T[?] ???
int P[?], V[?], T[?] ???
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?
![]() |
Similar Threads
- Safe Array (C++)
- Programming in VBA reading file into dynamic array (VB.NET)
- Programming VBA: Reading excel into an Array (Visual Basic 4 / 5 / 6)
- How to read excel cells into Array (Visual Basic 4 / 5 / 6)
- Reading a file into a Parallel Array (C++)
- seek function (Perl)
- C++ complete binary tree using an array. Unexpected end file (C++)
- reading a file into code (Java)
- File parsing in 'C' (C)
Other Threads in the C++ Forum
- Previous Thread: Questions about memmove, malloc and realloc - C/C++ - Urgent
- Next Thread: opengl won't map textures
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






