// Attached: HW_6c
// ===========================================================
// File: HW_6c.cpp
// ===========================================================
// Programmer: Elaine Torrez
// Class: CS 1A
// ===========================================================
#include <iostream>
#include <fstream>
using namespace std;
// ==== main ===================================================
// This program reads and displays the numbers in results.txt.
// Then it prompts the user to enter 3 more numbers and appends
// them to the same file.
//
// Input:
// results.txt - A text file containing integer values.
// 3 integers entered by the user.
//
// Output:
// The current numbers in the file are displayed, and the new
// numbers are appended to results.txt.
// ===========================================================
int main()
{
ifstream inFile;
ofstream outFile;
int count;
int number;
int userNumber;
count = 0;
// open the file for reading
inFile.open("results.txt");
// check if the file opened successfully
if (!inFile)
{
cout << "Error opening file!" << endl;
return 0;
}
cout << "Here are the numbers in the file:" << endl;
// read and display the current numbers in the file
while (inFile >> number)
{
cout << number << endl;
}
inFile.close();
cout << endl;
cout << "Enter 3 more numbers:" << endl;
// open the file in append mode
outFile.open("results.txt", ios::app);
// get 3 more numbers from the user and append them
while (count < 3)
{
cin >> userNumber;
outFile << userNumber << endl;
count = count + 1;
}
outFile.close();
cout << "The numbers have been written (appended) to results.txt." << endl;
cout << endl;
cout << "Press any key to continue . . .";
cin.ignore();
cin.get();
return 0;
} // end of main()
// ===========================================================