C++ Trouble reading and writing to text file using the fstream object -
i'm c++ rookie , having little trouble completing assignment, , teacher not going available next week, i'm stuck help! i'm sure i'm tired , missing small, i'm having trouble getting fstream object create file, , read print screen, else seems work ok.
here instructions assignment, pretty straightforward , basic:
1 - write program calculate area , circumference of circles.
2 - main input radii of circles via keyboard , store in array. must done via loop. assume maximum of 100 records.
3 - call function calculate circumference of each circle using radii above , store in array.
4 - call function calculate area of circle , store in array.
5 - main print screen radius, circumference , area of circles. information should printed data in 3 arrays. before printing actual data, print labels "radius", "circumference" , "area" , align information under each label.
6 - in main create fstream object output file called lecture20output.txt.
7 - call function write radius, circumference , area in arrays above lab20output.txt
8 - main print screen contents of lab20output.txt.
9 - sample run: radius 5, 4, 7.
#include <iostream> #include <iomanip> #include <fstream> #include <string> using namespace std; // prototypes void getcircumf(const double, double, double &); void getarea(const double, double, double &); void writeoutfile(int, double[], double[], double[], fstream &); void greeting(); int main() { const int array_size = 100; const double pi = 3.14; int = 0, i2 = 0; double radii[array_size], circumf[array_size], area[array_size]; fstream myfile; string line; // use loop prompt user radii cout << "==============================================================" << endl; cout << " below, may enter of radii (up 100 entries) " << endl; cout << " *** enter 0 (zero) when finished *** " << endl; cout << "==============================================================" << endl; while (i<100) { cout << "\nenter radius: "; cin >> radii[i]; if (radii[i] == 0) // test if user has no more entries = 100; else { getcircumf(pi, radii[i], circumf[i]); // call function calculate circumference getarea(pi,radii[i], area[i]); // call function calculate area i++; i2++; } } // print results table screen cout << "\n=======================================" << "\n| radius | circumference | area |" << "\n=======================================" << endl; (int i=0; i<i2; i++) { cout << fixed << setprecision(2); cout << "| " << setw(6) << radii[i] << " | " << setw(13) << circumf[i] << " | " << setw(10) << area[i] << " |" << endl; cout << "---------------------------------------" << endl; } // call function print results table output file myfile.open("lab20output.txt", ios::out | ios::in); if (!myfile) { cout << "file open error!" << endl; return 0; } cout << "\nwe writing data file..."; writeoutfile(i2,radii,circumf,area,myfile); cout << "done." << endl; // print screen contents of file "lab20output.txt" cout << "\nnow read data file..." << endl; while (getline(myfile, line)) { cout << line << '\n'; } myfile.close(); greeting(); return 0; } // function definitions void getcircumf(const double pi, double radii, double &circumf) { // caluculate circumference of circle circumf = 2 * pi * radii; } void getarea(const double pi, double radii, double &area) { // caluculate area of circle area = pi * (radii * radii); } void writeoutfile(int i2, double radii[], double circumf[], double area[], fstream &myfile) { // print results table myfile myfile << "\n=======================================\n" << "| radius | circumference | area |\n" << "=======================================" << endl; (int i=0; i<i2; i++) { myfile << fixed << setprecision(2); myfile << "| " << setw(6) << radii[i] << " | " << setw(13) << circumf[i] << " | " << setw(10) << area[i] << " |" << endl; myfile << "---------------------------------------" << endl; } } void greeting() { cout << "\n========================" << "\n have nice day! " << "\n========================" << endl; }
i'm going take stab you're trying do:
// call function print results table output file // note: open in out/trunc mode. myfile.open("lab20output.txt", ios::out|ios::trunc); if (!myfile) { cout << "file open error!" << endl; return 0; } cout << "\nwe writing data file..."; writeoutfile(i2,radii,circumf,area,myfile); cout << "done." << endl; myfile.close(); // print screen contents of file "lab20output.txt" cout << "\nnow read data file..." << endl; // note: open in read-mode myfile.open("lab20output.txt", ios::in); while (getline(myfile, line)) { cout << line << '\n'; } myfile.close(); greeting(); return 0;
this truncates output file, writes, closes, opens in read-mode. hard saying sure you're hoping based on question, seems appear want.
output
============================================================== below, may enter of radii (up 100 entries) *** enter 0 (zero) when finished *** ============================================================== enter radius: 10 enter radius: 11 enter radius: 0 ======================================= | radius | circumference | area | ======================================= | 10.00 | 62.80 | 314.00 | --------------------------------------- | 11.00 | 69.08 | 379.94 | --------------------------------------- writing data file...done. read data file... ======================================= | radius | circumference | area | ======================================= | 10.00 | 62.80 | 314.00 | --------------------------------------- | 11.00 | 69.08 | 379.94 | --------------------------------------- ======================== have nice day! ========================
Comments
Post a Comment