trouble with outputting program to a text file

I am done with my program and I am having trouble with the output text file. For my program, I already created an input file which my program will read into and I am also supposed to output the operations in my program to a text file. I made a text file on Notepad called "set_operations.txt". I implemented the ofstream commands in my program but it gives me a bunch of errors saying "outfile" is an undeclared identifier. Since I do not have a lot of experience with fstream, I was wondering where I should place my output text fie. Right now I have it opened and it is saved to my desktop. Is it supposed to be placed somewhere else. I put the input file in my program folder so should I do the same for the output text file. If so, where exactly in my program folder should I put it.

Here is my code. It is a long program but I just want you guys to check if I used my ifstream and ofstream correctly. Hopefully you guys could help. Thanks

code:
#include <iostream>
#include <fstream>
using namespace std;
void Union (int setA[10], int setB[10], int setU[20], int setNewU[20]);
void Intersection(int setA[10], int setB[10], int setX[10], int setNewX[10]);
void Differences(int setA[10], int setB[10], int setD[10], int setNewD[10], int setNewDtwo[10]);


int main()
{
ofstream outfile;
outfile.open("set_operations.txt");
ifstream infile;
int setA[10];
int setB[10];
int setU[20];
int setX[10];
int setD[10];
int setNewU[20];
int setNewX[10];
int setNewD[10];
int setNewDtwo[10];
int counter=0;

infile.open("sets.txt");


while(!infile.eof())
{
while(counter < 20)
{
if(counter < 10)
{
infile >> setA[counter];
}
else
{
infile >>setB[counter-10];
}
counter++;
}
counter = 0;
outfile<<"For set A= { ";

for(int i = 0; i < 20; i++)
{
if(i < 10)
{
outfile<<setA[i]<<" ";
}
}
outfile<<"} and ";
outfile<<"For set B= { ";
for(int i = 0; i < 20; i++)
{
if (i >= 10 && i<20)
{
outfile<<setB[i-10]<<" ";
}
}
outfile<<"}";
outfile<<endl<<endl;



Union (setA, setB ,setU, setNewU);
Intersection(setA, setB, setX, setNewX);
Differences(setA, setB, setD, setNewD, setNewDtwo);
}

infile.close();
outfile.close();

return 0;
}

void Union (int setA[10], int setB[10], int setU[20], int setNewU[20])
{
int sizeU=0;
int countItems= 0;
int numNotDuplicates = 0;
int unique=0;
//compute union
for(int i=0; i<10; i++) {
setU[i] = setA[i];
sizeU++;
}
int sizeUtwo = sizeU;
for(int i=0; i<10; i++) {
bool found=false;
for(int j=0; j<sizeUtwo; j++) {
if (setB[i] == setU[j]) {
found = true;
break;
}
}
if (!found) {
setU[sizeU] = setB[i];
sizeU++;
}
}
for(int i = 0; i < sizeU; i++)
{
if(i == 0)
{
setNewU[countItems] = setU[i];
countItems++;
}
else
{
for(int j = 0; j < countItems; j++)
{
if(setU[i] == setNewU[j])
{
break;
}
else
{
numNotDuplicates++;
}
}

if(countItems == numNotDuplicates)
{
setNewU[countItems] = setU[i];
countItems++;
}
}

numNotDuplicates = 0;
} //outer for

outfile<<"union B : { ";
for(int i = 0; i < countItems; i++)
{
outfile<<setNewU[i]<<" ";
}//end for i
outfile<<"}"<<endl;

for(int i=0; i < 20; i++)
{
if(setNewU[i]==setNewU[i+1])
{
continue;
}
else
unique++;
}
outfile<<"| A Union B | : "<< unique<<endl;

}

void Intersection(int setA[10], int setB[10], int setX[10], int setNewX[10])
{
int countItemstwo= 0;
int numNotDuplicatestwo = 0;
int sizeX=0;
int uniquetwo=0;
//compute intersection
for(int i=0; i<10; i++) {
for(int j=0; j<10; j++) {
if (setA[i] == setB[j]) {
setX[sizeX] = setA[i];
sizeX++;
}
}
}
for(int i = 0; i < sizeX; i++)
{
if(i == 0)
{
setNewX[countItemstwo] = setX[i];
countItemstwo++;
}
else
{
for(int j = 0; j < countItemstwo; j++)
{
if(setX[i] == setNewX[j])
{
break;
}
else
{
numNotDuplicatestwo++;
}
}

if(countItemstwo == numNotDuplicatestwo)
{
setNewX[countItemstwo] = setX[i];
countItemstwo++;
}
}

numNotDuplicatestwo = 0;
} //outer for

outfile<<"A intersect B: { ";
for(int i = 0; i < countItemstwo; i++)
{
outfile<<setNewX[i]<<" ";
}//end for i
outfile<<"}"<<endl;
for(int i=0; i < sizeX; i++)
{
if(setNewX[i]==setNewX[i+1])
{
continue;
}
else
uniquetwo++;
}
outfile<<"| A Intersect B | : "<< uniquetwo<<endl;
}




void Differences(int setA[10], int setB[10], int setD[10], int setNewD[10], int setNewDtwo[10])
{
int countItemsthree= 0;
int numNotDuplicatesthree = 0;
int countItemsfour=0;
int numNotDuplicatesfour=0;
int sizeD=0;
int uniquethree=0;
int uniquefour=0;
//compute differences
//A - B
for(int i=0; i<10; i++) {
bool found=false;
for(int j=0; j<10; j++) {
if (setA[i] == setB[j]) {
found = true;
break;
}
}
if (!found) {
setD[sizeD] = setA[i];
sizeD++;
}
}
for(int i = 0; i < sizeD; i++)
{
if(i == 0)
{
setNewD[countItemsthree] = setD[i];
countItemsthree++;
}
else
{
for(int j = 0; j < countItemsthree; j++)
{
if(setD[i] == setNewD[j])
{
break;
}
else
{
numNotDuplicatesthree++;
}
}

if(countItemsthree == numNotDuplicatesthree)
{
setNewD[countItemsthree] = setD[i];
countItemsthree++;
}
}

numNotDuplicatesthree = 0;
} //outer for

outfile<<"A - B : { ";
for(int i = 0; i < countItemsthree; i++)
{
outfile<<setNewD[i]<<" ";
}//end for i
outfile<<"}"<<endl;
for(int i=0; i < 10; i++)
{
if(setNewD[i]==setNewD[i+1])
{
continue;
}
else
uniquethree++;
}
outfile<<"| A - B | : "<< uniquethree<<endl;
sizeD=0;
//B - A
for(int i=0; i<10; i++) {
bool found=false;
for(int j=0; j<10; j++) {
if (setB[i] == setA[j]) {
found = true;
break;
}
}
if (!found) {
setD[sizeD] = setB[i];
sizeD++;
}
}
for(int i = 0; i < sizeD; i++)
{
if(i == 0)
{
setNewDtwo[countItemsfour] = setD[i];
countItemsfour++;
}
else
{
for(int j = 0; j < countItemsfour; j++)
{
if(setD[i] == setNewDtwo[j])
{
break;
}
else
{
numNotDuplicatesfour++;
}
}

if(countItemsfour == numNotDuplicatesfour)
{
setNewDtwo[countItemsfour] = setD[i];
countItemsfour++;
}
}

numNotDuplicatesfour = 0;
} //outer for

outfile<<"B - A : { ";
for(int i = 0; i < countItemsfour; i++)
{
outfile<<setNewDtwo[i]<<" ";
}//end for i
outfile<<"}"<<endl;
for(int i=0; i < sizeD; i++)
{
if(setNewDtwo[i]==setNewDtwo[i+1])
{
continue;
}
else
uniquefour++;
}
outfile<<"| B - A | : "<< uniquefour;
outfile<<endl<<endl;
}

OK guys I found out why the program was giving me a bunch of errors. I should declare the ofstream outfile before the main. But even when I do that and it is running, I still do not get an output. I know it has something to do with the output text file and where I should place. If you guys could help me out that would be great,
Last edited on
I'm new at this myself. I also notice that people out here seem to be reluctant to give much help with what might seem to be remedial prbs or studnts or hmwrk. But from what I understand, the compiler automatically produces the output file and creates it in the project file. You just have to make the input file(txt) and make sure you locate it in the project file. But the compiler creates the output file when you run the program. Assuming it runs successfully. Hope that's helpful.
closed account (3TkL1hU5)
It's not that people are reluctant to help at all.

People don't help out those who come here just asking for code to be written for them. And they may be a little reluctant to help if the problem can be found easily through search.

One thing that would help other people help you though would be to use the code tags and make your code a little more readable. When you make a new post if you look to the right hand side -------------------------------------> there is a "Format:" Section of buttons. The top-left button is for source code.
Last edited on
Topic archived. No new replies allowed.