Read and write to a txt file

Everything runs except that
in the results.txt file created in the c drive the only number outputted is the number 5

how do i create a loop so the output in the result.txt file will be:
3
4
5

here is my code:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int num;
ifstream inFile;
ofstream outFile;


inFile.open("data.txt");


if (! inFile)
{
cout << "ERROR - File did not open!\n";
exit(1);
}

cout << "Here are the numbers in the file: \n";
while(inFile >> num)
{
cout << num << '\n';
}


outFile.open("c:\\result.txt");

if(outFile.fail())
{
cout << "ERROR - File did not open!\n";
system("pause");
return 1; /// or return 1
}


cout << endl << endl;
outFile << num;


inFile.close();
outFile.close();


system("pause");
return 0;
}


Not sure if this is what you mean

1
2
3
4
//loop to generate the numebers
for(int a=3; a<=5; a++){
outfile << a  << endl;
}

i meant to copy the data.txt file to results.txt file
i meant to copy the data.txt file to results.txt file


What WesleyB is hinting at, is you need to loop through to output multiple things. At the moment you only put 1 thing in the file.
Topic archived. No new replies allowed.