代码块输入文本文件数组错误
Codeblocks inputting a text file array error
我不知道我哪里出了问题,我遇到了两个必须提交给学校作业的问题。这是我第一次尝试编写 C++,所以如果这听起来很愚蠢,我深表歉意,但即使这样我仍然收到错误消息未知的转义序列(出于剽窃的缘故,我会在得到答案后删除这个 post)我无意冒犯任何人。感谢所有帮助
我必须解决的问题是:
编写一个 C++ 程序,使用二维数组存储一年中每个月的最高和最低温度。该程序应输出当年的平均高温、平均低温以及最高和最低温度。您的程序必须包含以下功能:
函数getData:该函数读取并存储二维数组中的数据。
函数 averageHigh:此函数计算 returns 年的平均高温。
函数 averageLow:此函数计算 returns 年的平均低温。
函数indexHighTemp:此函数returns数组中最高温度的索引。
函数indexLowTemp:此函数returns数组中最低低温的索引。
(这些函数都必须有合适的参数。)
文件 temperaturedata.txt 在 D2L
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void getData ( ifstream& inData, double extremes [12][2] ) ;
void averageHigh ( double extremes [12][2], double& avgHigh ) ;
void averageLow ( double extremes [12][2], double& avgLow ) ;
void indexHighTemp ( double extremes [12][2], int& highMonth ) ;
void indexLowTemp ( double extremes [12][2], int& lowMonth ) ;
int main( int nNumberofArgs, char* pszArgs[] )
{
ifstream inData ;
double extremes [12] [2] ;
double avgHigh, avgLow ;
int highMonth, lowMonth ;
inData.open("C:\Users\Owner\Desktop\C++ homework\temperature problem\temperature problem ........\temperaturedata.txt");
if(!inData)
{
cout << "There was an error opening the input file" << endl ;
exit ( 1 ) ;
}
getData ( inData, extremes ) ;
averageHigh ( extremes, avgHigh ) ;
cout << fixed << showpoint << setprecision(2) ;
cout << "The average high temperature was " << avgHigh << " degrees" << endl ;
averageLow ( extremes, avgLow ) ;
cout << "The average low temperature was " << avgLow << " degrees" << endl ;
indexHighTemp ( extremes, highMonth ) ;
cout << "The month with the highest high temperature was " << highMonth << endl ;
indexLowTemp ( extremes, lowMonth ) ;
cout << "The month with the lowest low temperature was " << lowMonth << endl ;
return 0 ;
}
void getData ( ifstream& inData, double extremes [12][2] )
{
int row ;
for ( row=0; row<12; row++ )
inData >> extremes [row][0] >> extremes [row][1] ;
return ;
}
void averageHigh ( double extremes [12][2], double& avgHigh )
{
double sum = 0 ;
for ( int i=0; i<12; i++ )
sum += extremes [i][0] ;
avgHigh = sum/12.0 ;
return ;
}
void averageLow ( double extremes [12][2], double& avgLow )
{
double sum = 0 ;
for ( int i=0; i<12; i++ )
sum += extremes [i][1] ;
avgLow = sum/12.0 ;
return ;
}
void indexHighTemp ( double extremes [12][2], int& highMonth )
{
int ind = 0 ;
double highest = extremes [0][0] ;
for ( int i=0; i<12; i++ )
if ( extremes[i][0] > highest )
{
highest = extremes[i][0] ;
ind = i ;
}
highMonth = ind ;
return ;
}
void indexLowTemp ( double extremes [12][2], int& lowMonth )
{
int ind = 0 ;
double lowest = extremes [0][1] ;
for ( int i=0; i<12; i++ )
if ( extremes[i][1] < lowest )
{
lowest = extremes[i][0] ;
ind = i ;
}
lowMonth = ind ;
return ;
}
如果你想要反斜杠字符,你应该使用两个反斜杠。如果需要反斜杠字符,请使用 '\'
。如果要在字符串中使用 "\"
。这是因为反斜杠字符表示转义序列的开始。
将第 25 行替换为
inData.open("C:\Users\Owner\Desktop\C++ homework\temperature problem\temperature problem ........\temperaturedata.txt");
我不知道我哪里出了问题,我遇到了两个必须提交给学校作业的问题。这是我第一次尝试编写 C++,所以如果这听起来很愚蠢,我深表歉意,但即使这样我仍然收到错误消息未知的转义序列(出于剽窃的缘故,我会在得到答案后删除这个 post)我无意冒犯任何人。感谢所有帮助
我必须解决的问题是: 编写一个 C++ 程序,使用二维数组存储一年中每个月的最高和最低温度。该程序应输出当年的平均高温、平均低温以及最高和最低温度。您的程序必须包含以下功能:
函数getData:该函数读取并存储二维数组中的数据。
函数 averageHigh:此函数计算 returns 年的平均高温。
函数 averageLow:此函数计算 returns 年的平均低温。
函数indexHighTemp:此函数returns数组中最高温度的索引。
函数indexLowTemp:此函数returns数组中最低低温的索引。
(这些函数都必须有合适的参数。)
文件 temperaturedata.txt 在 D2L
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
void getData ( ifstream& inData, double extremes [12][2] ) ;
void averageHigh ( double extremes [12][2], double& avgHigh ) ;
void averageLow ( double extremes [12][2], double& avgLow ) ;
void indexHighTemp ( double extremes [12][2], int& highMonth ) ;
void indexLowTemp ( double extremes [12][2], int& lowMonth ) ;
int main( int nNumberofArgs, char* pszArgs[] )
{
ifstream inData ;
double extremes [12] [2] ;
double avgHigh, avgLow ;
int highMonth, lowMonth ;
inData.open("C:\Users\Owner\Desktop\C++ homework\temperature problem\temperature problem ........\temperaturedata.txt");
if(!inData)
{
cout << "There was an error opening the input file" << endl ;
exit ( 1 ) ;
}
getData ( inData, extremes ) ;
averageHigh ( extremes, avgHigh ) ;
cout << fixed << showpoint << setprecision(2) ;
cout << "The average high temperature was " << avgHigh << " degrees" << endl ;
averageLow ( extremes, avgLow ) ;
cout << "The average low temperature was " << avgLow << " degrees" << endl ;
indexHighTemp ( extremes, highMonth ) ;
cout << "The month with the highest high temperature was " << highMonth << endl ;
indexLowTemp ( extremes, lowMonth ) ;
cout << "The month with the lowest low temperature was " << lowMonth << endl ;
return 0 ;
}
void getData ( ifstream& inData, double extremes [12][2] )
{
int row ;
for ( row=0; row<12; row++ )
inData >> extremes [row][0] >> extremes [row][1] ;
return ;
}
void averageHigh ( double extremes [12][2], double& avgHigh )
{
double sum = 0 ;
for ( int i=0; i<12; i++ )
sum += extremes [i][0] ;
avgHigh = sum/12.0 ;
return ;
}
void averageLow ( double extremes [12][2], double& avgLow )
{
double sum = 0 ;
for ( int i=0; i<12; i++ )
sum += extremes [i][1] ;
avgLow = sum/12.0 ;
return ;
}
void indexHighTemp ( double extremes [12][2], int& highMonth )
{
int ind = 0 ;
double highest = extremes [0][0] ;
for ( int i=0; i<12; i++ )
if ( extremes[i][0] > highest )
{
highest = extremes[i][0] ;
ind = i ;
}
highMonth = ind ;
return ;
}
void indexLowTemp ( double extremes [12][2], int& lowMonth )
{
int ind = 0 ;
double lowest = extremes [0][1] ;
for ( int i=0; i<12; i++ )
if ( extremes[i][1] < lowest )
{
lowest = extremes[i][0] ;
ind = i ;
}
lowMonth = ind ;
return ;
}
如果你想要反斜杠字符,你应该使用两个反斜杠。如果需要反斜杠字符,请使用 '\'
。如果要在字符串中使用 "\"
。这是因为反斜杠字符表示转义序列的开始。
将第 25 行替换为
inData.open("C:\Users\Owner\Desktop\C++ homework\temperature problem\temperature problem ........\temperaturedata.txt");