0

Hi I am working on a class for a weather station that asks a user to input variables and it passes the hours to an array: calculating the values for average, Highs and lows. I got it to work but want to make the array[elements] private. Is it possible to do this?

Here is my code so far. Thank you in advance for any help.

Brian

#include <iostream>
#include <iomanip>

using namespace std;

class WeatherStation
{
public:
    WeatherStation();
    void GetATemperatures(int[], int);
    void DisplayATemperatures( int[], int);
    void arrayCalcs(int[], int);

private:
    static const int aTemps = 24;
    static const int atemps[aTemps];
};

WeatherStation::WeatherStation()
{
    int atemps[aTemps];
}

void WeatherStation::GetATemperatures(int atemps[], int aTemps)
{
    for (int i = 0; i < aTemps; i++ )
    {
        cout << "Please enter the temperature for " << i  << ":00 ";

        while(true)
        {
            cin >> atemps[i];

            if(atemps[i] >= -50 && atemps[i] <= 130)
            {
                break;
            } else {
                cout << "This temperature is not valid\n";
                cout << "Please enter a temperature between -50 and 130 degrees F \n";
                cout << "Please enter a new temperature: ";
            }
        }
    }
}

void WeatherStation::DisplayATemperatures( int atemps[], int aTemps)
{
    cout << setw (5) << "Hour" << setw(24)<< "Temperature \n";
    cout << "\n";

    for (int k = 0; k < aTemps; k++)
    {
        cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl;
    }

    cout <<"\n";
}

void WeatherStation::arrayCalcs(int atemps[], int aTemps)
{
    int sumA = 0;

    double average = 0.0;
    int minA = atemps[0];

    int maxA = atemps[0];

    int lowest = 0;
    int highest = 0;

    //Sum of the AM temps
    for (int kk = 0; kk < aTemps; kk++)
    {
        sumA = sumA + atemps[kk];
    }

    //calculation for average

    average = sumA  / aTemps;

    //Figuring out the Min and Max AM temps

    for (int MM = 0; MM < aTemps; MM++)
    {
        if(minA > atemps[MM])
        {
            minA = atemps[MM];
        }
        else if(maxA < atemps[MM])
        {
            maxA = atemps[MM];
        }

        lowest = minA;
        highest = maxA;
    }


    //Display of the Calculation results
    cout << "This is the average of todays temperatures: " << average <<endl;
    cout <<endl;
    cout << "Todays High temperature is: " << highest <<endl;
    cout <<endl;
    cout << "Todays Low temperature is: " << lowest <<endl;
}

int main()
{
    cout <<"Welcome to the weather station.\n";
    cout <<"Please enter Ferenheit temperatures for calculations: \n";

    WeatherStation alpha;
    alpha.GetATemperatures(atemps, aTemps);
    alpha.DisplayATemperatures(temps, Temps);
    alpha.arrayCalcs(temps,Temps);

    cout << "\n";

    system("pause");
    return 0;
}
6
  • 2
    you've got a lot of local/global naming conflicts in your code. The C++ compiler will always use local, when local and global have the same name. I'm not sure you're aware of that. In any case, that's just an observation. It's not really clear what your question is, can you please clarify? Commented Nov 14, 2011 at 21:09
  • I would avoid using variable names that are so similar like aTemps and atemps. A better name for aTemps would be size or length Commented Nov 14, 2011 at 21:12
  • To clarify, all of the functions use the array- atemps[] and the number of elements in that array defined as aTemps, which equals 24. Commented Nov 14, 2011 at 21:14
  • What I would like to do is define the array[number] ie atemp[aTemp] in a class and pass it to the arguments for the functions below. Does that make sense? Commented Nov 14, 2011 at 21:16
  • Do you have to use arrays? std::vector is the way to go in C++, and it would simplify your code too. Commented Nov 14, 2011 at 21:29

1 Answer 1

1

1) Is the array atemps[]? If so, it's already private... what's the problem?

2) Why is your array class member static? Don't do that without damned good reason (and as this appears to be a homework assignment, I'm almost certain you don't have a damned good reason).

3) Your constructor has a useless line of code in it -- and that's the only line in the function.

4) Your professor will not accept you naming variables atemps and aTemps -- and if they do overlook it, I would be very concerned for the quality of education you're receiving. It's not that the variable names themselves are a big issue, but rather that you're naming them so similarly, as this is a recipe for a maintenance nightmare if it were to happen in real code.

