What's wrong with my code?!?

Pages: 12
Below, I have c/p my code. The program is supposed to input a folder name, and inside that folder there are two .dat files with an array of data in each that I am supposed to read and output the average and the maximum values in each. Can someone help me out please? Also, I do not know what to include in my arguments in my first three function definitions...because when I uncomment my function calls, I get errors that refer to those function arguments. Please help ASAP! Thank you in advance for all the help!


#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& in_file, char string);
void OpenOutputFile(ofstream& out_file, char string);
int ReadArray(ifstream& in_file, int number[]);
float CalcAverage(int array[]);
float CalcMax(int array[]);
int getDir(string dir, stringVector &files);

int main()
{
ifstream in_file;
ofstream out_file;
int i=0, array[MAX_SIZE], next;
float average_temp, maximum_temp;
int start;
string dir, intro, compound;

intro = "C://*/*/*/*/*/*/*/";
cout << "Enter directory with files available for use: "; //Lists the files in the current directory you could input .
cin >> dir;
compound = intro + dir;
cout << compound << endl;
stringVector files = stringVector();

getDir (dir, files);
ifstream indiv_file;
int arr[MAX_SIZE];

for (unsigned int i = 0; i < files.size(); i++)
{
if (files[i].at(0)!= '.') // if the file is a valid file
{
//indiv_file.open((compound + files[i]).c_str());
indiv_file.open((dir + files[i]).c_str());
cout << dir+files[i] << endl;
}
while (indiv_file >> next)
{
arr[i] = next;
i++;
if(i == MAX_SIZE)
break;
}
for (int j = 0; j < i; j++)
{
cout << arr[j] << " " << endl;
}

indiv_file.close();
}
//OpenInputFile (array, string);
//OpenOutputFile (out_file, string);
//ReadArray (in_file, number[]);
average_temp = CalcAverage(array);
maximum_temp = CalcMax(array);

cin >> start;
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, int string)
{
char symbol;
ifstream in;
in.open("January.dat");
if (in.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
do
{
cin.get(symbol);
cout << symbol;
} while (symbol != '.');

in.open("February.dat");
if (in.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
}
void OpenOutputFile(ofstream &out_file, int string)
{
ofstream out;
out.open("MaxOfArrays.txt");
out_file.open("AveragesOfArrays.txt");
}
int ReadArray(ifstream &in_file, int number[])
{
int next;
ifstream in;
while (in >> next)
{
cout << next;
}
return 0;
}
float CalcAverage(int array[])
{
int average, total = 0, i = 0;
for (i; i < MAX_SIZE; i++)
{
total += array[i];
}
average = (total / i);
return 0;
}
float CalcMax(int array[])
{
return 0;
}
Nothing's more fun than a lecture on Netiquette, especially when it gets to come from me.

Why on God's green pastoral Earth would you post this long-@$$ $#!% in at least two (I haven't searched the rest) forums on the same $&#*ing message board?

Having taken that one small step for man, one giant leap for mankind, outside the bounds of propriety and good taste, couldn't you at least throw in a couple of code tags to half-@$$ straighten your @#!% out for us?

Here, let me do that for you, Mr. Lazy Bones...

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
#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& in_file, char string);
void OpenOutputFile(ofstream& out_file, char string);
int ReadArray(ifstream& in_file, int number[]);
float CalcAverage(int array[]);
float CalcMax(int array[]);
int getDir(string dir, stringVector &files);

int main()
{
ifstream in_file;
ofstream out_file;
int i=0, array[MAX_SIZE], next;
float average_temp, maximum_temp;
int start;
string dir, intro, compound;

intro = "C://*/*/*/*/*/*/*/";
cout << "Enter directory with files available for use: "; //Lists the files in the current directory you could input .
cin >> dir;
compound = intro + dir;
cout << compound << endl;
stringVector files = stringVector();

getDir (dir, files);
ifstream indiv_file;
int arr[MAX_SIZE];

for (unsigned int i = 0; i < files.size(); i++)
{
if (files[i].at(0)!= '.') // if the file is a valid file
{
//indiv_file.open((compound + files[i]).c_str());
indiv_file.open((dir + files[i]).c_str());
cout << dir+files[i] << endl;
}
while (indiv_file >> next)
{
arr[i] = next;
i++;
if(i == MAX_SIZE)
break;
}
for (int j = 0; j < i; j++)
{
cout << arr[j] << " " << endl;
}

indiv_file.close();
}
//OpenInputFile (array, string);
//OpenOutputFile (out_file, string);
//ReadArray (in_file, number[]);
average_temp = CalcAverage(array);
maximum_temp = CalcMax(array);

cin >> start;
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, int string)
{
char symbol;
ifstream in;
in.open("January.dat");
if (in.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
do
{
cin.get(symbol);
cout << symbol;
} while (symbol != '.');

in.open("February.dat");
if (in.fail())
{
cout << "Failed to open file." << endl;
exit(1);
}
}
void OpenOutputFile(ofstream &out_file, int string)
{
ofstream out;
out.open("MaxOfArrays.txt");
out_file.open("AveragesOfArrays.txt");
}
int ReadArray(ifstream &in_file, int number[])
{
int next;
ifstream in;
while (in >> next)
{
cout << next;
}
return 0;
}
float CalcAverage(int array[])
{
int average, total = 0, i = 0;
for (i; i < MAX_SIZE; i++)
{
total += array[i];
}
average = (total / i);
return 0;
}
float CalcMax(int array[])
{
return 0;
}


Isn't that a bit better? Everyone else is probably still going to ignore you completely but I think your "code" looks much better now.
How do you do that, I didn't know how...sorry dood
alright, so I figured out how to do it correctly. thanks for helping out coderebel...

Please read the note above my first post to see what my problem is. Thanks again in advance. All help is appreciated!

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
#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& in_file, char string);
void OpenOutputFile(ofstream& out_file, char string);
int ReadArray(ifstream& in_file, int number[]);
float CalcAverage(int array[]);
float CalcMax(int array[]);
int getDir(string dir, stringVector &files);

int main()
{
    ifstream indiv_file;
    ofstream out_file;
    int count = 0, array[MAX_SIZE], next; 
    float average_temp, maximum_temp;
    int start;
    string dir, intro, compound;
    
    intro = "C://*/*/*/*/*/*/*/";
    cout << "Enter directory with files available for use: ";  //Lists the files in the current directory you could input . 
    cin >> dir;
    compound = intro + dir;
    stringVector files = stringVector();
	
    /*cin >> InputFiles/; 
    string dir; 
    get_Dir() --> files[size] 
    ifstream ifs; 
    ifs.open("path+numbers.dat") 
    open((dir+files[i]).c_str())*/
    
    getDir (dir, files);
    
    for (unsigned int count = 0; count < files.size(); count++)
    {
        if (files[count].at(0)!= '.') // if the file is a valid file
        {
           //indiv_file.open((compound + files[i]).c_str());
           indiv_file.open((dir + files[count]).c_str());
           cout << endl << dir + " " + files[count] << endl;
           for (int j = 0; j < count; j++)
	       {
               while (indiv_file >> next)
	           {
                     cout << array[j];
                     j++;
               }
               cout << array[j] << " " << endl;
            }
            if(count == MAX_SIZE)
                 break;
        }
        indiv_file.close();
     }
    //OpenInputFile (array, string);
    //OpenOutputFile (out_file, string);
    //ReadArray (in_file, number[]);
    average_temp = CalcAverage(array);
    maximum_temp = CalcMax(array); 
    
    cin >> start;
    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, int string)
{
     char symbol;
     ifstream in;
     in.open("January.dat");
     if (in.fail())
     {
        cout << "Failed to open file." << endl;
        exit(1);
     }
     
     do
     {
        cin.get(symbol);
        cout << symbol;
     } while (symbol != '.');
             
     in.open("February.dat");
     if (in.fail())
     {
        cout << "Failed to open file." << endl;
        exit(1);
     }
}
void OpenOutputFile(ofstream &out_file, int string)
{
     ofstream out;
     out.open("MaxOfArrays.txt");
     out_file.open("AveragesOfArrays.txt");
}
int ReadArray(ifstream &in_file, int number[])
{
     int next;
     ifstream in;
     while (in >> next)
     {
           cout << next;  
     }   
     return 0;
}
float CalcAverage(int array[])
{
     int average, total = 0, i = 0;
     
     for (i; i < MAX_SIZE; i++)
     {
         total += array[i];         
     } 
     
     average = (total / i);
     return 0;  
}
float CalcMax(int array[])
{
     return 0;  
}
Im srry i know this has nothing to with your work but i also need help my program isn't running. I want it to enter the status of DVDs, and then display a list of the DVDs if they are available. Here's the code, your inputs will be appreciated very much.

#include <stdio.h>
#include <conio.h>
#include <string>

main ()
{
char av_dvd='a';
char out_dvd='o';
char name[10][25];
char status='a' || 'o';

cout<<"Enter the status of the DVDs:\n"<<endl;


//prompt user to input 10 names
for (int i = 0; i<=9; i++)
{
cout<<"\nStatus"<<i+1<<":";
cin>>status[i];
}

if (status == 'a')
{
cout<<"\nDVD LIST:"<<endl;
//display 10 names
for (int j=0;j<=9;j++)
{
cout<<name[j]<<endl;
}
//}

getch();
}
This puts a smile on my face at least, its all mayhem in this post.

satchmo is would be nice to actually read the errors your getting as thats one of the easiest ways to know whats wrong.
OK so from what I can see:
line 37 - I can't see a variable called InputFiles
line 38 - You already made a string call dir

Your passing the wrong type of arguements to your function calls, and you cant use string as a variable name.

smiles, your a hilarious person. Post a new topic to get help on your problem:
Also post any errors you get. And your main function should be int main
Last edited on
there are no errors in my code, its just garbage numbers that are being outputted. why is that?!
there are no errors in my code, its just garbage numbers that are being outputted. why is that?!


Now that's hilarious! I stayed up all night, satchmo, and solved all the problems that didn't exist in your garbage code, OK? I just posted the answers in that other forum you posted in. You must have missed it. :)
I think you have problems, because the only forum post of mine that you have replied to is this one....twice....and you didn't change anything to help me out.
I think you have problems


LOL How perceptive LOL

, because the only forum post of mine that you have replied to is this one


There's that. Then there's the being laid-off thing. If I wasn't laid off I probably wouldn't even have bothered to reply to this post. Would you feel better if I went over and replied to the duplicate of this post you dropped in the General C++ forum too? LOL

....twice....


Well, there you go. That should make you feel better. *snicker*

and you didn't change anything to help me out.


Because:
a) You posted 141 lines of code. I'm not reading that long-@$$ $#!%.
b) The requirements set for at the beginning of your post only call for about
60 lines of code at the most.

