I'm new here, but I think I have your code working. Two main comments:
First of all, there is no apparent need to keep a record of all the n variables, so a 2D array is not necessary. You can simply have an array of three int's, n[3], and overwrite them each time given you seem to be trashing them. You could even make it an array of 4 int's, n[4] and test n[0] as your dummy1 to clean up the code.
The other comment is more to the point. You need a different index for the eva_mesh array (and specifically for infile2) than the index being used to iterate through infile1, because the two files are not necessarily the same length, including in your example. While you may iterate through 4 lines from infile1 that don't start with a "3", you only want to have iterated once to the next line of infile2 when you reach the next "3". That bit of logic should fix your code. See bellow for what I've done. Not the best code, but I tried to change as little as possible so you could match it to yours.
As for it potentially being homework. If it offends people I answered this in such detail, I'm sorry but I'm new and going through the exercise helps me learn myself. Also, it only hurts sitikhadijahali if he doesn't learn this on his own for a class. Also, seems to me it's for a FEM application and not a CS class. Anyways, cheers to all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char **argv)
{
int noofneighbormesh = 7;
int i, j=0, dummy2;
int n[4];
double eva_mesh[4];
if(argc != 4)
{
cout << "Usage: ./exec infile1 infile2 outfile\n";
}
fstream infile1 (argv[1]);
fstream infile2 (argv[2]);
fstream outfile (argv[3]);
for(i=0; i<noofneighbormesh; i++)
{
infile1 >> n[0] >> n[1] >> n[2] >> n[3];
if(dummy1 == n[0])
{
infile2 >> dummy2 >> eva_mesh[j];
outfile << eva_mesh[j] << endl;
j++;
}
else
{
outfile << "100" << endl;
}
}
}
|