Everything here works like I want it to, it outputs to the text file just like I want it to but my teacher needs it to be organized in columns. I asked how and he literally just told me "use setfill and setw". Great, IDK how to do that and I can't figure it out online.
//Mason Wilson
//Project #5
//Start Date: November 17th
//Due Date: November 20th
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
usingnamespace std;
double f = 3.14159;
double upperBound = 0;
double lowerBound = 0;
double increment = 0;
double tempVal = 0;
double powVal = 0;
double expVal = 0;
double fact = 0;
double sinVal = 0;
double x = 0.0;
int n = 0;
int i = 0;
//Factorial function using recursion...
double factorial(constint n) {
if (n <= 1) return 1;
fact = n * factorial(n - 1);
return fact;
}
//Power function using recursion...
double power(constdouble x, constint n) {
if (n <= 0)
return 1;
powVal = x * power(x, n - 1);
return powVal;
}
//my_sin function using power and factorial functions
double my_sin(constdouble x) {
sinVal = 0;
for (int k = 0; k < 50; k++) {
sinVal += power(-1, k) * (power(x, 2 * k + 1) / factorial(2 * k + 1));
}
return sinVal;
}
//my_exp(x) Function
double my_exp(constdouble x) {
expVal = 0;
for (int k = 0; k < 50; k++) {
expVal += power(x, k) / factorial(k);
}
return expVal;
}
int main() {
ofstream fout("output.text");
cout << "Enter lower and upper bounds: ";
cin >> upperBound >> lowerBound;
cout << "Enter Increment: ";
cin >> increment;
//Checking if upper and lower bounds are in the right order...
if (upperBound < lowerBound) {
tempVal = upperBound;
upperBound = lowerBound;
lowerBound = tempVal;
}
fout << "x sin(x) my_sin(x) e(x) my_el(x) my_exp(x)" << endl;
//Loop to display and increase x by the incrememnt
for (x = lowerBound; x <= upperBound; x = x + increment) {
fout << setfill('0') << setw(8);
fout << setprecision(7) << x << " "
<< my_sin(x) << " "
<< sin(x) << " "
<< exp(x) << " "
<< my_exp(x) << endl;
}
return 0;
}
What I'm trying to do is make every number go to 7 decimal points, and then each output take 8 spaces so that it leaves a space between each one. So like output:
7.05 8.05
will become:
7.05000 8.05000
//Format a float to an exact width field.
//Sign is indicated as a blank or a minus.
//If necessary, it reverts to right-justified scientific notation.
string format(unsigned width, constdouble x) {
ostringstream ss;
// Output the sign field
if (x<0) ss << "-";
else ss << " ";
// Calculate the number of whole digits
long iwidth = (int)x ? (log10((int)abs(x))+1) : 1;
// Output a fixed value using only as many fractional
// digits as necessary to fill out the field.
// (minus two for the sign and the decimal point)
ss << fixed << setprecision( width - iwidth - 2 ) << abs(x);
// Perfect!
if (ss.str().size() <= width) return ss.str();
// Crud. Must use scientific notation:
ostringstream ss2;
ss2 << scientific << setw(10) << setprecision(0) << x;
return ss2.str();
}
And your main() updated to use the new formatting (and fixing the order of a couple of things to match your table heading):