The program is supposed to input a folder name, and inside that folder there are two .dat files with an array of data in each that I am supposed to read and output the average and the maximum values in each.


I'm only assuming that it's numerical data because you want to average and maximize it, but if it's some other data you should really let us know. In any case, you could at least make some effort to debug your code and narrow down the territory occupied by the bug to less than 100 lines before you come running and crying to us because you banged your programmatic knee on an algorithmic stump.

c) Are you going to pay me a salary to fix all of your code for you? Because
even before I was laid off I don't think that was technically part of my
job description.

d) If you intend to make fixing your #$@))^ code part of my job description,
you're going to have to pay me cash so I can still collect my unemploy-
ment benefits.

e) I don't just come here in the first place for sport, you know. Even in an
unemployed state I have my own projects to work on, you know.

f) My gosh, look at the time! I do believe it's about BEER:30! LOL
@CoDeReBeL

Nothing's more fun than a lecture on Netiquette


Insults and condescension don't help anyone. True, the original post was not perfectly phrased or formatted. So what? The majorty of posts in this forum are not well-written. People come here for help, and all you're doing is belittling them. What purpose does that serve?
thank you very much jdd! much appreciated!
What purpose does that serve?


None whatsoever. I'm kind of assuming that Satchmo's spammed a couple dozen other forums with this program if he posted it here twice. He's probably getting plenty of help at one of those. And besides, I helped him get his program inside a code tag, didn't I?

