簡體   English   中英

代碼塊輸入文本文件數組錯誤

[英]Codeblocks inputting a text file array error

我不知道我哪里出錯了,我遇到了兩個必須提交作業的問題。 這是我第一次嘗試編寫 C++,所以如果這聽起來很愚蠢,我深表歉意,但即使如此,我仍然收到錯誤消息未知轉義序列(我會在收到抄襲后刪除這篇文章)我不冒犯任何人的意思。 感謝所有的幫助

我必須解決的問題是:編寫一個 C++ 程序,該程序使用二維數組來存儲一年中每個月的最高和最低溫度。 該程序應輸出當年的平均高溫、平均低溫以及最高和最低溫度。 您的程序必須包含以下功能:

函數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 ;
}

如果你想要反斜杠字符,你應該使用兩個反斜杠。 如果需要反斜杠字符,請使用'\\\\' 如果你想在一個字符串中使用"\\\\" 這是因為反斜杠字符表示轉義序列的開始。

將第 25 行替換為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM