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

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

((Pinit*Vinit)/Tinit)=((P[i]*V[i])/T[i]);

error C2106: '=' : left operand must be l-value

#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.

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

So I have defined a variable:

double x=0.549;

And editted the equation:

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

// 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

int i = 0;
while( infile>>P[i]>>V[i] )
{
   // blabla

   ++i;
}

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

// set file pointer to end-of-file
infile.seekp(0, ios::end);
// get file location
N = infile.tellg();

I won't use this code, because I'm sure, the tutor will kill me. :D 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

int i = 0;
while( infile>>P[i]>>V[i] )
{
   // blabla

   ++i;
}

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?

>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:

int P[1000], T[1000], V[1000];

Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:

  1. You potentially waste a *lot* of space.
  2. 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.

>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:

int P[1000], T[1000], V[1000];

Of course, that size would pretty much be an arbitrary choice, and there are two immediate problems that arise from it:

  1. You potentially waste a *lot* of space.
  2. 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.

So, I cannot use "End of File" function at all. Right?

I actually know that the input file contains 10 values only, but our tutor is such a psycho. :D He wants to suffer us.

>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:

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
}

Which is quite pointless when you could do this instead:

for ( int i = 0; i < 1000 && infile>> P[i] >> V[i]; i++ ) {
  // The records were successfully read
}

How do we define the initial array for the second choice?

int P[?], V[?], T[?] ???

The definition of your arrays doesn't change. But if you know that there will only be ten records, 1000 is a smidge too big.

OK, Here we go.

The input file is:

2 40
3 80
6 50
1 15
2 70

And here's the result (WTH?)::-O

=> The Attachment

And the codes:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;



int main()

{

ifstream infile("TEMPER.dat");
ofstream outfile("RESULT.dat");

int i=0;
double x = 0.549;
double P[1000], V[1000], T[1000]; 

cout<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl 
    <<"----\t"<<"------\t"<<"-----\t"<<"----"<<endl;
outfile<<"Case\t"<<"P(atm)\t"<<"V(l)\t"<<"T(K)"<<endl 
       <<"----\t"<<"------\t"<<"-----\t"<<"----"<<endl;

for (i=0; infile>>P[i]>>V[i]; i++)
{ 
	infile>>P[i]>>V[i];

	T[i]=((P[i]*V[i])/x);


	cout<<i+1<<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl;

	outfile<<i+1<<"\t"<<P[i]<<"\t"<<V[i]<<"\t"<<T[i]<<"\t"<<endl;
 
}
return 0;
}
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

const double x = 0.549;
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

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?

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.

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.

#include <iostream>
#include <fstream>

using namespace std;


int main()

{

ifstream infile("Data.dat");


int t[1000], v[1000], x[1000];
int i=0;


for (i=0; infile>>t[i]>>v[i]; i++)
{ 
	x[i]=v[i]*t[i];

cout<<"The Displacement for t= "<<t[i]<<" and v= "<<v[i]<<" is: "<<x[i]<<endl<<endl;
 
}
return 0;
}

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

make the arrays float instead of int. ifstream is producing an error attempting to convert that decimal point to an int.

Another thread solved by Ancient Dragon.
I wish you were our lecturer/ tutor. :(

>>I wish you were our ... tutor

I am. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.