输入文本文件数组错误的代码块

时间:2016-04-01 02:37:09

标签: c++ c arrays codeblocks

我不知道我哪里出错我陷入两个问题,我必须提交上学。这是我第一次尝试编写C ++所以我道歉,如果这听起来很愚蠢,但即便如此我仍然得到错误消息未知的转义序列(我将删除这篇文章后我得到一个剽窃的答案)我不意味着冒犯任何人。感谢所有的帮助

我必须解决的问题是: 编写一个C ++程序,它使用二维数组来存储一年中每个月的最高和最低温度。该计划应输出该年的平均高温,平均低温和最高和最低温度。您的程序必须包含以下功能:

Function getData:该函数在二维数组中读取和存储数据。

函数averageHigh:此函数计算并返回当年的平均高温。

函数averageLow:此函数计算并返回当年的平均低温。

函数indexHighTemp:此函数返回数组中最高温度的索引。

函数indexLowTemp:此函数返回数组中最低温度的索引。

(这些函数必须都有适当的参数。)

文件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 ;
}

1 个答案:

答案 0 :(得分:0)

如果你想要反斜杠字符,你应该使用两个反斜杠。如果需要反斜杠字符,请使用'\\'。如果您想要字符串,请使用"\\"。这是因为反斜杠字符表示转义序列的开始。

将第25行替换为

inData.open("C:\\Users\\Owner\\Desktop\\C++ homework\\temperature problem\\temperature problem ........\\temperaturedata.txt");