Modify the program similar to the eBook Collection Program! (The
program should only display the multiplication table and not ask
the users any input. MAKE SURE TO DETERMINE WHETHER THE FILE WAS
OPENED!!!)
(Use if statements to open and close inFile and OutFile
)
Ex: if (outFile.is_open())
outFile.close();
if(inFile.is_open())
inFile.close();
оор ол Есм 3 4 5 6 7 8 #include <iostream> #include <string> #include <fstream> using namespace std; void displayMultiplicationTable(ofstream &out, int n); void displayAllMultiplicationTable(string fileName); ===”三 Qint main() { displayAllMultiplicationTable("multiplicationtable.txt"); cout << "The output is in the multiplication table text file" << endl; return; avoid displayMultiplicationTable(ofstream& out, int n) { int range = 12; out « n « "X" << endl; 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 CORG for (int i = 1; i <= range; ++i) { out « i « " X " « n « " = " « n * i < endl; out << endl; -D avoid displayAllMultiplicationTable(string fileName) { ofstream out(fileName.c_str()); for (int i = 1; i <= 12; i++) { displayMultiplicationTable(out, i); } out.close();
The eBook Collection Program (cont'd.) 1 //eBook Collection.cpp gets and displays the 2 //items in an eBook collection 3 //Created/revised by <your name> on <current date> 4 5 #include <iostream> 6 #include <string> 7 #include <fstream> 8 using namespace std; 9 10 //function prototypes 11 void saveInfo(); 12 void displayInfo(); 13 14 int main() 15 { 16 saveInfo(); 17 displayInfo(); 18 return 0; 19 //end of main function 20 Figure 14-12 Beginning of the eBook Collection Program
The eBook Collection Program (cont'd.) 37 38 39 21 1/*****function definitions***** 22 void saveInfo 23 { 24 [/writes records to a sequential access file 25 string title 26 string author author - ""; 28 //create file object and open the file 29 ofstream outFile; 30 outfile.open("eBooks.txt", ios: : app); :: 31 32 //determine whether the file was opened 33 if (outFile.is_open) 34 { 35 cout << "Title (-1 to stop): " 36 getline (cin, title); while (title != "-1") { cout << "Author: ": 40 getline (cin, author: 41 1/write the the record 42 outFile << title << << "#" author << endl; 43 44 cout << "Title "Title (-1 to (-1 to stop): "; 45 getline (cin, title: } Vend while 47 outfile.close ; } 49 else 50 << "eBooks.txt file could not be opened" 51 << endl; 52 Ilend if 53 } end of saveInfo function 54 void displayInfo 56 { 57 //displays the records stored in the file 58 string title = 59 string author - 60 61 //create file object and open the file 62 ifstream in File; 63 infile.open("eBooks.txt", os::in); ) 64 65 //determine whether the file was opened 66 if (inFile.is_open) 67 { 46 48 Cout Figure 14-12 Continuation of the eBook Collection Program
The eBook Collection Program (cont'd.) << cout << endl << endl << "eBook Collection" << endl; cout << endl; //read a record getline(inFile, title, '#'); getline(inFile, author); 11 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 } while (!inFile.eof()) { //display the record cout << title << " by << author << endl; //read another record getline(inFile, title, #'); getline(inFile, author); } //end while inFile.close(); } else cout "eBooks.txt file could not be opened" << endl; //end if //end of displayInfo function << Figure 14-12 Completion of the eBook Collection Program
Modify the program similar to the eBook Collection Program! (The program should only display the multiplication table an
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Modify the program similar to the eBook Collection Program! (The program should only display the multiplication table an
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!