| CODE |
#include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main() { int num; char filename[16], cont, again; ifstream instream; ofstream outstream; cout << "This program allows the user to open a file and write " << "some positive integers on it, ending at -1. The program " << "will then echo back the input."; do { do { cout << "Please enter in the name of a file\n"; cin >> filename; outstream.open(filename); if (outstream.fail() ) { cout << "The file failed to open\n"; system ("pause"); exit (1); } cout << filename << " opened succesfully, overwrite? " << "(enter \"Y\" if yes.\n"; cin >> cont; }while (cont != 'Y' || cont != 'y'); cout << "Enter a bunch of positive integers and when you " << "are done, enter a -1\n\n"; num = 1; while (num != -1) { cout << "Enter a number\n"; cin >> num; if (num >= 0) { outstream << num << " "; } else if(num != -1) { cout << num << "must be greater or equal " << "to 0 "; } } outstream << num; outstream.close(); instream.open(filename); cout << "I will now read the file you have modified :\n\n"; instream >> filename; instream.close(); cout << "Would you like to run this program again? enter " << "\"y\" if yes."; char again; }while (again == 'y' || again == 'Y'); return 0; |