Whatever, let's take a look at this:

#include <sys/types.h>

What's up with the slash? Is this normal? I vaguely recall having to try this once years ago and I ended up having to reinstall CodeWarrior before very long. I'm not trying to nitpick ... I just never see this and it makes me wonder if a development system has been corrupted somehow or another.

int count = 0, array[MAX_SIZE], next;

Why do we keep strings in vectors but we make arrays for integers? Just cut the array out and make yourself a vector <int> instead. You'll be lots happier in the long run.

1
2
void OpenInputFile(ifstream& in_file, char string);
void OpenOutputFile(ofstream& out_file, char string);


I believe someone already mentioned the foolhardiness of using "string" as the name of a variable when it's already the name of a class defined within the program. This could very well be the entire problem, but until you solve it and try to run the program again you won't know for sure.

float average_temp, maximum_temp;

You're really not going to need a floating-point variable for the maximum of a list of integers. An int would do fine.

intro = "C://*/*/*/*/*/*/*/";

Is there a reason for that? I'm not saying there couldn't be, by any means, I'm just wondering.

stringVector files = stringVector();

You don't really have to call the default constructor like this. That's why they call it the default constructor. Not calling one at all would have the same effect. In fact I really couldn't swear that this line is legal C++ at all, since initializing a stringVector with a parameterized constructor would read like

