2

I have setup a struct array and passed it to a function. I am trying to fill that struct array with input. I initially setup the inputs as:

w->rainFall

but was unable to figure out how it would populate the other subscripts.

I changed it to:

w[counter].rainFall

thinking this would cycle correctly through all of the arrays, but the output data is not giving expected results.

Can someone help me figure out out to populate this struct correctly from within a function?

Full Code Here

#include "pch.h"
#include <iostream>

using namespace std;

struct Weather
{
    int rainFall;
    int highTemp;
    int lowTemp;
    double averageTemp;
};

enum Months
{
    January, February, March, April, May, June, July, August, September, October, November, December
};

void showData(struct Weather* w, int);
void getData(struct Weather* w, int);
void displayMonthName(Months);

int main()
{
    const int SIZE = December+1;

    struct Weather w[SIZE];
    Months thisMonth;

    cout << "Enter The Following Information: " << endl;

    for (thisMonth = January; thisMonth <= December; thisMonth = static_cast<Months>(thisMonth +1))
    {
        int counter = 0;
        displayMonthName(thisMonth);
        cout << " | Month "<< thisMonth+1 << " " << endl;

        getData(w, counter);
        counter++;

    }

    showData(w, SIZE);

}

void showData(struct Weather* w, int SIZE)
{
    for (int i = 0; i <= SIZE; i++)
    {
        cout << w[i].rainFall << endl;
        cout << w[i].highTemp << endl;
        cout << w[i].lowTemp << endl << endl;
    }
}

void getData(struct Weather* w, int counter)
{

    cout << "          Total Rainfall: ";
    cin >> w[counter].rainFall;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    while (!cin || w->rainFall < 0)
    {
        cout << "Rainfall cannot be less than 0 inches" << endl;
        cout << "          Total Rainfall: ";
        cin >> w[counter].rainFall;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "          High Temperature: ";
    cin >> w[counter].highTemp;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    while (!cin || w->highTemp <-100 || w->highTemp >140)
    {
        cout << "High Temperature must be between -100 & +140" << endl;
        cout << "          High Temperature: ";
        cin >> w[counter].highTemp;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cout << "          Low Temperature :";
    cin >> w[counter].lowTemp;
    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    while (!cin || w->lowTemp > w->highTemp || w->lowTemp < -100 || w->lowTemp >140)
    {
        cout << "Low Temperature  must be between -100 & +140 while also being lower than the High Temperature" << endl;
        cout << endl << "          Low Temperature :";
        cin >> w[counter].lowTemp;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
}

void displayMonthName(Months m)
{
    switch (m)
    {
    case January:
        cout << "January";
        break;

    case February:
        cout << "February";
        break;

    case March:
        cout << "March";
        break;

    case April:
        cout << "April";
        break;

    case May:
        cout << "May";
        break;

    case June:
        cout << "June";
        break;

    case July:
        cout << "July";
        break;

    case August:
        cout << "August";
        break;

    case September:
        cout << "September";
        break;

    case October:
        cout << "October";
        break;

    case November:
        cout << "November";
        break;

    case December:
        cout << "December";
        break;
    }
}

1 Answer 1

1

First things first, int counter should be outside of the for loop otherwise you are reinitializing it to 0 and passing 0 to the getData function each time.

int counter = 0; // Should be here.

for (thisMonth = January; thisMonth <= December; thisMonth = static_cast<Months>(thisMonth +1))
    {
        //Not here
        displayMonthName(thisMonth);
        cout << " | Month "<< thisMonth+1 << " " << endl;

        getData(w, counter);
        counter++;

    }

Next, you are iterating through the array 1 too many times. Change <= to <.

void showData(struct Weather* w, int SIZE)
{
    for (int i = 0; i < SIZE; i++)
    {
        cout << w[i].rainFall << endl;
        cout << w[i].highTemp << endl;
        cout << w[i].lowTemp << endl << endl;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Josephff Either way, the above I just described was causing the issue. I just ran some tests on it and it works perfectly fine now.
Thanks Amjad, I just tested it too and it is working on my end. When i went back and redid it, i output that counter and found it wasn't counting and moved it outside the loop. I also found the other part you mentioned here. Thanks a ton for your reply; in no way did i think it was going to be something as simple as that :P

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.