1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Urgent help needed - C++ Array Program

  1. Mar 28, 2012 #1
    1. The problem statement, all variables and given/known data

    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.




    2. Relevant equations



    3. 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)
    {

    }
     

    Attached Files:

  2. jcsd
  3. Mar 28, 2012 #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 *"?
     
  4. Mar 28, 2012 #3
    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.
     
  5. Mar 28, 2012 #4
    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 refering to the prototype to make sure you are passing the correct datatypes.

    ps. its really bad practise to declare global variables, especially when you dont even use their globality.
     
  6. Mar 28, 2012 #5

    Mark44

    Staff: Mentor

    Here's your prototype:
    Code (Text):
    void getSumArray(double, double, double, double);
    Here's your function definition:
    Code (Text):
    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 (Text):
     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.
     
  7. Mar 28, 2012 #6
    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 (Text):
     tags.  Thanks!
     
  8. Mar 28, 2012 #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 (Text):

    #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)
    {
       
        }    



         
     

     
     
  9. Mar 28, 2012 #8
    you need to close( } ) your for loop in your double low function. that should be it
     
  10. Mar 28, 2012 #9
    protip: INDENT PROPERLY!!!!! It saves you a LOT of time and it will help you to ALWAYS close brackets. Like so:

    Code (Text):
    #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 (Text):
    if (array[i]>max)
    {
        max=array[i];
    }
    with this:
    Code (Text):
    if (array[i]>max)
        max=array[i];
    Also, your high function as it stands won't work properly.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Urgent help needed - C++ Array Program
Loading...