Urgent help needed - C++ array program

Discussion in 'Programmer's Corner' started by dmc0162, Mar 28, 2012.

  1. dmc0162

    Thread Starter Active Member

    Dec 13, 2007
    51
    1
    I am not completely finished with the program, 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.


    Code ( (Unknown Language)):
    1.  
    2. #include <iostream>
    3. #include <iomanip>
    4. #include <fstream>
    5.  
    6. const int NUM_MON = 12; // number of months
    7.  
    8. using namespace std;
    9.  
    10.  
    11. //function prototypes
    12.  
    13. void buildArrays(double[], double[], double[]);
    14.  
    15. void printUtilityStat(string, double, int);
    16.  
    17. void getSumArray(double, double, double, double);
    18.  
    19. void printArray(string, double, int);
    20.  
    21. double mean(double, int);
    22.  
    23. double sum(double, int);
    24.  
    25. double low(double, int);
    26.  
    27. double high(double, int);
    28.  
    29. void sortArray(double, int);
    30.  
    31. double gas[NUM_MON];
    32. double water[NUM_MON];
    33. double electricity[NUM_MON];
    34.  
    35.  
    36. int main()
    37. {
    38.  
    39. buildArrays(gas,water,electricity);
    40.  
    41. for (int i=0; i<NUM_MON; i++)
    42. {
    43. cout<< gas[i]<<endl;
    44. }
    45.  
    46. cout<<endl<<endl<<"Lowest is "<<low(gas, NUM_MON);
    47. cout<<endl<<endl<<"Highest is "<<high(gas, NUM_MON);
    48.  
    49.  
    50.  
    51. system ("pause");
    52. return 0;
    53. }
    54.  
    55. void buildArrays(double gas[], double electricity[], double water[])
    56. {
    57. ifstream inFile;
    58.  
    59. inFile.open("expenses.txt");
    60.  
    61. if (inFile.fail() )
    62. {
    63. cout<<"Unable to open expenses.txt file\n";
    64. exit(1);
    65. }
    66.  
    67. string temp;
    68. int i;
    69.  
    70. inFile>> temp;
    71.  
    72. for (i=0; i<NUM_MON; i++)
    73. {
    74. inFile>> gas[i];
    75. }
    76.  
    77. inFile>> temp;
    78.  
    79. for (i=0; i<NUM_MON; i++)
    80. {
    81. inFile>> electricity[i];
    82. }
    83.  
    84. inFile>> temp;
    85.  
    86. for (i=0; i<NUM_MON; i++)
    87. {
    88. inFile>> water[i];
    89. }
    90.  
    91. }
    92.  
    93.  
    94.  
    95.  
    96. void printUtilityStat(string caption, double array[], int size)
    97. {
    98.  
    99.  
    100. }
    101.  
    102. void getSumArray(double gas[], double electricity[], double water[], double sums[])
    103. {
    104.  
    105. }
    106.  
    107. void printArray(string caption, double array[], int size)
    108. {
    109.  
    110. }
    111.  
    112. double mean(double array[], int size)
    113. {
    114. double sum=0,
    115. result=0;
    116.  
    117. for (int i=0; i< NUM_MON; i++)
    118. {
    119. sum+= array[i];
    120.  
    121. return (sum)/ size;
    122.  
    123. }
    124.  
    125. }
    126.  
    127. double sum(double array[], int size)
    128. {
    129.  
    130. }
    131.  
    132. double low(double array[], int size)
    133. {
    134. int min =array[0];
    135.  
    136. for (int i=0; i<size; i++)
    137. {
    138. if (array[i]<min)
    139. {
    140. min=array[i];
    141. }
    142.  
    143. return min;
    144.  
    145. }
    146.  
    147. double high(double array[], int size)
    148. {
    149. int max =array[0];
    150.  
    151. for (int i=0; i<size; i++)
    152. {
    153. if (array[i]>max)
    154. {
    155. max=array[i];
    156. }
    157.  
    158. return max;
    159.  
    160. }
    161.  
    162. void sortArray(double array[], int size)
    163. {
    164.  
    165. }
    166. [/i][/i][/i][/i][/i][/i][/i][/i][/i]
     
    Last edited by a moderator: Mar 31, 2012
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    It might actually help if you showed the offending line. Don't expect us to read through your dozens of lines of code.
     
  3. cheezewizz

    Active Member

    Apr 16, 2009
    82
    10
    At a glance, one problem jumps out. Your low and high functions are defined as if they're supposed to return doubles but they return 'min' and 'max' which are ints despite the fact you're trying to squeeze doubles into them...
     
  4. maxpower097

    Well-Known Member

    Feb 20, 2009
    795
    388
    error logs help too.
     
Loading...