Outputting the number of values in a txt file?

Hello,
For my project I am meant to put out a small table regarding wind speeds, categories, etc, of Hurricanes. I am having trouble adding the number of storms, and the number of them which are Hurricanes in my output and report file. Here is the already created text file:

Marco 65
John 157
Wilfred 159
Iota 60
ahmed 70
Gonzalo 171

And 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#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.
int
main ()
{
  const int MAX_SIZE = 500;	//Declare and initialize variables.
  int k (0), npts, StormNum, HurricaneNum;
  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] > max)
	{
	  max = mph[k];
	}
      ++k;
      fin >> id[k] >> mph[k];
    }				//end while
  npts = k;			//Print hurricane report.
  if (max >= 74)
    {
      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";
    }
  else
    {
      cout << "No hurricanes in the file \n";

      report << "No hurricanes in the file \n";
    }
  for (k = 0; k < npts; ++k)
    {
      if (mph[k] >= 74)
	{
	  if (mph[k] == max)
	    {
	      cout << "\t" << id[k] << "*\t\t" << mph[k] << "\t" <<
		category (mph[k]) << StormSurge << PropertyDamage << endl;

	      report << "\t" << id[k] << "*\t\t" << mph[k] << "\t" <<
		category (mph[k]) << StormSurge << PropertyDamage << endl;
	    }
	  else
	    {
	      cout << "\t" << id[k] << "\t\t" << mph[k] << "\t"
		<< category (mph[k]) << StormSurge << PropertyDamage << endl;

	      report << "\t" << id[k] << "\t\t" << mph[k] << "\t"
		<< category (mph[k]) << StormSurge << PropertyDamage << 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;

}
Last edited on
You appear to have the pieces, you have storm data, an category() function that classifies the storm, so it should be simple.

But you have all this other stuff, the need for which isn't initially obvious. For example, what's special about 74?

The pseudo code could look something like:
1
2
3
4
5
6
7
8
9
10
while (read name, windSpeed)
    nStorms++
    switch (category(windSpeed))
    case 5:
        nHurricanes++
        break;
    end-switch
end-while

print nStorms, nHurricanes
Last edited on
As a first refactor, possibly something like (output needs formatting!) :
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
#include <iostream>
#include <fstream>
#include <string>

double category(double speed);

int main() {
	constexpr size_t MAX_SIZE {500};
	constexpr size_t Hurricane {74};

	std::ifstream fin("MonaeWindData.txt");

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

	std::ofstream report("MonaesReport");

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

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

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

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

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

	if (HurricaneNum > 0) {
		std::cout << "\nStorms that Qualify as Hurricanes \n"
			<< "Identification\t Peak Wind(mph)\t Category\t Storm Surge\t Property Damage Expected\n";
		report << "\nStorms that Qualify as Hurricanes \n"
			<< "Identification\t Peak Wind(mph)\t Category\t Storm Surge\t Property Damage Expected\n";

		for (size_t k {}; k < StormNum; ++k) {
			if (mph[k] >= Hurricane)
				if (mph[k] == max) {
					std::cout << "\t" << id[k] << "*\t\t" << mph[k] << "\t" <<
						category(mph[k]) << StormSurge << PropertyDamage << '\n';
					report << "\t" << id[k] << "*\t\t" << mph[k] << "\t" <<
						category(mph[k]) << StormSurge << PropertyDamage << '\n';
				} else {
					std::cout << "\t" << id[k] << "\t\t" << mph[k] << "\t"
						<< category(mph[k]) << StormSurge << PropertyDamage << '\n';
					report << "\t" << id[k] << "\t\t" << mph[k] << "\t"
						<< category(mph[k]) << StormSurge << PropertyDamage << '\n';
				}
		}
	} else {
		std::cout << "No hurricanes in the file \n";
		report << "No hurricanes in the file \n";
	}
}

double 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;
}

Registered users can post here. Sign in or register to post.