I am taking a intro programming course currently. I am stumped by my current assignment. The program is suppose to do the following using functions only (no other code allowed in main):

1. Get data from an input file and put into a parallel array.
2. Find and display the high temp.
3. Find and display the low temp.
4. Calculate the average temp and display.

I am getting a run time error. Please give me some advice on what is going wrong.

I attached two files to this post. One is the source code (cityTemps) and the other is the input file (temperatures). Thanks a lot.

Attachments
#include <iostream>	// for cin, cout, endl
#include <string>		// string class
#include <fstream>		// input file stream

using namespace std;

// function prototypes
void readData (string [], double []);
void getHighTemp (string [], double []);
void getLowTemp (string [], double []);
void calcAvgTemp (double []);


int main()	 // start of main
{
	// declaration of variables and arrays
	string cityArr[8] = {" "};
	double tempArr[8] = {0};

	// functions
	readData(cityArr, tempArr);
   	getHighTemp(cityArr, tempArr);
  	getLowTemp(cityArr, tempArr);
    	calcAvgTemp(tempArr);
		
	system ("pause");
	return 0;	// indicates that the program ended successfully

}   // end of main

// function definitions

// read data from input file (temperatures.txt) and put into parrallel arrays
void readData (string cityArr[], double tempArr[]) 
{
	string city = " ";
	double temp = 0;
	ifstream inFile;
    inFile.open("termperatures.txt");
	if (inFile.is_open() == true) // if 1
	{
		inFile >> city;
		int i = 0;
		while (!inFile.eof()) // while 1
		{
			cityArr[i] = city;
			inFile >> temp;
			tempArr [i] = temp;
			inFile >> city;
			i++;
		}
	}
	else
	{
		cout <<"File did not open.";
	} //end if 1
	
}

// find the highest temp and display it and corresponding city
void getHighTemp (string cityArr[], double tempArr[])
{
	double highTemp = tempArr[0];
	string city = " ";
	
	int x = 1;
	
	while (x < 8) // while 1
	{
		if (tempArr[x] > highTemp) // if 1
			highTemp = tempArr[x];
		// end if 1
		x = x + 1;
		city = cityArr[x];
	} // end while 1
	cout << "Highest temperature:" << highTemp << ", " << city;
}

// find the lowest temp and display it and corresponding city	
void getLowTemp (string cityArr[], double tempArr[])
{
	double lowTemp = tempArr[0];
	string city = " ";
	
	int x = 1;
	
	while (x < 8) // while 1
	{
		if (tempArr[x] < lowTemp) // if 1
			lowTemp = tempArr[x];
		// end if 1
		x = x + 1;
		city = cityArr[x];
	} // end while 1
	cout << "Lowest temperature:" << lowTemp << ", " << city;
}

// calc the average temp and display
void calcAvgTemp (double tempArr[])
{
	double total = 0.0;
	double avg = 0.0;
	
	for (int x = 0; x < 8; x = x+1)
		total = total + tempArr[x];
	// end for
	avg = total / 8.0;
	cout << "Average temperature: " << avg << endl;
}
Anchorage	-1
Austin 	89
Chicago	46
Dallas	75
Detroit	60
Philadelphia	55
Phoenix	95
Reno	44

Start with

readData(cityArr, tempArr);
//   	getHighTemp(cityArr, tempArr);
//  	getLowTemp(cityArr, tempArr);
//	calcAvgTemp(tempArr);

Does it crash?

How about

readData(cityArr, tempArr);
   	getHighTemp(cityArr, tempArr);
//  	getLowTemp(cityArr, tempArr);
//	calcAvgTemp(tempArr);

Maybe this

readData(cityArr, tempArr);
   	getHighTemp(cityArr, tempArr);
  	getLowTemp(cityArr, tempArr);
//	calcAvgTemp(tempArr);

Understand?

Or you could use a debugger, which would save all the edit the code, try it and repeat.

Thanks. I tried that. When I run the first function by itself it claims the input file is not opening. I am try to figure out why right now.

Look at the spelling of the file name you are attempting to open.

A good idea is to have your reading function return a value indicating success (or how much success) or failure - use that to determine if the program should even go on to attempt processing the data.

Darn, I feel like a fool. Thanks!

Don't beat yourself up. I think most of us do something like this from time to time. It's hard to proofread your own writing.

This question has already been answered. Start a new discussion instead.