Alignment Issues

Hello I am trying to get my end result to be lined up properly, but I'm having a hard time doing and understanding the process.

It should look like this as the output: http://imgur.com/a/WQREu


But I'm getting really weird spacing issues. I know my setw() have strange number it was from me tinkering with the space to try and match them up.

I'm getting this:
-------------------------
Base Pay: $ 100.00
Medical fee: $ 24.00
Weight fee: $ 4.00
-------------------------
Subtotal: $ 128.00
Tax: $ 5.76
-------------------------
Total: $ 133.76

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
 
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <string>
#include <ctime>
#include <cstdlib>
using namespace std;


int main () {

	string name;
	string petname;
	string typeofpet;
	int petweight;
	int max=30;
	int min=20;
	float petweightfee;
	float medicalfee;
	double percentage_tax=4.5;
	float total;
	float salestax;
	float subtotal;
	float basepay=100.00;
	unsigned seed = time(0);
	srand(seed);

	cout << "What is your full name? " << name;
	getline(cin,name);
	cout << "What is your pet type? "<< typeofpet;
	getline(cin,typeofpet);
	cout << "What is your pet's name? " << petname;
	getline(cin,petname);


	cout << "Weight of pet? ";
	cin >> petweight;
	cout << endl;

	medicalfee =  (rand() % max - min + 1) + min;

	petweightfee = petweight * 0.5;
	subtotal = petweightfee + basepay +  medicalfee;
	salestax = subtotal * (percentage_tax / 100);
	total = salestax + subtotal;

	cout << "Congragulations on adopting this critter!" << endl;
	cout << name << ",the cost to adopt your new " << typeofpet << " " << petname << " who is " << petweight << " pounds is as follows: " << endl;
	cout << "-------------------------" << endl;
	cout << setprecision(2)<< fixed;
	cout << "Base Pay:" << setw(9);
	cout <<	" $   " << right << basepay << endl;
	cout << "Medical fee:" << setw(6);
	cout << "$   " << right << medicalfee << endl;
	cout << "Weight fee:" << setw(7);
	cout << "$   " << right << petweightfee << endl;
	cout << "-------------------------" << endl;
	cout << "Subtotal:" << setw(8);
	cout << "$   " << right << subtotal << endl;
	cout << "Tax:" << setw(15);
	cout << "$   " << right << salestax << endl;
	cout << "-------------------------" << endl;
	cout << "Total:" << setw(10);
	cout << "$   " << right << total << endl;


return 0;
}
Last edited on
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
87
#include <iostream>
//#include <cstdlib>
//don't use rand(): http://www.cplusplus.com/forum/beginner/208283/
#include <random>
#include <cmath>
#include <iomanip>
#include <string>
#include <ctime>
#include <limits>
#include <chrono>
//using namespace std;
//http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

const int maX = 30, miN = 20;
//max and min variable names conflict with C++ standard library names;
const double percentage_tax = 4.5, basepay = 100.00;
//const qualify these variables as they cannot be changed
const double wtMult = 0.5;

http://en.cppreference.com/w/cpp/language/using_declaration
using std::cout; using std::cin; using std::endl;
using std::setw; using std::right;


int main () {

	//initialize variables upon declaration
    //and declare variables as close as possible to actual use

	//float - use double throughout, double the precision!

    std::string name{};
    cout << "What is your full name? \n";
	getline(cin, name);

	std::string typeofpet{};
	cout << "What is your pet type? \n";
	getline(cin,typeofpet);

	std::string petname{};
	cout << "What is your pet's name? \n";
	getline(cin,petname);

    int petweight{};//consider making this double as well
    cout << "What is your pet's weight? \n";
	cin >> petweight;
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	//remove '\n' character from input stream

	std::default_random_engine numGenerator(static_cast<unsigned int>(time(nullptr)));
    std::uniform_real_distribution<double> range(miN, maX);
    double medicalfee =  range(numGenerator) + miN;

    http://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout
    cout.setf(std::ios::fixed);
    cout.setf(std::ios::showpoint);
    cout.precision(2);

	double petweightfee = petweight * 0.5;
	double subtotal = petweightfee + basepay +  medicalfee;
	double salestax = subtotal * (percentage_tax / 100);
	double total = salestax + subtotal;

	cout << "Congratulations on adopting this critter!" << endl;
	cout << name << ", the cost to adopt your new " << typeofpet << " " << petname
        << " who is " << petweight << " pounds is as follows: " << endl;
	cout << "-------------------------" << endl;


    //http://www.cplusplus.com/reference/ios/ios_base/width/
	cout.width(15);
    cout << std::left << "Base Pay:"  << " $ " << basepay << endl;
    cout.width(15);
    cout << std::left << "Medical fee:" << " $ " << medicalfee << endl;
    cout.width(15);
    cout << std::left << "Weight fee:" << " $ " << petweightfee << endl;
	cout << "-------------------------" << endl;
	cout.width(15);
	cout << std::left << "Subtotal:" << " $ " << subtotal << endl;
	cout.width(15);
	cout << std::left << "Tax:" << " $ " << salestax << endl;
	cout << "-------------------------" << endl;
	cout.width(15);
	cout << std::left << "Total:" << " $ " << total << endl;

return 0;
}
Topic archived. No new replies allowed.