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

In summary: return(mean(array, size)); }double sum(double array[], int size) /**{ ... return(sum(array, size)); }double low(double array[], int size) /**{ ... return(low(array, size)); }double high(double array[], int size) /**{ ... return(high(array, size)); }
  • #1
Joe_K
33
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

  • expenses.txt
    287 bytes · Views: 399
Physics news on Phys.org
  • #2
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 *"?
 
  • #3
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.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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!
 
  • #7
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)
{
   
    }
 
  • #8
you need to close( } ) your for loop in your double low function. that should be it
 
  • #9
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.
 

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

1. What is the purpose of the "Urgent Help Needed: C++ Array Program Homework"?

The purpose of the homework is to practice and demonstrate understanding of using arrays in C++ programming.

2. How do I get started with the C++ Array Program Homework?

To get started, you should review your class notes and any relevant textbook chapters on arrays in C++. It would also be helpful to look at examples of similar programs online or consult with your classmates or instructor for guidance.

3. What specific concepts should I focus on when completing the C++ Array Program Homework?

The main concepts to focus on include understanding how to declare and initialize arrays, how to access and manipulate elements in arrays, and how to use loops to iterate through arrays.

4. Can I use online resources or get help from others when completing the C++ Array Program Homework?

While it is important to understand and complete the homework on your own, it is acceptable to use online resources and consult with others for guidance and clarification. However, it is important to ensure that you understand the concepts and do not simply copy and paste code without understanding it.

5. What should I do if I am still struggling with the C++ Array Program Homework?

If you are still struggling with the homework, it is important to seek help from your instructor or a tutor. They can provide additional guidance and clarification to help you better understand the concepts and complete the homework successfully.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
23
Views
1K
Back
Top