Search code examples
c++carrayscodeblocks

Codeblocks inputting a text file array error


i dont know where im going wrong im stuck in two problems that i have to submit for school work . its my first time ever trying to write C++ so i do apologize if this sounds stupid, but even with this i kept getting error message unknown escape sequence (i will delete this post after i get an answer out of plagiarism sake) I don't mean to offend anyone. appreciate all the help

the question that i had to solve is : Write a C++ program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions:

Function getData: This function reads and stores data in the two-dimensional array.

Function averageHigh: This function calculates and returns the average high temperature for the year.

Function averageLow: This function calculates and returns the average low temperature for the year.

Function indexHighTemp: This function returns the index of the highest high temperature in the array.

Function indexLowTemp: This function returns the index of the lowest low temperature in the array.

(These functions must all have the appropriate parameters.)

The file temperaturedata.txt is in 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 ;
}

Solution

  • If you want the backslash character, you should use two backslashes. Use '\\' if you want the backslash character. Use "\\" if you want it in a string. This is because the backslash character means the beginning of an escape sequence.

    Replace line 25 with

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