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

Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue related to a compile error encountered when trying to convert a double to a double in the context of array handling. Participants are examining function prototypes and definitions, particularly focusing on the mismatch between them, as well as other compile errors arising from the code structure. The scope includes debugging, technical explanations, and code refinement.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes an error message indicating "CANNOT CONVERT DOUBLE TO DOUBLE" and seeks help in identifying the cause.
  • Another participant points out a mismatch between the function prototype and its definition for the function getSumArray, suggesting that the prototype specifies double arguments instead of double arrays.
  • Further clarification is provided that the compiler checks the prototype to ensure the correct data types are passed, emphasizing the importance of matching function prototypes and definitions.
  • Participants discuss the implications of declaring global variables, with one suggesting it is poor practice, especially when not utilized effectively.
  • After addressing the prototype issue, a participant reports new compile errors related to function definitions and syntax, indicating ongoing challenges in the code structure.

Areas of Agreement / Disagreement

Participants generally agree on the importance of matching function prototypes with their definitions. However, the discussion remains unresolved regarding the new compile errors and the overall structure of the code.

Contextual Notes

Limitations include unresolved syntax errors and potential issues with the handling of global variables. The discussion does not clarify the specific line numbers associated with the compile errors mentioned.

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++)
{
count<< gas<<endl;
}

count<<endl<<endl<<"Lowest is "<<low(gas, NUM_MON);
count<<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() )
{
count<<"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 ·
Replies
3
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K