Comp Sci Why Does My C++ Array Program Give a Cannot Convert Double to Double Error?

AI Thread Summary
The discussion centers on a C++ programming error related to function prototypes and definitions, specifically the "cannot convert double to double" error. The user is attempting to implement functions to find the lowest and highest values in an array but encounters compilation issues due to mismatched function signatures. It is noted that the function prototypes for `getSumArray` do not match its definition, as the prototype specifies single doubles while the definition uses arrays. Additionally, there are suggestions to improve code readability through proper indentation and closing brackets. The conversation emphasizes the importance of matching function prototypes with their definitions to avoid compilation errors.
Joe_K
Messages
32
Reaction score
0

Homework Statement



I am not completely finished with the assignment, but I was trying to test to make sure that my functions "low" and "high" would in fact return the proper value (the lowest number in the array, and the highest number in the array, respectively). However, when I compile, I get an error "CANNOT CONVERT DOUBLE TO DOUBLE". Can anybody see the reason why I might be getting this error? I would very much appreciate any help! Thank you!

The file reads in data from the expenses.txt document, so I have included that file as an attachment as well.

As you can see, I have no finished coding all of my functions yet, but I am trying to find out why I am getting the compile error.




Homework Equations





The Attempt at a Solution



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

const int NUM_MON = 12; // number of months

using namespace std;


//function prototypes

void buildArrays(double[], double[], double[]);

void printUtilityStat(string, double, int);

void getSumArray(double, double, double, double);

void printArray(string, double, int);

double mean(double, int);

double sum(double, int);

double low(double, int);

double high(double, int);

void sortArray(double, int);

double gas[NUM_MON];
double water[NUM_MON];
double electricity[NUM_MON];


int main()
{

buildArrays(gas,water,electricity);

for (int i=0; i<NUM_MON; i++)
{
cout<< gas<<endl;
}

cout<<endl<<endl<<"Lowest is "<<low(gas, NUM_MON);
cout<<endl<<endl<<"Highest is "<<high(gas, NUM_MON);



system ("pause");
return 0;
}

void buildArrays(double gas[], double electricity[], double water[])
{
ifstream inFile;

inFile.open("expenses.txt");

if (inFile.fail() )
{
cout<<"Unable to open expenses.txt file\n";
exit(1);
}

string temp;
int i;

inFile>> temp;

for (i=0; i<NUM_MON; i++)
{
inFile>> gas;
}

inFile>> temp;

for (i=0; i<NUM_MON; i++)
{
inFile>> electricity;
}

inFile>> temp;

for (i=0; i<NUM_MON; i++)
{
inFile>> water;
}

}




void printUtilityStat(string caption, double array[], int size)
{


}

void getSumArray(double gas[], double electricity[], double water[], double sums[])
{

}

void printArray(string caption, double array[], int size)
{

}

double mean(double array[], int size)
{
double sum=0,
result=0;

for (int i=0; i< NUM_MON; i++)
{
sum+= array;

return (sum)/ size;

}

}

double sum(double array[], int size)
{

}

