Making A Presentable Table?

Hello,
I have a program below that it supposed to output data for storms analyzed in a data file. However, the table looks a bit odd and is kind of difficult to read. Any help on evening the spaces?
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include<iostream>		//Required for cin, cout, cerr. // PROGRAM CHAP 7_5
#include<fstream>		//Required for fin. // This program reads storm values from a data file
#include <string>		//Required for string.
using namespace std;
double category (double speed);	//Function Prototypes.
string StormSurgeFeet (double speed);
string PropertyDamageLevel (double speed);
int
main ()
{
  const int MAX_SIZE = 500;	//Declare and initialize variables.
  int k (0), npts, StormNum (0), HurricaneNum (0);
  double mph[MAX_SIZE], max (0);
  string filename, id[MAX_SIZE], StormSurge, PropertyDamage;
  ifstream fin ("MonaeWindData.txt");
  ofstream report;
  if (fin.fail ())
    {
      cerr << "Could not open file MonaeWindData.txt" << endl;
      exit (1);
    }
  report.open ("MonaesReport");
  fin >> id[k] >> mph[k];	//Read data and determine maximum mph.

  while (!fin.fail ())
    {				// { PUT HERE TO GET FULL CODE ON ONE SLIDE
      if (mph[k] > 74)
	{
	  HurricaneNum++;
	}
      ++k;
      StormNum++;
      fin >> id[k] >> mph[k];
    }				//end while
  npts = k;			//Print hurricane report.


  cout << "The total number of storms recorded is:" << StormNum << endl;
  report << "The total number of storms recorded is:" << StormNum << endl;

  cout << "The total number of Hurricanes recorded is:" << HurricaneNum <<
    endl;
  report << "The total number of storms recorded is:" << HurricaneNum << endl;

  cout << "Storms that Qualify as Hurricanes \n"
    <<
    "Identification\t Peak Wind(mph)\t Category\t Storm Surge\t Property Damage Expected\n";

  report << "Storms that Qualify as Hurricanes \n"
    <<
    "Identification\t Peak Wind(mph)\t Category\t Storm Surge\t Property Damage Expected\n";

  for (k = 0; k < npts; ++k)
    {
      if (mph[k] >= 74)
	{
	  report << "\t" << id[k] << "\t\t" << mph[k] << "\t"
	    << category (mph[k]) << StormSurge << PropertyDamage << endl;
	  cout << "\t" << id[k] << "\t\t" << mph[k] << "\t" << category (mph[k]) << "\t" << StormSurgeFeet(mph[k]) << "\t "<< PropertyDamageLevel(mph[k]) << "\t" << endl;
	  
	}			//end if k
    }				//end for

  fin.close ();
  report.close ();
  return 0;
}				// end main

double
category (double speed)		//This function determines the hurricane intensity category
{
  int intensity (1);		//Declare variables.
  if (speed >= 155)		//Determine category.
    {
      intensity = 5;
    }
  else if (speed >= 131)
    {
      intensity = 4;		// { intensity=4; put on this line for fitting code on slide
    }
  else if (speed >= 111)
    {
      intensity = 3;
    }
  else if (speed >= 96)
    {
      intensity = 2;
    }

  return intensity;

}
 string StormSurgeFeet (double speed)		//This function determines the hurricane intensity category
{
 string intensity ("4-5");		//Declare variables.
  if (speed >= 155)		//Determine category.
    {
      intensity = "18+";
    }
  else if (speed >= 131)
    {
      intensity = "13-18";		// { intensity=4; put on this line for fitting code on slide
    }
  else if (speed >= 111)
    {
      intensity = "9-12";
    }
  else if (speed >= 96)
    {
      intensity = "6-8";
    }

  return intensity;

}
 string PropertyDamageLevel (double speed)		//This function determines the hurricane intensity category
{
 string intensity ("Minimal");		//Declare variables.
  if (speed >= 155)		//Determine category.
    {
      intensity = "Catastrophic";
    }
  else if (speed >= 131)
    {
      intensity = "Extreme";		// { intensity=4; put on this line for fitting code on slide
    }
  else if (speed >= 111)
    {
      intensity = "Extensive";
    }
  else if (speed >= 96)
    {
      intensity = "Moderate";
    }

  return intensity;
  
  /*OUTPUT
*/
}
Last edited on
Read up on std::setw and use it instead of '\t'.
What compiler are you using? If VS2022 you can use C++20 std::format

https://en.cppreference.com/w/cpp/utility/format/format

PS. Some provided test data would be helpful as well.
Last edited on
Using iomanip (for setw), possibly:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using std::cout;
using std::cerr;
using std::setw;

constexpr size_t MAX_SIZE { 500 };
constexpr double Hurricane { 74.0 };

unsigned category(double speed);
std::string StormSurgeFeet(double speed);
std::string PropertyDamageLevel(double speed);

void header(std::ostream& os) {
	os << "\nStorms that Qualify as Hurricanes \n" << setw(18) << std::left <<
		"Identification" << setw(16) << "Peak Wind(mph)" << setw(12) << "Category" << setw(15) << "Storm Surge" <<
		setw(25) << "Property Damage Expected\n";
}

void detail(std::ostream& os, std::string id, double mph) {
	 os << setw(18) << std::left << id << setw(16) << mph << setw(12) << category(mph) << setw(15) << StormSurgeFeet(mph) << setw(25) << PropertyDamageLevel(mph) << '\n';
}

int main() {
	std::ifstream fin("MonaeWindData.txt");

	if (!fin)
		return (cerr << "Could not open file MonaeWindData.txt\n"), 1;

	std::ofstream report("MonaesReport");

	if (!report)
		return (cerr << "Could not open report file\n"), 2;

	size_t StormNum {}, HurricaneNum {};
	double mph[MAX_SIZE] {};
	std::string id[MAX_SIZE];

	for (; StormNum < MAX_SIZE && (fin >> id[StormNum] >> mph[StormNum]); ++StormNum)
		HurricaneNum += mph[StormNum] >= Hurricane;

	cout << "The total number of storms recorded is: " << StormNum << '\n';
	report << "The total number of storms recorded is: " << StormNum << '\n';

	cout << "The total number of Hurricanes recorded is: " << HurricaneNum << '\n';
	report << "The total number of Hurricanes recorded is: " << HurricaneNum << '\n';

	header(cout);
	header(report);

	for (size_t k {}; k < StormNum; ++k)
		if (mph[k] >= 74) {
			detail(cout, id[k], mph[k]);
			detail(report, id[k], mph[k]);
		}
}

unsigned category(double speed) {
	if (speed >= 155)
		return 5;

	if (speed >= 131)
		return 4;

	if (speed >= 111)
		return 3;

	if (speed >= 96)
		return 2;

	return 1;
}

std::string StormSurgeFeet(double speed) {
	if (speed >= 155)
		return "18+";

	if (speed >= 131)
		return "13-18";

	if (speed >= 111)
		return "9-12";

	if (speed >= 96)
		return "6-8";

	return "4-5";
}

std::string PropertyDamageLevel(double speed) {
	if (speed >= 155)
		return "Catastrophic";

	if (speed >= 131)
		return "Extreme";

	if (speed >= 111)
		return "Extensive";

	if (speed >= 96)
		return "Moderate";

	return "Minimal";
}

Last edited on
What compiler are you using? If VS2022 you can use C++20 std::format

It works with VS2019 already, but I doubt that std::format will be allowed for homework.
Topic archived. No new replies allowed.