debug

I think I am getting a message about the formal parameter list[](an array). Am I not introducing it into the function correctly. The following are messages that I am receiving:
10 C:\Dev-Cpp\main.cpp declaration of `list' as array of references

C:\Dev-Cpp\main.cpp In function `int main(int, char**)':

10 C:\Dev-Cpp\main.cpp too many arguments to function `void find_mean(std::ifstream&, std::ofstream&, int&, double&)'

Can there be too many arguments in a function?


#include <cstdlib>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;

void get_file(ifstream& fin, ofstream& fout);
/* to get, open, and test the files for input and output data.*/
void find_mean(ifstream& fin, ofstream& fout, int& count, double& mean, double& list[]);
/* to find the average of all numbers in the file, write the mean to
the output file, and return the count to the main. */
void deviation (ifstream& fin, ofstream& fout, double list[], double mean, int count);
/*to geet the standard deviation of the group of numbers and write it to the output file. */



int main(int argc, char *argv[])
{
int count = 0;
double mean, list[1000];
ifstream fin;
ofstream fout;
get_file (fin, fout);
find_mean (fin, fout, count, mean, list);
deviation (fin, fout, list, mean, count);
system("PAUSE");
return EXIT_SUCCESS;
}
void get_file (ifstream &fin, ofstream &fout)
{
char in_file[40], out_file[40];
cout << "What is the name of the file containing data to be analyzed?\n";
cin >> in_file;
fin.open(in_file);
if (fin.fail())
{
cout << "Input file failed.\n";
exit(1);
}
cout << "What is the name of the file where you would like to store data?\n";
cin >> out_file;
fout.open(out_file);
if (fout.fail())
{
cout << "imput file failed.\n";
exit(1);
}
}
void find_mean (ifstream& fin, ofstream& fout, int& count, double& mean, double& list[])
{
count = 0;
double next, sum = 0;
fout.setf(ios::fixed);
fout.setf(ios::showpiont);
fout.precision(2);
while (fin >> next)
{
list[count] = next;
count++;
sum += next;
{
mean = (sum/count);
fout << "MEAN: " << mean << endl;
}
void deviation (ifstream& fin, ofstream& fout, double list[], double mean, int count)
{
fout.setf(ios::fixed);
fout.setf(ios::showpiont);
fout.precision(2);
double variance, double;
int sum = 0;
for (int i = 0, i < count, i++)
{
sum += pow((list[i] - mean), 2.0);
}
variance = sum/count;
fout << "Standard deviation: " << sqrt(variance) << endl;
}

i do not know and i am new of c++ but i do not see any reason why putting ofstream and ifstream in the parentesis
ifstream file("C:/folder/file");
ofstream fle("C:/folder/file");
file.close();
and i will use bool not void like
.......
bool getfile(){
ifstream file("C:/folder/file");
file.close();
return true;

}

.......
if (bool getfile() == true){
do all the maths
}
.........
sorry if the little help good luck
Last edited on
double& list[]
You can't declare list like that.
I think just
double list[]
will do what you want it to do.

And
fout.setf(ios::showpiont);
You misspelled ios::showpoint. (Twice, I think)

And you're missing a few closing braces.
This is what your find_mean function looks like right now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void find_mean (ifstream& fin, ofstream& fout, int& count, double& mean, double& list[])
{
    count = 0;
    double next, sum = 0;
    fout.setf(ios::fixed);
    fout.setf(ios::showpiont); // Remember to fix the typo here!
    fout.precision(2);
    while (fin >> next)
    {
        list[count] = next;
        count++;
        sum += next;
        { // You probably meant for this to be a }, not a {
            mean = (sum/count);
            fout << "MEAN: " << mean << endl;
        }
void deviation (ifstream& fin, ofstream& fout, double list[], double mean, int count)
{
    //... 


double variance, double;
I think you can tell what you did wrong there.

for (int i = 0, i < count, i++)
The commas should be semicolons.

And in your deviation function, you don't use fin.
You might want to remove that parameter if you're not going to use it.

Same with argc and argv in
int main(int argc, char *argv[])
(you can put just int main())

EDIT: Oh, and I don't think you're computing the standard deviation correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void deviation (ifstream& fin, ofstream& fout, double list[], double mean, int count)
{
    fout.setf(ios::fixed);
    fout.setf(ios::showpiont); // Typo, as I already mentioned
    fout.precision(2);
    double variance, double; // I think you know about this already
    int sum = 0; // Why int? Should be double
    for (int i = 0, i < count, i++) // Should be semicolons, as I already said
    {
        sum += pow((list[i] - mean), 2.0);
    }
    variance = sum/count; // I think this is supposed to be (count-1)
    fout << "Standard deviation: " << sqrt(variance) << endl;
}
Last edited on
Very helpful. Thanks. Without the "&" in front of the array in the function declaration and heading, will the program still pass the values obtained by the array list in the function find_mean back to the main function after find_mean is executed?
The values will still be there, because when you pass the array to a function, you're really just passing the location in memory of the first element of the array (not a copy of the whole array), so any changes that the function makes to the array will still be there after it returns.
Topic archived. No new replies allowed.