What's wrong with my code?!?

Pages: 12
My problem is that it doesn't really recognize that the file is even there...


Is it there?
yes, I have a folder named "FILE" with an "InputFiles" folder within "FILE". "InputFiles" has "February.dat" and "January.dat" inside....
OK, I guess the question should have been phrased this way: Does your program know the files are there? Have it cout the path it is using just before it opens a file. Is it the right path? (That C:/*/*/*/*/*/ stuff really makes me wonder.) Did you maybe forget the last backslash when you input the directory? Maybe you should add some code to check for that and add a backslash if the user (most likely you) forgets about that. Anyway, the problem is most likely in your path so that's where I would be looking.
ok, so now I put the file up one level (I don't understand why exactly) and it is recognizing "InputFiles/" when I say it in the program. However, now I'm getting this error:

Enter directory with files available for use: InputFiles/

InputFiles/February.dat
Failed to open file.


What's the problem now, I really do not see any problem. Oh, and CodeRebel, I took out the C://*/*/......... because it really has no purpose, like you said.
I figured it out, does anyone have a tip for rounding the number I return from my average number function?! Thanks for the help again
OK, so I basically have every thing all set. However, like my last post said, I do not know how to round my numbers to the nearest integer (i.e. 50.27 --> 50, 62.76 --> 63, 50.50000 --> 51). Also, I forget how to write my output to a file. Basically, I need to output the maximums of each month to "maxsofarrays.txt" and "avgsofarrays.txt". Here is my code, so you know how to call things. Thanks again for everyone's help. Much appreciated. I went from almost giving up on this assignment to understanding most of this stuff, so props to everyone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
  
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>

using namespace std;
typedef vector<string> stringVector;  //Defines a vector of string values
const int MAX_SIZE = 31;

void OpenInputFile(ifstream&, string);
void OpenOutputFile(ofstream&, string);
int ReadArray(ifstream&, int[]);
float CalcAverage(int[], int array_size);
float CalcMax(int array[], int array_size);
int getDir(string dir, stringVector &files);

int main()
{
    ifstream indiv_file;
    ofstream ofs_max;
    ofstream ofs_average;
    int array[MAX_SIZE], next, count = 0;
    float average_temp, maximum_temp;
    int array_size;
    string dir, out_file_max = "MaxsOfArrays.txt", out_file_average = "AvgsOfArrays.txt";
   
    cout << "Enter directory with files available for use: ";  
    cin >> dir;
    stringVector files = stringVector();
    OpenOutputFile (ofs_max, out_file_max);
    OpenOutputFile (ofs_average, out_file_average);
    
    getDir (dir, files);
   
    int fsize = files.size();
    for (unsigned int i = 0; i < fsize; i++)
    {
        count = 0;
       
        if (files[i].at(0)!= '.') 
        {
           //cout << endl << dir + files[i];
           OpenInputFile (indiv_file, (dir + files[i]));
	   if (indiv_file.fail())
           {
              cout << "Fail";
              return 0;
           }
           array_size = ReadArray(indiv_file, array);
           average_temp = CalcAverage(array, array_size);
           maximum_temp = CalcMax(array, array_size);
         
           /* for (int j = 0; j < array_size; j++)
           {
               cout << array[j] << " ";
           } */

           cout << endl << "The average temperature in " << files[i] << " was: " << average_temp << endl;
	   cout << "The maximum temperature in " << files[i] << " was: " << maximum_temp << endl;
	   out_file_max << "The maximum temperature in " << files[i] << " was: " << maximum_temp << endl;        
	}
        indiv_file.close();
     }
     cout << endl;
     return 0;
}

//----------------------------------------

int getDir (string dir, stringVector &files)
{
        DIR *dp;
        struct dirent *dirp;
       
        if((dp  = opendir(dir.c_str())) == NULL) {
                cout << "Error(" << errno << ") opening " << dir << endl;
                return errno;
        }
        while ((dirp = readdir(dp)) != NULL)  {
                files.push_back(string(dirp->d_name));
        }
        closedir(dp);
        return 0;
}

//---------------------------------------

void OpenInputFile(ifstream &in_file, string file_name)
{
     in_file.open(file_name.c_str());
     if (in_file.fail())
     {
        cout << "Failed to open file." << endl;
        exit(1);
     }
}

//--------------------------------------

void OpenOutputFile(ofstream &out_file, string file_name)
{
     out_file.open("MaxsOfArrays.txt");
     out_file.open("AvgsOfArrays.txt");
}

//--------------------------------------

int ReadArray(ifstream &in_file, int number[])
{
     int next(0);
     
     while (in_file >> number[next]) 
     {
	next++;
    	if (next == MAX_SIZE)
		break;
     }
     return next; 
}

//-------------------------------------

float CalcAverage(int array[], int array_size) /
{
     int total = 0, i = 0;
     float average;
    
     for (i; i < array_size; i++)
         total += array[i];        
    
     average = ((static_cast <float> (total)) / array_size); 
}

//------------------------------------

float CalcMax(int array[], int array_size)
{
     float max;	 
     int temp; 

     for (int i = array_size - 1; i >= 0; i--) 
     {
	for (int j = 0; j < array_size - 1; j++) 
        {
		if (array[j] > array[j+1])
                {
			temp = array[j];
                        array[j] = array[j+1];
                        array[j+1] = temp;
                }
        }
        
     }
     max = array[array_size - 1]; 
     return max; 
}
I wouldn't bother rounding an average unless it was an explicit requirement of the assignment. Check the documentation for the cmath file and I think there's a rounding function in it. You can also convert a floating-point number to a string and then delete the decimal point and everything to the right of it from the string and convert the string to an integer if you want to go that route.

Read up on your ofstream. You can pretty much treat it just like cout. Just be sure to flush() it before you close it and you should be good to go.
You do understand that you've posted this topic twice and your getting 2x as much help as you need, which is insulting to those individuals who have gone through the trouble to help.
we're kinda past that now Umz. its understood
Question for all with my code above. I have fixed certain things and it runs beautifully now thanks to all the help. Now, how to I write to my out_file_max and out_file_average files using my OpenOutputFiles. My program doesn't seem to be writing to those two .txt files. All help is appreciated! Thanks!
Topic archived. No new replies allowed.
Pages: 12