Hi guys:

I created this program that will read into two files and compare a number from one file into the other and if they match, it will print out some information. I ave two if statements and it seems that the second one can't be read. I tried putting them in the same part but none are read. I know I need to fix my if statement, any ideas where ?

Thanks

Gus

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
    char fileToRead[20];
    char fileForTFR[20];
    char cmpFile[20];
    int numberImages, starsTFR;
    cout << "Enter name of tfr file(e.g. daom_iiv1.tfr): ";
    cin >> fileToRead;
    cout << "Enter name for output file (e.g. list_tfr.comp): ";
    cin >> fileForTFR;
    cout << "Enter name of cmp file to be read (e.g. se3tose1.cmp) ";
    cin >> cmpFile;
    cout << "How many companion stars in file: ";
    cin >> numberImages;
    cout << "How many stars in TFR file: ";
    cin >> starsTFR;

    ifstream in1, in2;
    in1.open(fileToRead);
    in2.open(cmpFile);
    ofstream out(fileForTFR);

    double xc, yc,cxc,cyx,cidc2,cxc2,cyxc2 ;
    vector<double> idc(starsTFR);
    vector<double> idil(starsTFR);
    vector<double> idis(starsTFR);
    vector<double> idv(starsTFR);
    vector<double> cidc(numberImages);
    for(int j=0;j<starsTFR;j++ )
    {
        for (int i=0;i<numberImages;i++)
        {
            if (in1 >>idc[j]>>xc>>yc>>idil[j]>>idis[j]>>idv[j] && in2 >>cidc[i],cxc,cyx,cidc2,cxc2,cyxc2) 
            {
                out <<idc[j] << endl;
//          for(int i=0;i<numberImages;i++)
//          {
//              if (in2 >>cidc[i],cxc,cyx,cidc2,cxc2,cyxc2)
//              {[CODE][/CODE]
                    out <<cidc[i];
                    if (idc[j]==cidc[i]) 
                    {
                        out << cidc[i] << " " << idil[j] << " " << idis[j] << " " << idv[j] << " " << idc[j] << endl;
                    }
//              }
//          }
            }
        }
    }
    out << endl;
    return 0;
}

tfr file

   1  333.497  288.780     1     1     1
   2  -40.218  324.683     4     4     4
   3  453.943  539.927     3     3     6
   4   10.354  584.221     8     8     2
   5  358.160  589.220     2     2    15
   6  630.655  766.242    10    10     3
   7  666.667  124.235     6     6     5
   8  439.938  920.690     5     5     8
   9  689.891  874.437     7     7     9
  10  506.154 1000.178     9     9    23
  11  617.505  631.415    13    12    12
  12  459.831  914.150    25    29     7
  13  569.784  778.539    14    16    42
  14  435.473  849.193    22    26    19

cmp file

  10  506.15 1000.18      9  506.12 1000.14
  13  569.78  778.54     15  569.75  778.51
  14  435.47  849.19     16  435.45  849.19
  16  168.07  999.13     19  168.07  999.13
  22  608.81 1063.53     26  608.75 1063.48
  23   95.55  551.93     29   95.57  551.94

I wouldn't "read" the second IF either if I designed it that way.

Assuming your files will always be in sorted order as you posted them:

Read from file in1 
Read from file in2
loop until EOF
  cmp = compare in1-line with in2-line
  if < read in1 again
  else 
  if > read in2 again
  else 
  if =
    do = stuff
    read in1 again
    read in2 again
endloop

Don't make the IF do so much work. It makes the statement too hard to read, and you will always read both files making the comparisons very inaccurate (and wrong).

Aren't you beginning programmers taught how to desk-check your code using pencil and paper?

I apologize for the lack of practical knowledge, still a beginner. I have tweaked my for and if loops and organized them in a better matter, but now the output is giving is pretty much everything it the tfr file and not just the number that match the tfr and the cmp file. Here is where I change things

for(int j=0;j<starsTFR;j++)
	{
		for (int i=0;i<numberImages;i++)
		{
				in1 >>idc[j]>>xc>>yc>>idil[j]>>idis[j]>>idv[j];
				in2 >>cidc[i],cxc,cyx,cidc2,cxc2,cyxc2; 
//					out <<idc[j] << endl;
//					out <<cidc[i];
					if (cidc[i]==idc[j]) 
					{
						out << cidc[i] << " " << idil[j] << " " << idis[j] << " " << idv[j] << " " << idc[j] << endl;
					}
		}
	}
	out << endl;
	return 0;

I apologize for the lack of practical knowledge, still a beginner.

I guess it would be simpler if you were born with this information already, but since we're not, no apology necessary. And it's extremely obvious. :icon_wink:

Now, your tweak was completely useless, as you could tell. Here's why:
1: I showed 1 loop, you have 2
2: I showed 3 IFs, you have 1
3: I have 3 reads from each file, you have 1

Did I waste my time giving you the algorithm? Did you in fact desk-check your code like I implied? My guess so far is yes and no. How'd I do? :icon_rolleyes:

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.