OK, looking at the problem you're doing way too much work. I've attached a working example. I'd encourage you to use this as a basis for your own program and not just turn this in as is. I can't stop you, but if you're struggling now you need to put the work in to figure out why or things will get a lot worse for the next assignment.
First problem is that you're trying to process the whole file in one array. This is a bad idea for 2 reasons. First off, as you've found it's not straightforward to get an array large enough to hold the data. Second, even with your fix it's possible to overflow that array with a big enough file. So I've redone the program to read and parse a single line at a time. This cuts the memory requirement way down and eliminates the problem of having to guess how big the file is.
Each call to the doConvert function returns the number of seconds for that particular line. As mentioned previously in the responses, keeping a running total plus the number of lines processed allows you to calculate the average with only two variables instead of a possibly limitless array.
Next, I've used getline to read the line. This preserves the formatting of the data (spaces in this case) to make it easier to process. An there's no need to keep multiple copies of the data.
The processing function was also way too complex. For each line you have at most four integer values that need to be stored (seconds, minutes, hours, and days). To keep with your way of doing things, I've used a 4 entry array to hold this data.
I'm also working backwards through the time string. This has the advantage that you know that the first characters you read (the ones at the end of the time) are always the seconds. The next ones are always the minutes, and then the hours, and then the days.
I've used another variable to keep track of the value of the digit you're currently reading (multiplier). This is going to be 1, 10, 100, and so on. Your example data only uses 1 and 10, but 100 might be needed if the days count gets long enough. This multiplier is reset each time a : or - is seen because the next digit processed will be in the 1's place for that particular part of the time.
A final calculation combines the seconds, minutes, hours and days into one total count of seconds, and this is returned to the main function to add to the running total.
I haven't tested this with all of the example input you gave, so at the very least you'll want to do that to make sure I haven't missed anything. And be sure you understand what this is doing if you decide to use it for your program since the class isn't going to get any easier from here.
Oh, also get familiar with the idiom int x = c - '0'. I'll leave it as homework for you to figure out what that means. Same with if ((ch >= '0') && (ch <= '9')), and see where I use a library function to accomplish the same test.
also, if you look at the code deeper, the ONLY reason i read it into a string is so i could easily get the amount of characters that are in the array with str.size(); i dont know how to get the size of the array without doing that. Look at strlen().
#include <iostream>
#include <fstream>
using namespace std;
// Take a line of input data, return the number of seconds used
// for that particular line. Return 0 on parsing error (no data,
// missing imapd token, etc).
unsigned int doConvert(const char *inp_ptr)
{
const char *ptr = inp_ptr;
unsigned int time[4] = {0,0,0,0};
int time_idx = 3;
int multiplier = 1;
// Find space separating time from "imapd" string
while(*ptr && !isspace(*ptr))
ptr += 1;
// Return 0 if there is no space on the line or imapd doesn't come
// right after the space. This also throws out empty lines
if (!*ptr || strncmp(ptr + 1, "imapd", 5))
return 0;
// Work backwards through the time string. Fill in the time[] array
// starting from entry 3 (seconds) and work up to entry 0 (days). Not all
// time strings have the larger time values, but fill them in if they do exist.
for (ptr -= 1; ptr >= inp_ptr; ptr -= 1)
{
// Add this digit to the current time[] array entry, using multiplier
// to adjust it to the correct power of 10 for the digits position in the
// number
// Update that multiplier to keep track of the place of the next
// most significant digit read (ones, tens, hundreds and so on as needed)
if (isdigit(*ptr))
{
time[time_idx] += (*ptr - '0') * multiplier;
multiplier *= 10;
}
// Hitting : or - means that the next entry in the time[] array
// needs to be used
// Reset multiplier to 1 since the first char is always in the ones place
else if ((*ptr == ':') || (*ptr == '-'))
{
time_idx -= 1;
multiplier = 1;
}
}
// convert individual parts of the time string (days, hours, minutes, seconds)
// into one value holding just the number of seconds.
// time[0] = days
// time[1] = hours
// time[2] = minutes
// time[3] = seconds
return (((time[0] * 24) + time[1]) * 60 + time[2]) * 60 + time[3];
}
int main()
{
char c[256];
unsigned int rc;
unsigned int lines = 0;
double total = 0.0;
ifstream read("dataFile");
if (read.is_open())
{
while(! read.eof())
{
// use getline here to preserve white space and other formatting
read.getline(c, sizeof(c));
rc = doConvert(c);
// The function returns 0 if there's no data on the current line
// If convert_line returns anything other than 0, the value is
// the number of seconds extracted from the time string on the
// current line.
if (rc)
{
total += rc;
lines += 1;
}
}
read.close();
cout << endl << "Total is: " << total << " seconds." << endl;
cout << endl << "The average time spend on imapd is: " << total/lines << " seconds." << endl;
}
else
cout << "Unable to open dataFile.";
return 0;
}