I broke my array reference code

I'm trying to write a program that takes in a average rainfall for the past year, then it will ask for the current rainfall for the year. I also made it so it would show the difference between the years in a total. But now I want to make a display for output that would have the names of the month in my array at the top, under each month will show the avg, act, and difference going across the screen. I wanted to reference as much as I could and I am also trying to do as much with functions because I'm horrible at functions. Any help would be appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;
void avgRainfall (string a[], int numMonths, double &avgTotal);
void actRainfall (string a[], int numMonths, double &totalRain);
void diffRain ( double &totalRain , double &avgTotal );

int main()
{
    const int Months = 12;
    string names[Months]={"January", "February", "March", "April", "May", "June", "July", "August", "September"
                          , "October", "November", "December"};
    double totalRain, avgTotal;




    avgRainfall(names, Months, avgTotal);
    cout << endl;
    actRainfall(names, Months, totalRain);
    diffRain (totalRain , avgTotal );





    return 0;
}

// first function will get the info for the average rainfall
// this program will take in array name and number
// this will reference avgtotal to keep the value in main
void avgRainfall (string a[], int numMonths, double &avgTotal)
{
    double avgRain [12];
     avgTotal = 0;
    for (int i= 0; i < numMonths; i++ )
    {

        cout << "Please enter last years average rainfall "
             << "in inches for " << a[i] << endl;
        cin >> avgRain [i];
        avgTotal += avgRain [i];

    }

    //cout << "Your total average rainfall is " << avgTotal << endl;


}

// this function will get the actual rainfall
// will take in names and months
// this will reference total rain to keep the value accessible in main
void actRainfall (string a[], int numMonths, double &totalRain)
{

    double actRain [12];
    totalRain = 0;

    for ( int i = 0; i < numMonths; i++)
    {
        cout << "Please enter the actual rainfall "
             << "in inches for "   << a[i] << endl;
        cin >> actRain [i];
        totalRain += actRain [i];


    }

    //cout << " you total actual rainfall is " << totalRain << endl;


}

// function will show the difference between the average
// and the actual rainfall
void diffRain ( double &totalRain , double &avgTotal )
{

    cout << "Your total rain fall for the year is: " << totalRain << "inches" << endl;
    cout << "Compared to the average rainfall of last year: " << avgTotal << "inches" << endl;

    double sumTotal;
    sumTotal = (totalRain - avgTotal);
    cout << "The difference in the rainfall between last year and this year is: " << sumTotal << "inches";





}
Hello JayGln,

When I worked with your program I noticed you created arrays in the functions to hold the input for average and actual rain fall. This would work better if you defined the arrays in main and passed them to to the functions. This way the information would be in main and available to be passed to say a display function. As it is when your functions end so do the arrays and your information is lost. So all that work was for nothing.

As for a "Display" function I would start with a piece of paper to figure out how you want the display to work. When you get to the point of writing the display function I would use: std::cout << std::fixed << std::showpoint << std::setprecision(2) along with std::setw() to line everything up on your display.

Knowing that the input functions work I would define the arrays in main and initialize them with some test numbers to save some typing. Comment out the lines that call these input and just work on the "Display" function until you hae that the way you want.

Hope that helps,

Andy
Hello JayGln,

I was playing around with a "display" function. Having no idea what you might want I came up with this:
   January      February        March         April           May          June
  Act    Avg    Act    Avg    Act    Avg    Act    Avg    Act    Avg    Act    Avg
──────────────────────────────────────────────────────────────────────────────────
 1.35   1.25   1.85   1.75   2.10   2.00   1.60   1.50   2.35   2.25   2.00   1.95



     July         August      September       October      November      December
  Act    Avg    Act    Avg    Act    Avg    Act    Avg    Act    Avg    Act    Avg
──────────────────────────────────────────────────────────────────────────────────
 1.15   1.00   1.70   1.65   1.19   1.12   1.39   1.35   1.52   1.45   2.20   2.00

The headings are just simple strings. I adjusted the positions after I printed the numbers.

The numbers are positioned using "std::setw()" for each pair with three spaces between each pair of numbers.

Just an idea for you.

Hope that helps,

Andy
Hello Andy,

Thank you so much for the advice! I was working on the code and did change the array so that it was in main. I will have to change the display, I was able to get a display to work, however it was done poorly in comparison to yours. It also went on to the next line and wasn't matching with the corresponding months to rainfall data.

I appreciate you time on this,
JayGln
Hello JayGln,

Your welcome.

What my output does not show is the months of practice and trial and error to understand how that all works along with the years of looking at other work to put something out that looks that good. With some experience you will be doing that well.

Just to give you an idea this is what I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
size_t index{};  // <--- "size_t" is another name for "unsigned int" and may be spific to VS and Windows.
constexpr size_t NUMSIZE{ 5 };  // <--- So I would only have one place to change the "setw()" size.

std::cout <<
	"   January      February        March         April           May          June" << std::endl <<
	"  Act    Avg    Act    Avg    Act    Avg    Act    Avg    Act    Avg    Act    Avg" << std::endl;
std::cout << std::setfill(static_cast<char>(196)) << std::setw(83) << " " << std::setfill(' ') << std::endl;

std::cout << std::fixed << std::showpoint << std::setprecision(2);

std::cout <<
	std::setw(NUMSIZE) << actRain[index] << "  " << std::setw(NUMSIZE) << avgRain[index] << "  " <<
	std::setw(NUMSIZE) << actRain[index + 1] << "  " << std::setw(NUMSIZE) << avgRain[index + 1] << "  " <<
	std::setw(NUMSIZE) << actRain[index + 2] << "  " << std::setw(NUMSIZE) << avgRain[index + 2] << "  " <<
	std::setw(NUMSIZE) << actRain[index + 3] << "  " << std::setw(NUMSIZE) << avgRain[index + 3] << "  " <<
	std::setw(NUMSIZE) << actRain[index + 4] << "  " << std::setw(NUMSIZE) << avgRain[index + 4] << "  " <<
	std::setw(NUMSIZE) << actRain[index + 5] << "  " << std::setw(NUMSIZE) << avgRain[index + 5] << std::endl;


Hope that helps,

Andy
Topic archived. No new replies allowed.