### 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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207`` ``````#include #include #include #include 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````... //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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206`` ``````#include #include #include #include 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; }``````
 ``123456789101112131415161718`` ``````// 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.