I am trying to write a program that reads text from two different input files. the text from the first input file, made up of words, and the text from the second input file, made up of lines of text, are both being written to an output file that the user names.
I have all of the code and the program compiles succesfully, but when I run it it only reads from the first file and not the second one. but if i take out the portion of code that opens, reads, outputs, and closes the first input file ("words.txt"), then the second part DOES read from the second input file ("phrases.txt"). So why in the world won't it do both? what is the error in my code and how do I fix it? My teacher said something about the ignore() thing, but even when I try that it still only reads and writes the first section.
help please!!
CODE
#include<iostream> // Lab 4_B by Tim Nelson
#include<fstream>
using namespace std;
int main()
{
ifstream infile; // keep on declarin' those I/O variables!!
ofstream outfile;
char word1[20], word2[20], word3[20], word4[20], fileName[20];
char text1[40], text2[40], text3[40];
cout << "Please enter the name of the output file you would like the data written to: "
<< endl;
cin >> fileName;
cout << "Thank you, the data will be written to " << fileName << endl;
outfile.open(fileName);
infile.open("words.txt");
infile >> word1 >> word2 >> word3 >> word4;
outfile << word1 << endl
<< word2 << endl
<< word3 << endl
<< word4 << endl;
infile.close(); //make sure to close the first input file before opening the second
infile.open("phrases.txt");
infile.getline(text1, 40);
infile.getline(text2, 40);
infile.getline(text3, 40);
outfile << text1 << endl;
outfile << text2 << endl;
outfile << text3 << endl;
infile.close();
outfile.close();
return 0;
}