0

I'm completely new to Stack OverFlow so excuse me if I'm breaking any rules or incorrectly asking a question (I would also appreciate advice on how to properly ask questions on Stack overflow).

Anyways so I get this error

*"argument of type 'data *' is incompatible with parameter of type 'data '."

when I call the user-defined function weather in int main(). I would appreciate it if someone could take a look at my code and guide me to what's causing the error. Just so you know, I am currently taking a programming fundamentals intro course and I have not learned about classes and pointers yet.

Thanks!

#include <iostream>
#include <string>


using namespace std;

const int SIZE = 2;

void weather(struct data array[], int SIZE, string months[]);

int main()
{
string months[12] = { "January", "February", "March", "April", "May", "June"
                "July", "August", "September", "October", "November", "December" };


struct data {
    double totalRainfall;
    double highTemp;
    double lowTemp;
    double avgTemp;
};

data annualWeather[SIZE]; 

//Asks user for total rainfall, high and low temperature, and calculates
//avg temperature of each month
for (int i = 0; i < SIZE; i++)
{
    cout << "What was the total rainfall in " << months[i]
        << "?" << endl;
    cin >> annualWeather[i].totalRainfall;

    //Prompts user for temperature high
    do
    {
        cout << "What was the temperature high in " << months[i]
            << "?" << endl;
        cin >> annualWeather[i].highTemp;

    } while (annualWeather[i].highTemp > 140);

    //Prompts user for temperature low
    do
    {
        cout << "What was the temperature low in " << months[i]
            << "?" << endl;
        cin >> annualWeather[i].lowTemp;
    } while (annualWeather[i].lowTemp < -100);

    annualWeather[i].avgTemp = (annualWeather[i].highTemp + annualWeather[i].lowTemp) / 2;
}

**weather(annualWeather, SIZE, months); <--THIS IS WHERE I SEE ERROR**

return 0;
}

void weather(struct data array[], int SIZE, string months[])
{
double avgRainFall = 0;
double totalAnnualRainFall = 0;
double annualHigh = 0;
double annualLow = 0;
double avgMonthlyTemp = 0;

//Calculates sum annual rain fall, total annual rain fall, and total average monthly temperature
for (int i = 0; i < SIZE; i++)
{
    avgRainFall += annualWeather[i].totalRainfall;
    totalAnnualRainFall += annualWeather[i].totalRainfall;
    avgMonthlyTemp += annualWeather[i].avgTemp;
}

//Calculates average annual rain fall and average annual monthly temperature
avgRainFall = avgRainFall / SIZE;
avgMonthlyTemp = avgMonthlyTemp / SIZE;

//Selection Sort of annualWeather[].highTemp
for (int index = 0; index < SIZE - 1; index++)
{
    //Find location of smallest element
    int smallestIndex = index;
    for (int location = index + 1; location < SIZE; location++)
        if (annualWeather[location].highTemp < annualWeather[smallestIndex].highTemp)
            smallestIndex = location;
    //Swap smallest element of annualWeather[].highTemp to front
    int tempSales = annualWeather[smallestIndex].highTemp;
    annualWeather[smallestIndex].highTemp = annualWeather[index].highTemp;
    annualWeather[index].highTemp = tempSales;

    //Swap smallest element of months[] to front
    string tempMonths = months[smallestIndex];
    months[smallestIndex] = months[index];
    months[index] = tempMonths;

}

//Displays average monthly rainfall, total annual rainfall, annual high, annual low,
//And average of the monthly temperature averages
cout << "The average monthly rainfall is " << avgRainFall << "." << endl
     << "The total rainfall for the year is " << totalAnnualRainFall << "."
     << endl << "The high temperature of the year is " << annualHigh << " during "
     << months[0] << endl << "The low temperature of the year is " << annualLow << " during "
     << months[2] << endl << "The average temperature of all the monthly averages is "
     << avgMonthlyTemp << ".";

cin.get(); cin.get();
}

2 Answers 2

1

The struct data declared in your main function is different from the struct data used in the prototype of your weather function.

Move the definition of struct data out of main to the global namespace, somewhere before you use it to declare weather.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for answering! I moved the struct data definition out of int main() and the user defined function weather worked. Thanks!
0

For the first please specified your personal data types in global space. Your problem not in sending array to function. Problem inside your function you send array to them with name array, and inside trying to access them by name from main. Just rename your function parameter.

void weather(Data array[], int SIZE, string months[])
{
    double avgRainFall = 0;
    double totalAnnualRainFall = 0;
    double annualHigh = 0;
    double annualLow = 0;
    double avgMonthlyTemp = 0;

    //Calculates sum annual rain fall, total annual rain fall, and total average monthly temperature
    for (int i = 0; i < SIZE; i++)
    {
        avgRainFall += array[i].totalRainfall;
        totalAnnualRainFall += array[i].totalRainfall;
        avgMonthlyTemp += array[i].avgTemp;
    }

    //Calculates average annual rain fall and average annual monthly temperature
    avgRainFall = avgRainFall / SIZE;
    avgMonthlyTemp = avgMonthlyTemp / SIZE;

    //Selection Sort of annualWeather[].highTemp
    for (int index = 0; index < SIZE - 1; index++)
    {
        //Find location of smallest element
        int smallestIndex = index;
        for (int location = index + 1; location < SIZE; location++)
            if (array[location].highTemp < array[smallestIndex].highTemp)
                smallestIndex = location;
        //Swap smallest element of annualWeather[].highTemp to front
        int tempSales = array[smallestIndex].highTemp;
        array[smallestIndex].highTemp = array[index].highTemp;
        array[index].highTemp = tempSales;

        //Swap smallest element of months[] to front
        string tempMonths = months[smallestIndex];
        months[smallestIndex] = months[index];
        months[index] = tempMonths;

    }

    //Displays average monthly rainfall, total annual rainfall, annual high, annual low,
    //And average of the monthly temperature averages
    cout << "The average monthly rainfall is " << avgRainFall << "." << endl
        << "The total rainfall for the year is " << totalAnnualRainFall << "."
        << endl << "The high temperature of the year is " << annualHigh << " during "
        << months[0] << endl << "The low temperature of the year is " << annualLow << " during "
        << months[2] << endl << "The average temperature of all the monthly averages is "
        << avgMonthlyTemp << ".";

    cin.get(); cin.get();
}

1 Comment

Thanks for your response! I just moved my struct data definition out of int main and it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.