double low(double array[], int size)
{
int min =array[0];

for (int i=0; i<size; i++)
{
if (array<min)
{
min=array;
}

return min;

}

double high(double array[], int size)
{
int max =array[0];

for (int i=0; i<size; i++)
{
if (array>max)
{
max=array;
}

return max;

}

void sortArray(double array[], int size)
{

}
 

Attachments

Physics news on Phys.org
your function prototype for getsumarray(); does not match the definition.

does your compiler provide line numbers for the error?
does it perhaps say "cannot convert double to double *"?
 
earlofwessex said:
your function prototype for getsumarray(); does not match the definition.

does your compiler provide line numbers for the error?
does it perhaps say "cannot convert double to double *"?

I must be missing something, where do you see that the prototype does not match the definition? Thanks for you help. I will post the line number of the compile error in a few minutes when I get back to my main computer.
 
Joe_K said:
I must be missing something, where do you see that the prototype does not match the definition? Thanks for you help. I will post the line number of the compile error in a few minutes when I get back to my main computer.

your functions (all of them) specify double arrays as arguments, aka a pointer to a double.
your prototypes specify a double, not a double* (or double[]). does that make sense?

though your function definition is what counts, the compiler is referring to the prototype to make sure you are passing the correct datatypes.

ps. its really bad practise to declare global variables, especially when you don't even use their globality.
 
Here's your prototype:
Code:
void getSumArray(double, double, double, double);

Here's your function definition:
Code:
void getSumArray(double gas[], double electricity[], double water[], double sums[])
{

}

They don't match: the first takes four double args and the second takes four double[] args, which are essentially pointers to type double.

When you post code use [noparse]
Code:
 tag at the top and a
[/noparse] tag at the bottom. These make your code easier to read by preserving any indentation you might have used.
 
Mark44 said:
Here's your prototype:
Code:
void getSumArray(double, double, double, double);

Here's your function definition:
Code:
void getSumArray(double gas[], double electricity[], double water[], double sums[])
{

}

They don't match: the first takes four double args and the second takes four double[] args, which are essentially pointers to type double.

When you post code use [noparse]
Code:
 tag at the top and a
[/noparse] tag at the bottom. These make your code easier to read by preserving any indentation you might have used.

Ah, I see now. Thank you very much for pointing that out for me guys. From now on I will be sure to use the
Code:
 tags.  Thanks!
 
Ok, I fixed that and now it seems I have a new problem. I am getting compile errors:line 148: a function-definition is not allowed before '{' token
line 148: expected , or ; before '{' token
line 164: a function-definition is not allowed before '{' token
line 164: expected , or ; before '{' token
line 166: expected '}' at end of input

Here is my revised code:

Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <fstream>

const int NUM_MON = 12;   // number of months

using namespace std;//function prototypes

void buildArrays(double[], double[], double[]);

void printUtilityStat(string, double[], int);

void getSumArray(double[], double[], double[], double[]);

void printArray(string, double[], int);

double mean(double[], int);

double sum(double[], int);

double low(double[], int);

double high(double[], int);

void sortArray(double[], int);

double gas[NUM_MON];
double water[NUM_MON];
double electricity[NUM_MON];int main()
{
    
    buildArrays(gas,water,electricity);
    
    for (int i=0; i<NUM_MON; i++)
        {
         cout<< gas[i]<<endl;    
             }
             
             cout<<endl<<endl<<"Lowest is "<<low(gas, NUM_MON);
             cout<<endl<<endl<<"Highest is "<<high(gas, NUM_MON);    
system ("pause");
return 0;    
}

void buildArrays(double gas[], double electricity[], double water[])
{
     ifstream inFile;
     
     inFile.open("expenses.txt");
     
     if (inFile.fail() )
        {
        cout<<"Unable to open expenses.txt file\n";
        exit(1);
        }
        
        string temp;
        int i;
        
        inFile>> temp;
        
        for (i=0; i<NUM_MON; i++)
            {
                inFile>> gas[i];  
                  }
                  
        inFile>> temp;
        
        for (i=0; i<NUM_MON; i++)
            {
                inFile>> electricity[i];  
                  }
                  
        inFile>> temp;
        
        for (i=0; i<NUM_MON; i++)
            {
                inFile>> water[i];  
                  }
                       
                       }
                       
   
     

void printUtilityStat(string caption, double array[], int size)
{
     
     
     }

void getSumArray(double gas[], double electricity[], double water[], double sums[])
{
     
     }

void printArray(string caption, double array[], int size)
{
     
     }

double mean(double array[], int size)
{
       double sum=0,
              result=0;
              
       for (int i=0; i< NUM_MON; i++)
       {
        sum+= array[i];
        
        return (sum)/ size;
           
           }
       
       }

double sum(double array[], int size)
{
       
       }

double low(double array[], int size)
{
     double min =array[0];
  
     for (int i=0; i<size; i++)
      {
       if (array[i]<min)
          {
          min=array[i];
          }
          
          return min;
            
       }

double high(double array[], int size)
{
       double max =array[0];
  
     for (int i=0; i<size; i++)
      {
       if (array[i]>max)
          {
          max=array[i];
          }
          
          return max;
          }

 } 

void sortArray(double array[], int size)
{
   
    }
 
you need to close( } ) your for loop in your double low function. that should be it
 
protip: INDENT PROPERLY! It saves you a LOT of time and it will help you to ALWAYS close brackets. Like so:

Code:
#include <iostream>
#include <iomanip>
#include <math.h>
#include <fstream>

const int NUM_MON = 12;   // number of months

using namespace std;//function prototypes
void buildArrays(double[], double[], double[]);
void printUtilityStat(string, double[], int);
void getSumArray(double[], double[], double[], double[]);
void printArray(string, double[], int);
double mean(double[], int);
double sum(double[], int);
double low(double[], int);
double high(double[], int);
void sortArray(double[], int);

double gas[NUM_MON];
double water[NUM_MON];
double electricity[NUM_MON];

int main()
{
    buildArrays(gas,water,electricity);
    for (int i=0; i<NUM_MON; i++)
    {
         cout<< gas[i]<<endl;    
    }
    cout<<endl<<endl<<"Lowest is "<<low(gas, NUM_MON);
    cout<<endl<<endl<<"Highest is "<<high(gas, NUM_MON);
    system ("pause");
    return 0;    
}

void buildArrays(double gas[], double electricity[], double water[])
{
     ifstream inFile;
     
     inFile.open("expenses.txt");
     
     if (inFile.fail() )
     {
         cout<<"Unable to open expenses.txt file\n";
         exit(1);
     }
     string temp;
     int i;
     inFile>>temp; 

     for (i=0; i<NUM_MON; i++)
     {
          inFile>> gas[i];  
     }
                  
     inFile>> temp;
        
     for (i=0; i<NUM_MON; i++)
     {
          inFile>> electricity[i];  
     }
     inFile>> temp;
     for (i=0; i<NUM_MON; i++)
     {
         inFile>> water[i];  
     }
}
                       
void printUtilityStat(string caption, double array[], int size)
{
     
}

void getSumArray(double gas[], double electricity[], double water[], double sums[])
{
     
}

void printArray(string caption, double array[], int size)
{
     
}

double mean(double array[], int size)
{
       double sum=0;
       double result=0;
       for (int i=0; i< NUM_MON; i++)
       {
            sum+= array[i];
            return (sum)/ size;
       }
}

double sum(double array[], int size)
{
       
}

double low(double array[], int size)
{
     double min = array[0];
  
     for (int i=0; i<size; i++)
     {
         if (array[i]<min)
         {
             min=array[i];
         }
         return min;
      }
//ERROR HERE! indenting doesn't match up!

double high(double array[], int size)
{
     double max =array[0];
     for (int i=0; i<size; i++)
     {
          if (array[i]>max)
          {
               max=array[i];
          }
          return max;
      }
} 

void sortArray(double array[], int size)
{
   
}

Be as regular as possible in your indenting, and just use the tab key. +1 tab for going into { blocks, -1 tab for leaving it.

protip 2:
you can replace this:

Code:
if (array[i]>max)
{
    max=array[i];
}
with this:
Code:
if (array[i]>max)
    max=array[i];

Also, your high function as it stands won't work properly.
 

Similar threads

Replies
3
Views
7K
Replies
5
Views
2K
Replies
1
Views
2K
Replies
4
Views
1K
Replies
3
Views
2K
Replies
15
Views
3K
Replies
2
Views
2K
Replies
7
Views
2K
Back
Top