I really need help formating output to a text file.

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.

Here's my code
1
2
3
4
5
6
7
8
9
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//Mason Wilson
//Project #5
//Start Date: November 17th
//Due Date: November 20th

#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace 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(const int n) {
	if (n <= 1) return 1;
	fact = n * factorial(n - 1);
	return fact;
}

//Power function using recursion...
double power(const double x, const int 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(const double 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(const double 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
Last edited on
Alas, correctly formatting floating point numbers is not so simple as throwing a few setfill() and setw()'s in there.

Here's a crazy-looking function that can help:

 
#include <sstream> 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//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, const double 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):

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	//Draw the Heading
	fout	<< left
		<< setw(10) << "x"		<< " "
		<< setw(10) << "sin(x)"		<< " "
		<< setw(10) << "my_sin(x)"	<< " "
		<< setw(10) << "e(x)"		<< " "
		<< setw(10) << "my_el(x)"	<< " "
		<< setw(10) << "my_exp(x)"	<< endl;
	
	//Loop to display and increase x by the incrememnt
	for (x = lowerBound; x <= upperBound; x = x + increment) {
		fout	<< format(10,x)		<< " "
			<< format(10,sin(x))	<< " "
			<< format(10,my_sin(x))	<< " "
			<< format(10,exp(x))	<< " "
			<< format(10,0)		<< " "  // fixme: what is my_el()?
			<< format(10,my_exp(x))	<< endl;
	}
	return 0;
}

A couple of runs:

C:\Users\Michael\Programming\cpp\random\cc>a
Enter lower and upper bounds: 0 1
Enter Increment: .1

C:\Users\Michael\Programming\cpp\random\cc>type output.text
x          sin(x)     my_sin(x)  e(x)       my_el(x)   my_exp(x)
 0.0000000  0.0000000  0.0000000  1.0000000  0.0000000  1.0000000
 0.1000000  0.0998334  0.0998334  1.1051709  0.0000000  1.1051709
 0.2000000  0.1986693  0.1986693  1.2214028  0.0000000  1.2214028
 0.3000000  0.2955202  0.2955202  1.3498588  0.0000000  1.3498588
 0.4000000  0.3894183  0.3894183  1.4918247  0.0000000  1.4918247
 0.5000000  0.4794255  0.4794255  1.6487213  0.0000000  1.6487213
 0.6000000  0.5646425  0.5646425  1.8221188  0.0000000  1.8221188
 0.7000000  0.6442177  0.6442177  2.0137527  0.0000000  2.0137527
 0.8000000  0.7173561  0.7173561  2.2255409  0.0000000  2.2255409 
 0.9000000  0.7833269  0.7833269  2.4596031  0.0000000  2.4596031
 1.0000000  0.8414710  0.8414710  2.7182818  0.0000000  2.7182818

C:\Users\Michael\Programming\cpp\random\cc>a
Enter lower and upper bounds: 0 10
Enter Increment: 1

C:\Users\Michael\Programming\cpp\random\cc>type output.text
x          sin(x)     my_sin(x)  e(x)       my_el(x)   my_exp(x)
 0.0000000  0.0000000  0.0000000  1.0000000  0.0000000  1.0000000
 1.0000000  0.8414710  0.8414710  2.7182818  0.0000000  2.7182818
 2.0000000  0.9092974  0.9092974  7.3890561  0.0000000  7.3890561
 3.0000000  0.1411200  0.1411200  20.085537  0.0000000  20.085537
 4.0000000 -0.7568025 -0.7568025  54.598150  0.0000000  54.598150
 5.0000000 -0.9589243 -0.9589243  148.41316  0.0000000  148.41316
 6.0000000 -0.2794155 -0.2794155  403.42879  0.0000000  403.42879
 7.0000000  0.6569866  0.6569866  1096.6332  0.0000000  1096.6332
 8.0000000  0.9893582  0.9893582  2980.9580  0.0000000  2980.9580
 9.0000000  0.4121185  0.4121185  8103.0839  0.0000000  8103.0839
 10.000000 -0.5440211 -0.5440211  22026.466  0.0000000  22026.466

C:\Users\Michael\Programming\cpp\random\cc>a
Enter lower and upper bounds: 0 100
Enter Increment: 10

C:\Users\Michael\Programming\cpp\random\cc>type output.text
x          sin(x)     my_sin(x)  e(x)       my_el(x)   my_exp(x)
 0.0000000  0.0000000  0.0000000  1.0000000  0.0000000  1.0000000
 10.000000 -0.5440211 -0.5440211  22026.466  0.0000000  22026.466
 20.000000  0.9129453  0.9129453     5e+008  0.0000000     5e+008
 30.000000 -0.9880316 -0.9878137     1e+013  0.0000000     1e+013
 40.000000  0.7451132 -58.522234     2e+017  0.0000000     2e+017
 50.000000 -0.2623749    -3e+011     5e+021  0.0000000     2e+021
 60.000000 -0.3048106    -3e+019     1e+026  0.0000000     1e+025
 70.000000  0.7738907    -2e+026     3e+030  0.0000000     1e+028
 80.000000 -0.9938887    -1e+032     6e+034  0.0000000     7e+030
 90.000000  0.8939967    -1e+037     1e+039  0.0000000     2e+033
 100.00000 -0.5063656    -5e+041     3e+043  0.0000000     3e+035

C:\Users\Michael\Programming\cpp\random\cc>


I don't know if this helps or not.
Topic archived. No new replies allowed.