Question: C++ - I am having a problem printing the column headers and the commas to the file. It works fine on my screen, but once
C++ - I am having a problem printing the column headers and the commas to the file. It works fine on my screen, but once I open the file I have the program pointed to the column headers and commas are missing. I have attached my program, the print screen, and the file.
Print out a table of powers. Use a for loop that goes from 101 to 112 inclusive. Print out r, r squared, r cubed, square root of r, and cube root of r. Use two decimal places for the roots, and zero decimal places for the squares and cubes, and 2 decimal places for the averages. Use commas in numbers 1000 and above. After the loop, print out the average of the r squared and r cubed. Use columns for your data. Print column headings above your loop.
Also print the data to a file. Attach the file to your submission. Use a txt extension for your file so I can open it in notepad.
So, you will have output to the screen as usual, and also to the file. You can attach as many files as you need to an assignment, just don't click Submit until you have all the files attached.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/* * File: main.cpp * Author: AA * * Created on September 16, 2018, 1:16 PM */
#include
using namespace std; using std::ofstream; struct comma : public std::numpunct
{
explicit comma(size_t refs = 0) : std::numpunct
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\003"; }
};
/* * */ int main(int argc, char** argv) { ofstream outfile;
double number = 0.0;
double number_sqr = 0.0;
double number_cube = 0.0;
double number_sqr_rt = 0.0;
double number_cube_rt = 0.0;
double sqr_average = 0.0;
double cube_average = 0.0;
double sqr_sum = 0.0;
double cube_sum = 0.0;
cout << setw(0) << "Number" << setw(12) << "Squared" << setw(12) << "Cubed" << setw(20) << "Sqare Root" << setw(21) << "Cubed Root" << endl;
outfile << setw(0) << "Number" << setw(12) << "Squared" << setw(12) << "Cubed" << setw(20) << "Sqare Root" << setw(21) << "Cubed Root" << " ";
cout << fixed << setprecision(2);
outfile << fixed << setprecision(2);
locale global;
locale withgroupings(global, new comma);
locale was = cout.imbue(withgroupings);
outfile.open("Homework");
for(number = 101; number <= 112; number++)
{
number_sqr = pow(number, 2);
number_cube = pow(number, 3);
number_sqr_rt = sqrt(number);
number_cube_rt = cbrt(number);
sqr_sum = sqr_sum + number_sqr;
sqr_average = sqr_sum / 12;
cube_sum = cube_sum + number_cube;
cube_average = cube_sum / 12;
std::cout << setw(0) << setprecision(0) << number << setw(14) << setprecision(0) << number_sqr << setw(17) < outfile << setw(0) << setprecision(0) << number << setw(14) << setprecision(0) << number_sqr << setw(17) < cout << "The average of the Squares are: " << sqr_average << endl; cout << "The average of the Cubes are: " << cube_average << endl; outfile << "The average of the Squares are: " << sqr_average << " "; outfile << "The average of the Cubes are: " << cube_average << " "; return 0; } Screen Output Number Squared Cubed Sqare Root Cubed Root 101 10,201 1,030,301 10.05 4.66 102 10,404 1,061,208 10.10 4.67 103 10,609 1,092,727 10.15 4.69 104 10,816 1,124,864 10.20 4.70 105 11,025 1,157,625 10.25 4.72 106 11,236 1,191,016 10.30 4.73 107 11,449 1,225,043 10.34 4.75 108 11,664 1,259,712 10.39 4.76 109 11,881 1,295,029 10.44 4.78 110 12,100 1,331,000 10.49 4.79 111 12,321 1,367,631 10.54 4.81 112 12,544 1,404,928 10.58 4.82 The average of the Squares are: 11,354.17 The average of the Cubes are: 1,211,757.00 RUN SUCCESSFUL (total time: 160ms) File Output 101 10201 1030301 10.05 4.66 102 10404 1061208 10.10 4.67 103 10609 1092727 10.15 4.69 104 10816 1124864 10.20 4.70 105 11025 1157625 10.25 4.72 106 11236 1191016 10.30 4.73 107 11449 1225043 10.34 4.75 108 11664 1259712 10.39 4.76 109 11881 1295029 10.44 4.78 110 12100 1331000 10.49 4.79 111 12321 1367631 10.54 4.81 112 12544 1404928 10.58 4.82 The average of the Squares are: 11354.17 The average of the Cubes are: 1211757.00
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
