How do you adjust the setw of these arrays?

Right now the outfile looks like this:

YEARLY MONTHLY RAINFALL
Month Rainfall in inches
January ///////3.8
February ///////5.2
March///// 4.7
April/////6.3
May///3.9
June ////0.9
July////0.9
August//////2.2
September///////// 3.4
October ///////2.0
November//////// 2.0
December ////////3.7



I want it to look like this


YEARLY MONTHLY RAINFALL
Month Rainfall in inches
January/////////3.8
February////////5.2
March///////////4.7
Apri/////////////6.3
May/////////////3.9
June////////////0.9
July/////////////0.9
August//////////2.2
September//////3.4
October/////////2.0
November//////2.0
December//////3.7


This is what I have in the compiler and I'm not supposed to add anything else. (So no vectors or anything else)

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;



Here's the rest of my code:


int readRainFall (ifstream& inFileName, double rainfalls[], int MONTHS);
double getrainTotal(double[], int);
double gethighestRainfall(double rainfalls[], int MONTHS);
double getlowestRainfall(double rainfalls[], int MONTHS);


int main() {

//get inputs
const int MONTHS = 12;

string monthName[MONTHS] = { "January","February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
double rainfallAmount[MONTHS];
int count=0;
double avg;
double totalRain, mostRain, leastRain;
string name, mostMonth, leastMonth, inFileName, outFileName;



//Get the the name of input and output files

cout << "Please enter the name of output file: ";
ofstream outFile;
cin>>outFileName;
outFile.open(outFileName);
outFile << fixed << showpoint << setprecision(1);


ifstream inFile(inFileName);
readRainFall (inFile, rainfallAmount, MONTHS);

cout << "Programmer: ";
cin>>name;
cout << "Processing complete"<<endl;



//Display tittle
outFile << "YEARLY MONTHLY RAINFALL" << endl;
outFile << "Month"<<setw(27)<<"Rainfall in inches"<<endl;


for (int count = 0; count < MONTHS; count++) {
outFile << monthName[count]<<setw(17)<<rainfallAmount[count]<<endl;

}
// Call total
totalRain = getrainTotal (rainfallAmount, MONTHS);
outFile << "The total rainfall for the year is "<<totalRain<<" inches."<<endl;
//Call average
avg = totalRain/MONTHS;
outFile << "The average monthly rainfall is "<<avg<<" inches."<<endl;

//Call highest rainfall
mostRain = gethighestRainfall(rainfallAmount, MONTHS);
outFile << "The highest rainfall of "<<mostRain<<" occurred in April"<<endl;
//Call lowest rainfall
leastRain = getlowestRainfall (rainfallAmount, MONTHS);
outFile << "The lowest rainfall of "<<leastRain<<" occurred in June"<<endl;
outFile << "The lowest rainfall of "<<leastRain<<" occurred in July"<<endl;
outFile<<" "<<endl;
//Get months with at least 4 inches
outFile <<"Rainfall of 4 inches or more occurred in February."<<endl;
outFile <<" "<<endl;
outFile<<"Rainfall of 4 inches or more occurred in March."<<endl;
outFile << " "<<endl;
outFile << "Rainfall of 4 or more inches occurred in April."<<endl;


outFile<<"Programmer: ";
outFile<<name;



//close the outfile
outFile.close();



return 0;


}

//define input
int readRainFall (ifstream& inFile, double rainfalls[], int MONTHS)
{
string inFileName;
int count=0;
cout << "Please enter the name of the input file: ";
cin>>inFileName;
inFile.open(inFileName);
if (!inFile.is_open())
{
cout << "Error opening file"<<endl;
}
else {
while(count<MONTHS && inFile >> rainfalls[count])
count++;
}
inFile.close();
return 0;


}

//define total
double getrainTotal (double rainfalls[], int MONTHS)

{
double total=0;
for ( int count = 0; count < MONTHS; count++)
total += rainfalls[count];
return total;
}
//define the highest rainfall
double gethighestRainfall(double rainfalls[], int MONTHS)
{
double highest;
highest = rainfalls[0];
for (int count=1; count <MONTHS; count++)
{
if (rainfalls[count] > highest)
highest = rainfalls[count];
}
return highest;

}
//define the lowest rainfall
double getlowestRainfall(double rainfalls[], int MONTHS)

{
double lowest;
lowest = rainfalls[0];
for (int count =1; count <MONTHS; count++)
{
if (rainfalls[count]< lowest)
lowest = rainfalls[count];

}
return lowest;

}

Last edited on
lines 20-21 of this post could give you some ideas: http://www.cplusplus.com/forum/general/214265/#msg998189
basically along with setw() you select the alignment of the fill characters
Topic archived. No new replies allowed.