I'm struggling with a class assignment. I've tried writing this several ways, I'm extremely confused.
Assignment:
Read in ten integer values from a from a file, and store them in an array or vector. The program you will write will read and store the last ten values read from a temperature gauge. The reading of the values should be done in a separate function that takes an integer array as a parameter, and read from a file named tempInput.txt: example: void readData(int tempArray[]) The file numbers will be on consecutive lines. Then, from main, you will call another function, whose signature and return type is thus: bool isDangerous(int tempArray[]); The function is dangerous will sum the values in the tempArray and divide by 10 storing the result (average) in another variable of the appropriate data type. Then, if the temperature is greater than 100, the function should return true. if it is 100 or less, it should return false. in main, you should use the function in a way such that you print: the temp is ok (if return false) or, the temp is too hot (if > 100)
If I write a function readData(int array[]) then I must already an array to pass as the argument. But, the entire purpose of this function is to read from file, THEN create the array. Do I need to write a placeholder temp. array?
Do I need to make the array a string then parse to int or double?
My non-working start is:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
//prototypes
void readTemp();
int main()
{
int allTemp[10];
readTemp();
}
void ReadTemp()
{
ifstream in_File;
int inNumbers[10];
double average;
in_File.open("tempInput.txt");
for(int i = 0; i < 10; ++i)
{
in_File>>inNumbers[i];
average = inNumbers[i++]/i;
}
cout<< average <<endl;
}
Thanks for any tips. I couldn't get the file read in working so I haven't started on the function that will average the data and return bool.
ReadDatais the array you will fill up with data from the file. In C++ raw arrays are by default, passed by reference, so you will directly modify the one you pass in.