stringVector files (v) ; ,

assuming v to be, for instance, a vector to copy.

...

I don't even know where to start about the complications you've introduced into the simple process of opening two text files for reading and two for writing. Towards the beginning of the program you obtained a string representing the directory from the user, right? All you need to open fstreams at will now is to append filenames to that string (in various copies, of course). And don't hard-code "January" and "Februrary" into the file names because next month you don't want to have to edit these source files just to get the same totals and averages, right? Let the computer figure out what the name of the file ought to be. It has a clock for just such a purpose. Call a date() function or something and parse the month out of it. For that matter, demand the exact names of the files from the user, who for all you know misspelled the name of the month when he/she named the files in the first place.

I also think passing the entire array[] of integers as the parameter to a function could be causing you problems if the routines you pass it to alter any of the values stored in it, but we already decided the damn thing should really be a vector <int> anyway, right?

Finally, while it looks to me like your function to calculate the average of these numbers will probably work all right, this...

1
2
3
4
float CalcMax(int array[])
{
     return 0;  
}


is pretty much doomed to failure, don't you think? ROFLMAO Even if all of the integers in array[] are negative ZERO is still the wrong $&#*ing answer unless 0 happens to be in the array as well by some happy coincidence. And, as I pointed out before, the maximum of a set of integers is quite likely to itself be an integer.

Now I hope I've helped to steer you in the right direction on this project.

There. Everybody happy now? :)
Last edited on
Oops, one more thing...

1
2
3
4
5
6
7
8
9
10
11
12
float CalcAverage(int array[])
{
     int average, total = 0, i = 0;
     
     for (i; i < MAX_SIZE; i++)
     {
         total += array[i];         
     } 
     
     average = (total / i);
     return 0;  
}


I guess it's kind of futile to have a function correctly calculate an average if it's just going to return 0 anyway. My bad, dude. Sorry. Should have caught that in the last post.
Wow CoDeReBeL, your a real ball-buster. I'm sorry to hear about your being laid-off but its not helpful to just take it out on someone. Everyone has to learn somehow.

satchmo he debugged your code in a very rude way but at least the problems have been solved, that deserves a heart felt thanks by my standards.
I'm sorry to hear about your being laid-off...


That makes one of us. LOL :) No, really, don't be. Everyone who's not laid off where I work is only working 4 days (32 hrs.) a week. If I wasn't laid off this week I would be working 32 hours to make $5 ... that's right ... $d=5 more than my unemployment benefits for the week. So until we get back to 5-day weeks like a normal workplace I'd just as soon sit around home and play with the computer all week, know what I mean? :)
Thanks a lot coderebel, much appreciated. that'll get me going for a while. Also, where you were wondering about my CalcMax() function, I just hadn't put anything down yet in code, that's why I commented the function call in my int main() function. Thanks a lot, I may have another question here in a little while...
...I just hadn't put anything down yet in code...


LOL Well, I was hoping that'd be the case. Just thought I'd point that out.
would that really mess up my code though?!? to the point where it wouldnt be running properly. My problem is that it doesn't really recognize that the file is even there, not that i'm getting the wrong maximum value (from CalcMax() )...
Pages: 12