need help with assignment please

doing an assignment where a program reads rainfall values for 12 months
the program must calculate the total, average, least and greatest amounts.
. I've completed the majority of the program but I need to display the name of the month spelled out next to both the least and greatest amounts

ie the least amount is 2.8 inches in the month of ( month with least amount )

I tried passing a string from a function but i receive a error message so i assume that i cannot return a string from a function.

we cannot use pointers yet. any help is greatly appreciated

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int SIZE = 12;	//array size

//function prototypes
double totalRain(double[], const int );
double largestRain(double[], const int);
double smallestRain(double[], const int);
void showData(double[], const int, double , double);
char largestMonth(double[], const int, double);
char smallestMonth(double[], const int, double);

int main()
{
	
	double rainfall[SIZE],		//array with 12 elements
		   total,				//total rain variable
		   average,				//average rain variable
		   largest,				//largest amount of rainfall	
		   smallest,			//smallest amount of rainfall
		   Lrain,		
		   Srain;			
	
	char response,				

		 file[20];
	int count;					//loop counter variable
	ifstream inputFile;			//inputFile stream object
	
	

	//numeric output formatting
	cout << fixed << showpoint << setprecision(2);

	do
	{

		// user must enter the file name with rainfall data
		string file;
		cout << "\nenter the filename for rainfall data (or enter -1 to quit): \n";
		getline(cin, file);		

		//open rainfall file
		inputFile.open(file);
		
		count = 0;
		//read numbers from file into an array
		while( count < SIZE && inputFile >> rainfall[count])
			count++;

		//close file
		inputFile.close();

		//display rainfall total
		total = totalRain( rainfall, SIZE );
		cout << " \n total: "<< total << " inches.\n";

		//display rainfall averages
		average = total / SIZE; 
		cout << " average: "<< average << " inches.\n";

		//display largest rainfall amount
		largest = largestRain( rainfall, SIZE );
		Lrain = largestMonth( rainfall, SIZE, largest );
		cout << " largest: "<< largest << " inches in " << Lrain <<" \n";
	
		//display smallest rainfall amount
		smallest = smallestRain( rainfall, SIZE );
		Srain = smallestMonth( rainfall, SIZE , smallest );
		cout << " smallest: "<< smallest << " inches in " << Srain<<" \n";

		//display month and rainfall data
		showData( rainfall, SIZE, smallest,  largest);

		fflush(stdin);
		//ask user if they wish to continue
		cout << "\nrun again (y/n)?\n";
		cin >> response;
		fflush(stdin);

	}while( response == 'y' || response == 'Y' );

	if( response == 'n' || response == 'N' )
		cout << "programmer: ynzon \n";

	system( "pause" );

	return 0;
}

//total rinfall function

double totalRain(double nums[], const int SIZE )
{
	double sum = 0;
		
	for( int count = 0; count < SIZE; count++)
			sum += nums[count];
	
	return sum;
}

//highest rainfall amount function

double largestRain( double nums[], const int water )
{
	
	double highest;
	int count;

	highest = nums[0];
	for( count = 1; count < SIZE; count++ )
	{
		if( nums[count] > highest )
			highest = nums[count];
	}

	return highest;
}

//lowest rainfall amount function

double smallestRain( double nums[], const int water )
{
	double smallest;
	int count;

	smallest = nums[0];
	for( count = 1; count < SIZE; count++ )
	{
		if( nums[count] < smallest )
			smallest = nums[count];
	}

	return smallest;
}

// month and rainfall data function

void showData( double nums[], const int SIZE, double S, double L )
{

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };


	cout << "\n month" << "\t" << " rainfall\n";
	cout << "-------"<< "\t" << " --------\n";
	for ( int count = 0; count < SIZE; count++ )
	{
		if (nums[count] == S)
			cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << " (smallest)"<< endl;
		
		else if (nums[count] == L)
			cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << " (largest)"<< endl;
		
		else
			cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << endl;
		
	}

}

// display largest month spelled out

char largestMonth(double nums[], const int SIZE , double LM )
{
	double Lmonths;

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };

	for ( int count = 0; count < SIZE; count++ )
	{
		if( nums[count] == LM )
			LM = months[count];
	
	}
	return Lmonths;
}


// display smallest month spelled out

char smallestMonth(double nums[], const int SIZE , double SM )
{
	double Smonths;

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };

	for ( int count = 0; count < SIZE; count++ )
	{
		if( nums[count]== SM )
			SM = months[count];
	
	return Smonths;
	}
	
}
closed account (Dy7SLyTq)
can you use std::map?
I dont think so it hasnt been covered
Any other suggestions anyone, im stuck and this assignment is due today
You can return a string from a function.

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
...
//function prototypes
...
string smallestMonth(double[], const int, double); //returns string
...

int main()
{
	
	double rainfall[SIZE],		//array with 12 elements
		   total,				//total rain variable
		   average,				//average rain variable
		   largest,				//largest amount of rainfall	
		   smallest;			//smallest amount of rainfall
	string   Lrain,		// changed to string
                   Srain;
...



...
// display smallest month spelled out

string smallestMonth(double nums[], const int SIZE , double SM ) //returns string
{
	string Smonths; // changed to string

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };

	for ( int count = 0; count < SIZE; count++ )
	{
		if( nums[count]== SM )
			Smonths = months[count]; //changed to Smonths
	}
	return Smonths; // moved outside for loop
	
}
Last edited on
thanks vin, i ran the compiler but i received a error message that says

debug assertion failed

expression: string out of range
closed account (Dy7SLyTq)
that means that you stepped through a string at one plus the last character
how do I fix this? should i increase the number in file on line 30? or does it have to do with the data type im using
closed account (Dy7SLyTq)
yes you could, but it would be easier to just use std::string
could you please show me where, the book nor the professor discussed this
closed account (Dy7SLyTq)
oh then you should probably not use it then. yes just increase the number
Which string?
closed account (Dy7SLyTq)
file @ line 30
line: 1441 ?
I increased the number on line 30, cleaned solution then built it but still receiving the same error message
What filename are you typing in?

You have file defined as char[20] on line 30.

You have file defined as string on line 43.

For getline you need a string.

For inputFile.open you need a c string.

So maybe get rid of line 30 and use inputFile.open(file.c_str())
we are given a two sets of 12 numbers and are to save them as a .txt file and store them in the project folder.

the program is supposed to prompt the user to enter the filename and the program runs and computes the numbers

heres what i did vin but i recieved another error message

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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;

const int SIZE = 12;	//array size

//function prototypes
double totalRain(double[], const int );
double largestRain(double[], const int);
double smallestRain(double[], const int);
void showData(double[], const int, double , double);
string largestMonth(double[], const int, double);
string smallestMonth(double[], const int, double);

int main()
{
	
	double rainfall[SIZE],		//array with 12 elements
		   total,				//total rain variable
		   average,				//average rain variable
		   largest,				//largest amount of rainfall	
		   smallest;			//smallest amount of rainfall
	string Lrain,		
		   Srain;			
	
	char response;				

	string file[30];
	int count;					//loop counter variable
	ifstream inputFile;			//inputFile stream object
	inputFile.open(file.str());
	

	//numeric output formatting
	cout << fixed << showpoint << setprecision(1);

	do
	{

		// user must enter the file name with rainfall data
		string file;
		cout << "\nenter the filename for rainfall data (or enter -1 to quit): \n";
		getline(cin, file);		

		//open rainfall file
		inputFile.open(file);
		
		count = 0;
		//read numbers from file into an array
		while( count < SIZE && inputFile >> rainfall[count])
			count++;

		//close file
		inputFile.close();

		//display rainfall total
		total = totalRain( rainfall, SIZE );
		cout << " \n total: "<< total << " inches.\n";

		//display rainfall averages
		average = total / SIZE; 
		cout << " average: "<< average << " inches.\n";

		//display largest rainfall amount
		largest = largestRain( rainfall, SIZE );
		Lrain = largestMonth( rainfall, SIZE, largest );
		cout << " largest: "<< largest << " inches in " << Lrain <<" \n";
	
		//display smallest rainfall amount
		smallest = smallestRain( rainfall, SIZE );
		Srain = smallestMonth( rainfall, SIZE , smallest );
		cout << " smallest: "<< smallest << " inches in " << Srain<<" \n";

		//display month and rainfall data
		showData( rainfall, SIZE, smallest,  largest);

		fflush(stdin);
		//ask user if they wish to continue
		cout << "\nrun again (y/n)?\n";
		cin >> response;
		fflush(stdin);

	}while( response == 'y' || response == 'Y' );

	if( response == 'n' || response == 'N' )
		cout << "programmer: alex martinez and vesela monov \n";

	system( "pause" );

	return 0;
}

//total rinfall function

double totalRain(double nums[], const int SIZE )
{
	double sum = 0;
		
	for( int count = 0; count < SIZE; count++)
			sum += nums[count];
	
	return sum;
}

//highest rainfall amount function

double largestRain( double nums[], const int water )
{
	
	double highest;
	int count;

	highest = nums[0];
	for( count = 1; count < SIZE; count++ )
	{
		if( nums[count] > highest )
			highest = nums[count];
	}

	return highest;
}

//lowest rainfall amount function

double smallestRain( double nums[], const int water )
{
	double smallest;
	int count;

	smallest = nums[0];
	for( count = 1; count < SIZE; count++ )
	{
		if( nums[count] < smallest )
			smallest = nums[count];
	}

	return smallest;
}

// month and rainfall data function

void showData( double nums[], const int SIZE, double S, double L )
{

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };


	cout << "\n month" << "\t" << " rainfall\n";
	cout << "-------"<< "\t" << " --------\n";
	for ( int count = 0; count < SIZE; count++ )
	{
		if (nums[count] == S)
			cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << " (smallest)"<< endl;
		
		else if (nums[count] == L)
			cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << " (largest)"<< endl;
		
		else
			cout << setw(10) << left << months[count] << setw(10) << right << nums[count] << endl;
		
	}

}

// display largest month spelled out

string largestMonth(double nums[], const int SIZE , double LM )
{
	string largem;

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };

	for ( int count = 0; count < SIZE; count++ )
	{
		if( nums[count] == LM )
			LM = largem[count];
	
	}
	return largem;
}


// display smallest month spelled out

string smallestMonth(double nums[], const int SIZE , double SM )
{
	string smallm;

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };

	for ( int count = 0; count < SIZE; count++ )
	{
		if( nums[count]== SM )
			smallm = months[count];
	}
	
	return smallm;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// display largest month spelled out

string largestMonth(double nums[], const int SIZE , double LM )
{
	string largem;

	string months[12] = { " january" , " february" , " march" , " april" , 
							" may" , " june" , " july" , " august" , " september" 
							, " october" , " november" , " december" };

	for ( int count = 0; count < SIZE; count++ )
	{
		if( nums[count] == LM )
			LM = largem[count]; // this is wrong!    largem = months[count]
	
	}
	return largem;
}
Topic archived. No new replies allowed.