help me~~~

what's wrong with it?!

#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

void minMax(const double, int, double&, double&);
void numMeasures(const double, int, double&, double&);
void getData(fstream&, double, int&);

int main()
{
   double count, min, max, mean, var;
   double data[400];
   count = 25; 
   /* how can I 'count' the number of measurements read from the file?*/
   char file;

   cout<<"Enter the name of the data file>";
   cin>>file;

   ifstream dataFile;
   dataFile.open("file");
   if(dataFile.fail())
      {
         cout<<"file did not open, please check it"<<endl;
         system("pause");
         return 1;
      }

   cout<<endl;
   cout<<endl;
   cout<<"Descriptive Statistics"<<endl;
   cout<<"------------------------------"<<endl;

   while(dataFile)
   {
      getData(dataFile, data[], count);  //**error**//
   
      minMax(data[], count, min, max); //**error**//
   
      cout<<"Min:"<<setw(20)<<setprecision(4)<<min<<endl;
      cout<<"Max:"<<setw(20)<<max<<endl;
   
      float range;
      range = max - min;
      cout<<"Range:"<<setw(20)<<range<<endl;
   
      numMeasures(data[], cout, mean, var); //**error**//
         
      cout<<"Mean:"<<setw(20)<<mean<<endl;
      cout<<"Variance:"<<setw(20)<<var<<endl;
       
      float s_d;
      s_d = sqrt(var);
      cout<<"Standard Deviation:"<<setw(20)<<s_d<<endl;
   }
      system("pause");
      return 0;
}

void minMax(const double data[], int count, double& min, double& max)
{  
   min = data[0];
   max = data[0];
      
   for(int i=0; i<count; i++)
      {
         if(data[i] > max)
            max = data[i];
         else if(data[i] < min)
            min = data[i];
      }
}        
      
void numMeasures(const double data[], int count, double& mean, double& var)
{      
   double sum_mean = 0;
   for(int i=0; i<count; i++)
      sum_mean += data[i];
   mean = sum_mean / count;
      
   double sum_var = 0;
   for(int j=0; j<count; j++)
      sum_var += (data[j] - mean);
   var = sum_var / (count-1);
}  
   
void getData(fstream& dataFile, double data[], int& count)
{     
   int amountRead = 0;
   int max_read = 400;
   while(dataFile>>data[amountRead] && amountRead<max_read)
      amountRead++;
}

Dani AI

Generated

The compile error shown in the thread is the classic symptom of invalid tokens in function calls and several API/type mismatches. As observed, removing the square brackets from call sites is required; correctly explained why passing the array name is the right approach. Below are the complementary, practical fixes and a minimal usage example to make the posted program behave as intended.

  • Fix the prototypes so parameter types match their definitions (arrays decay to pointers): use const double data[] or const double * for the first parameter.
  • Make count an int (initialized to 0) — not a double. getData must set count to the number of items actually read.
  • Use std::string filename; (not char file) and open the file with that variable (not the literal "file"). Prefer std::ifstream for input.
  • Call functions with the array name (e.g. getData(in, data, count);) — do not write data[] in a call.
  • Bounds-check while reading: test the index before writing into the array, then assign count = amountRead at the end.
  • Compute variance using squared deviations: accumulate (xi - mean)*(xi - mean) and divide by count - 1 (guard when count < 2). Consider std::vector<double> for unknown size.

Minimal corrected usage pattern:

void minMax(const double data[], int count, double& min, double& max);
void numMeasures(const double data[], int count, double& mean, double& var);
void getData(std::ifstream& in, double data[], int& count);

int count = 0;
double data[400];
std::string filename;
std::cin >> filename;
std::ifstream in(filename);
getData(in, data, count);
minMax(data, count, min, max);
numMeasures(data, count, mean, var);

Compiler warnings (e.g. -Wall) will quickly reveal mismatched prototypes and unused/incorrect types. The combination of corrected prototypes, proper file handling, and the array-passing fix described by and resolves the reported error and several lurking bugs.

Recommended Answers

All 4 Replies

If it doesn't compile, providing the errors would really help because i don't feel like testing the code myself.

If it doesn't compile, providing the errors would really help because i don't feel like testing the code myself.

i put //**error**// next to the 'errors'

Try removing the [] at the data[] so just data

When you are passing an array, you only use the name of the array, you do not use the braces. If you use braces, the compiler thinks you are trying to pass a specific element.

This causes 2 errors:
1. you haven't identified the specific element you want
2. you are no longer passing an array, you're passing a single value

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.