Edit -- based on our comment-chat, here is my suggestion. I have not tried to compile this and I don't claim this is the best (or even a suggested) way to write your program... my suggestion is limited to leaving the data within your object (in a way that has room for growth beyond this question / discussion).

#include <iostream>
#include <iomanip>

using namespace std;

class WeatherStation
{
public:
    WeatherStation();
    void GetATemperatures();
    void DisplayATemperatures();
    void arrayCalcs();

private:
    static const int aTemps = 24;
    int atemps[aTemps];
};

WeatherStation::WeatherStation()
{
}

void WeatherStation::GetATemperatures()
{
    for (int i = 0; i < aTemps; i++ )
    {
        cout << "Please enter the temperature for " << i  << ":00 ";

        while(true)
        {
            cin >> atemps[i];

            if(atemps[i] >= -50 && atemps[i] <= 130)
            {
                break;
            } else {
                cout << "This temperature is not valid\n";
                cout << "Please enter a temperature between -50 and 130 degrees F \n";
                cout << "Please enter a new temperature: ";
            }
        }
    }
}

void WeatherStation::DisplayATemperatures()
{
    cout << setw (5) << "Hour" << setw(24)<< "Temperature \n";
    cout << "\n";

    for (int k = 0; k < aTemps; k++)
    {
        cout << setw (3) << k << ":00" << setw (16) << atemps[k]<<endl;
    }

    cout <<"\n";
}

void WeatherStation::arrayCalcs()
{
    int sumA = 0;

    double average = 0.0;
    int minA = atemps[0];

    int maxA = atemps[0];

    int lowest = 0;
    int highest = 0;

    //Sum of the AM temps
    for (int kk = 0; kk < aTemps; kk++)
    {
        sumA = sumA + atemps[kk];
    }

    //calculation for average

    average = sumA  / aTemps;

    //Figuring out the Min and Max AM temps

    for (int MM = 0; MM < aTemps; MM++)
    {
        if(minA > atemps[MM])
        {
            minA = atemps[MM];
        }
        else if(maxA < atemps[MM])
        {
            maxA = atemps[MM];
        }

        lowest = minA;
        highest = maxA;
    }


    //Display of the Calculation results
    cout << "This is the average of todays temperatures: " << average <<endl;
    cout <<endl;
    cout << "Todays High temperature is: " << highest <<endl;
    cout <<endl;
    cout << "Todays Low temperature is: " << lowest <<endl;
}

int main()
{
    cout <<"Welcome to the weather station.\n";
    cout <<"Please enter Ferenheit temperatures for calculations: \n";

    WeatherStation alpha;
    alpha.GetATemperatures();
    alpha.DisplayATemperatures();
    alpha.arrayCalcs();

    cout << "\n";

    system("pause");
    return 0;
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for this, I will be sure to clean up the naming variables. The member functions need the array[] is atemp[] and the array elements ie [aTemp] for arguments. How do I pass these and call the functions on objects in main to make the program run?
@Brian -- I'm not sure I understand, because your class already contains data within it. Typically in C++, main() would not have direct access to that data; one reason for using an object is to hide it from external code. However, if main() actually owns the array and provides it to the object, you can do that as you've defined GetATemperatures() -- but you're not using the object's variables like this, you're using the callers (main()'s). If your class will eventually have more functions to access the data, you can copy the data locally or save the pointer to the array (and its length).
Ok, I see more of your code now than I did before, and it's a little more clear. If you remove atemps and aTemps from your class, and put them in main(), your code will work. Alternatively, you can leave them in the class and remove them from the function parameters (to just use the class elements) -- which would be a more expected approach towards object oriented programming.
For this example I placed everything in one file to make it easier to read. I plan to eventually seperate the class and main into seperate files with a header to link them. I guess I am confused on how the member functions are called on objects. Dont I need to provide arguments? For example in main I created an object called alpha and called alpha.getATemperatures(); Dont I need to provide arguments?
If you remove the arguments from the GetATemperatures() method, then there are no arguments to provide. When you declare WeatherStation alpha;, you create an object (named alpha) which has members atemps and aTemps created for it... your class methods just refer to them by name directly without using local declarations.
|

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.