c++ - Setprecision and file -


i have 2 questions:
1. how make if file isn't entered, wont proceed?
2. numbers wont show decimal points , i'm not understanding why, used setprecision

    #include <iostream>     #include <fstream>     #include <string>     #include <iomanip>      using namespace std;     int main()     {         ifstream infile;         ofstream outfile;         string file;         int bailey ,              harrison,              grant,              peterson,              hsu,              bowles,              anderson,              nguyen,              sharp,              jones,              mcmillan,              gabriel;         int m, f , cc, un;           cout << "                  welcome maggie's student survey!"; //introduction        cout << endl;        cout << " enter file name wish open: " <<endl;// promts  user enter file name want open         getline(cin, file);         infile.open(file);         outfile.open(file);         cout << " student's genders, colleges, , scores:" << endl;         cout << "\n bailey           m cc 68"             "\n harrison         f cc 71"             "\n grant            m un 75"             "\n peterson         f un 69"             "\n hsu              m un 79"             "\n bowles           m cc 75"             "\n anderson         f un 64"             "\n nguyen           f cc 68"             "\n sharp            f cc 75"             "\n jones            m un 75"             "\n mcmillan         f un 80"             "\n gabriel          f un 62 " << endl; //the data file       user can see opened         m = (75 + 79 + 75 + 75) / 4; // average 76         f = (71 + 69 + 64 + 68 + 75 + 80 + 62) / 7; //average 69.85         cc = (71 + 75 + 68 + 75) / 4; //average 72.25         un = (75 + 69 + 79 + 64 + 75 + 80 + 62) / 7; // average 72         cout << " total males average scores: " << std::setprecision(2) << m  << endl;         cout << " total females average scores: " << std::setprecision(2) << f << endl;         cout << " total community college average scores " << std::setprecision(2) << cc << endl;         cout << " total university average scores: " << std::setprecision(2) << un    << endl;          system("pause"); 

regarding second question, came solution first need declare m, f, cc, un doubles, not ints. try storing other numbers other variables. example cc:

 #include <iostream>  #include <iomanip>    int main() {         double a, b, c, d; // or ints, here doesn't matter     = 71;     b = 75;     c = 68;     d = 75;     double cc = (a + b + c + d) / 4; //average 72.25     std::cout << " total community college average scores " << std::setprecision(4) << cc << '\n';     system("pause"); } 

if this, precision works. set 4 in order display number, 2 not enough.

other solution (maybe easier):

 #include <iostream>  #include <iomanip>    int main() {        double p;     p = 71 + 75 + 68 + 75;     double cc = p / 4; //average 72.25     std::cout << " total community college average scores " << std::setprecision(4) << cc << '\n';     system("pause"); } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -