Thanks to whoever reads this:
So in my intro level C++ course I have this program I had to code that checks for high/low temperatures and its averages, total rainfall and its averages for the year, through user inputted data. Everything is sent to/from a structure.
I get an error:
"cannot convert 'double' to 'double*' for argument '1' to 'double findLowestTemp(double*, int)."
I get the same error for the findHighestTemp functions.
I'm supposed to be able to pass an entire array through the function, and return one answer. I'm thinking it's works differently with structures involved but I can't find anything good in my textbook to help me understand.
Here are the codes that I know are trouble:
Prototypes:
The calls:
and the function headers:
And the full program. Don't make fun of it's crappiness
Thanks again!
EDIT: Was just about to post that I was getting an error too then I fixed it so give me a second while i edit this post to give you the solution!
EDIT 2:
Right then, so the issue you're having is that you're trying to pass a single double value (month[numMonths].lowTemp) into the functions findLowestTemp and findHighestTemp, when the parameter is asking for an array of doubles (double lowTemp[]).
What you'll want to do is pass in your "months" variable, which as far as I can tell is an array of Mweather, size 3.
Firstly, change
Code:
lowestTemp = findLowestTemp (month[numMonths].lowTemp, numMonths);
highestTemp = findHighestTemp (month[numMonths].highTemp, numMonths);
to
Code:
lowestTemp = findLowestTemp (month, numMonths);
highestTemp = findHighestTemp (month, numMonths);
And what we're going to do is let the functions pick out the highTemp and lowTemp variables.
Now my error here was it was still saying we were trying to convert months[3] to a double [], head to the top of the code and change
Code:
Mweather findLowestTemp (double[], int);
Mweather findHighestTemp (double[], int);
to
Code:
[B]double[/B] findLowestTemp ([B]Mweather[][/B], int);
[B]double[/B] findHighestTemp ([B]Mweather[][/B], int);
(Changes bolded).
Then in the functions for finding the lowest and highest you'll wnat to make it pick out the lowtemp and hightemp variables as it goes on.
Code:
double findLowestTemp(double lowTemp[], int months)
{
double lowestTemp=360; //assigned high value
for(int count=0; count < months; count++){
if (lowTemp[count] > lowestTemp)
lowestTemp = lowTemp[count];
count++;
}
return lowestTemp;
}
Change this to match this:
Code:
double findLowestTemp([B]Mweather lowTemp[][/B], int months)
{
double lowestTemp=360; //assigned high value
for(int count=0; count < months; count++){
if (l[B]owTemp[count].lowTemp[/B] > lowestTemp)
lowestTemp = [B]lowTemp[count].lowTemp[/B];
}
return lowestTemp;
}
So it's now accepting an array of Mweather, and in the if statement it picks up the lowTemp value. Do the same for the findHighestTemp function and you should be able to compile as I was.
Did spot some other errors in the code which is why I haven't actually attempted to test the code with input once it ran because I'll let you find those
However, one thing I will note is that you've used count++; inside the for loops in those two functions, this isn't necessary as count++ in the for constructor does that for you as it loops around.
Hopefully I got this right, it compiles for me anyway without errors, fairly new to C++ myself but i'm profficient in Java and these are mostly syntax errors anyway.
EDIT 3: Ran through the code and it works, but there are issues in your findHighestTemp and findLowestTemp functions that will give you the wrong values at the end, I'll let you figure those out for yourself
Here's my full code WITHOUT that last thing I just mentioned changed incase anything doesn't work that you change:
Code:
#include <iostream>
using namespace std;
struct Mweather
{
double totalRain,
highTemp,
lowTemp,
avgTemp;
};
double computeAvgTemp (double, double);
double findLowestTemp (Mweather[], int);
double findHighestTemp (Mweather[], int);
int main()
{
const int numMonths=3;
int yearlyRain=0;
double avgYearlyRain=0, highestTemp=0, lowestTemp=0;
Mweather month[numMonths];
for (int count=0;count<numMonths;count++){
cout << "--For the " << count+1;
if (count == 0)
cout << "st ";
else if (count == 1)
cout << "nd ";
else if (count == 2)
cout << "rd ";
else
cout << "th ";
cout << "month--" << endl;
cout << "Total rainfall: ";
cin >> month[count].totalRain;
cout << "Highest temperature: ";
cin >> month[count].highTemp;
cout << "Lowest temperature: ";
cin >> month[count].lowTemp;
cout << endl << endl;
}
for (int count=0; count < numMonths; count++){
month[count].avgTemp = computeAvgTemp(month[count].highTemp, month[count].lowTemp);
cout << "The average temperature for month " << count+1 << " is: "
<< month[count].avgTemp << "." << endl;
}
for (int count=0; count < numMonths; count++)
yearlyRain += month[count].totalRain;
avgYearlyRain = yearlyRain / numMonths;
cout << "\nThe average rainfall for the year is " << avgYearlyRain << ".";
cout << "\nThe total rainfail for the year is " << yearlyRain << ".";
lowestTemp = findLowestTemp (month, numMonths);
cout << "The lowest temperature for the year was " << lowestTemp
<< " degrees.";
highestTemp = findHighestTemp (month, numMonths);
cout << "The highest temperature for the year was " << highestTemp
<< " degrees.";
return 0;
}
double computeAvgTemp(double highest, double lowest)
{
double average, twoTemps;
for (int count=0; count < 3; count++){
twoTemps = highest + lowest;
average = twoTemps / 2;
}
return average;
}
double findLowestTemp(Mweather lowTemp[], int months)
{
double lowestTemp=360; //assigned high value
for(int count=0; count < months; count++){
if (lowTemp[count].lowTemp > lowestTemp)
lowestTemp = lowTemp[count].lowTemp;
}
return lowestTemp;
}
double findHighestTemp(Mweather highTemp[], int months)
{
double highestTemp=0;
for(int count=0; count < months; count++){
if (highTemp[count].highTemp < highestTemp)
highestTemp = highTemp[count].highTemp;
count++;
}
return highestTemp;
}