How do I turn this into an array?

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
#include <iostream>

using namespace std;

int main()
{
	double monthOne, monthTwo, monthThree, monthFour, monthFive, monthSix, monthSeven, monthEight, monthNine, monthTen, monthEleven, monthTwelve, total, average;

	cout << "This program will give you the total spent and the monthly average cost of your water bills for the last year.\n";
	cout << "Enter the cost of your water bill from Month 1.\n";
	cin >> monthOne;
	cout << "Enter the cost of your water bill from Month 2.\n";
	cin >> monthTwo;
	cout << "Enter the cost of your water bill from Month 3.\n";
	cin >> monthThree;
	cout << "Enter the cost of your water bill from Month 4.\n";
	cin >> monthFour;
	cout << "Enter the cost of your water bill from Month 5.\n";
	cin >> monthFive;
	cout << "Enter the cost of your water bill from Month 6.\n";
	cin >> monthSix;
	cout << "Enter the cost of your water bill from Month 7.\n";
	cin >> monthSeven;
	cout << "Enter the cost of your water bill from Month 8.\n";
	cin >> monthEight;
	cout << "Enter the cost of your water bill from Month 9.\n";
	cin >> monthNine;
	cout << "Enter the cost of your water bill from Month 10.\n";
	cin >> monthTen;
	cout << "Enter the cost of your water bill from Month 11.\n";
	cin >> monthEleven;
	cout << "Enter the cost of your water bill from Month 12.\n";
	cin >> monthTwelve;

	total = monthOne + monthTwo + monthThree + monthFour + monthFive + monthSix + monthSeven + monthEight + monthNine + monthTen + monthEleven + monthTwelve;
	average = total / 12;
	cout << "The total spent over the last year was: " << total << endl;
	cout << "The average spent each month for the year was: " << average << endl;

	return 0;
}


Not asking for full out answers, I just need a push in the right direction.

I tried double month for the variable [12]= months. I might be way off, but any help would be awesome. Thanks.
Why do you need an array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

int main()
{
   double cost, total;
   for ( int m = 1; m <= 12; m++ )
   {
      cout << "Enter the cost of your water bill from month " << m << ": ";
      cin >> cost;
      total += cost;
   }
   cout << "The total spent over the last year was: " << total << '\n';
   cout << "The average spent each month for the year was: " << total / 12 << '\n';
}
Oh man, I think you gave me the whole answer haha. I was looking for tips on how to do it. Thank you though.
I was trying something new and wanted to see how would I be able to do this. I saw on a site that you can do array with double.
Last edited on
Using a double array to hold each month's payment, and a std::string array for the name of the months (makes for neater output):
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
#include <iostream>
#include <string>

int main()
{
   const unsigned NUM_MONTHS { 12 };

   std::string months[NUM_MONTHS] { "January", "February", "March", "April",
                                    "May", "June", "July", "August",
                                    "September", "October", "November", "December" };

   double payments[NUM_MONTHS] { };

   double totalPayment { };

   for (unsigned month { }; month < NUM_MONTHS; ++month)
   {
      std::cout << "Enter your water bill cost for " << months[month] << ": ";
      std::cin >> payments[month];

      totalPayment += payments[month];
   }

   std::cout << "Total water bill: " << totalPayment << '\n';

   double average { totalPayment / NUM_MONTHS };

   std::cout << "Average payment: " << average << '\n';
}

Enter your water bill cost for January: 12.5
Enter your water bill cost for February: 8.79
Enter your water bill cost for March: 15.5
Enter your water bill cost for April: 10
Enter your water bill cost for May: 16.8
Enter your water bill cost for June: 35.88
Enter your water bill cost for July: 45
Enter your water bill cost for August: 25
Enter your water bill cost for September: 18
Enter your water bill cost for October: 9
Enter your water bill cost for November: 15
Enter your water bill cost for December: 15
Total water bill: 226.47
Average payment: 18.8725

Sometimes it is easier to just throw out a full code example than do piece-meal hints.

Using an array (or a C++ standard library container such as std::vector) for something this simple can be a bit of over-kill, as lastchance said.

If'n you wanted to use std::vector instead of regular arrays #include <vector>; and change the two arrays to use vectors:
8
9
10
11
12
   std::vector<std::string> months { "January", "February", "March", "April",
                                     "May", "June", "July", "August",
                                     "September", "October", "November", "December" };

   std::vector<double> payments(NUM_MONTHS);

The rest of the code will be the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	const unsigned NUM_MONTHS {12};

	const char* const months[NUM_MONTHS] {"January", "February", "March", "April",
					 "May", "June", "July", "August",
					 "September", "October", "November", "December"};

	double totalPayment { };

	for (const auto& mon : months) {
		double payment {};

		std::cout << "Enter your water bill cost for " << mon << ": ";
		std::cin >> payment;

		totalPayment += payment;
	}

	std::cout << "\nTotal water bill: " << totalPayment;
	std::cout << "\nAverage payment: " << totalPayment / NUM_MONTHS << '\n';
}

Last edited on
Topic archived. No new replies allowed.