当我在输出的第二行输出代码时,会添加一个空格,我无法弄清楚如何删除。我已经在这个网站和谷歌上搜索了答案。抱歉,这是一个简单的修复。会张贴图片,但没有足够的声誉。
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
// global constant variables
const int YEARS = 8;
const int MONTHS = 12;
const int SPACER =5;
// function prototypes
// function to read in values
void getData(double[][MONTHS], int[]);
// function to display values in table format
void printData(double[][MONTHS], int[]);
// function to print data to screen in table format using arrays
int main()
{
double rain [YEARS][MONTHS];
int years[YEARS];
/*cout << " ";*/
getData(rain, years);
printData(rain, years);
return 0;
}
// function definitions
void getData (double rainArray[][MONTHS], int yearArray[])
{
ifstream fin;
fin.open("rainfall.txt");
if (!fin)
{
cout << "Error opening file, shutting down now.\n" ;
exit(EXIT_FAILURE);
}
else
{
for( int i = 0; i < YEARS; i++)
{
fin >> yearArray[i];
for (int j = 0; j < MONTHS; j++)
{
cout << fixed << setprecision(1);
fin >> rainArray[i][j];
}
}
}
fin.close();
}
void printData (double rainArray[][MONTHS], int yearArray[])
{
for ( int i = 0; i < YEARS; i++){
cout << yearArray[i] << setw(SPACER);
for ( int j = 0; j < MONTHS; j ++)
{cout << rainArray[i][j] << setw(SPACER);
if (j == 11)
cout << endl;
}
}
}