Arrays

I have an issue with a new code I am writing involving arrays. I have a program where it suppose to ask for the current month and then the rainfall. after it will ask for the previous months and their avg rainfall. Then it will output a table and a graph.It will not the loop that I am looking for to enter the data for the other months. Shockingly, I got the program to compile, however the output is wrong. I have decided to use numbers for the months instead of the actual name. Any advice you can give me will be helpful. The code is below:
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
#include <iostream>
#include <cmath>

using namespace std;

//read in the current month and average rainfall for the previous 12 months
//display a table and graph showing rainfall for each of the 12 months and the difference/gain the average has over the current month

const int MONTHS = 12;

void readInput1 (int nMonths[], double actMonthlyRainfall); //data based on user input
void readInput2 (int nMonths[], double avgMonthlyRainfall);
void scale (int nMonths[], int size);
void graph (const int asterisk[], int previousMonths);
int round (double number);
//void showDifference () //obtain differences between the previous months and current month rainfall
void printAsterisk (double rainfall);


int main ( )
{
	int rain[MONTHS];

	cout << " This program will display the average monthly rainfall in a table or graph." << endl;
	cout << " Please enter the current month by number." << endl;
	cout << " For example if it is July, please enter the number 7." << endl;

	readInput1 (rain, MONTHS);
	readInput2 (rain, MONTHS);
	scale (rain, MONTHS);
	graph (rain, MONTHS);

	return 0;
}

void readInput1 (int nMonths[], double actMonthlyRainfall)
{
	int currentMonth;
	for (int currentMonth = 1; currentMonth <= 0; currentMonth++);
	{
		cout << " Enter Number of Current Month: ";
		cin >> currentMonth;
		cout << " Now Enter the actual rainfall for current month: ";
		cin >> actMonthlyRainfall;
	}
}

void readInput2 (int nMonths[], double avgMonthlyRainfall)
{
	int currentMonth = 1;
	int previousMonths;

	for (int previousMonths = 1; previousMonths <= currentMonth; previousMonths++);
	{
		cout << " Please Enter the previous month: ";
		cin >> previousMonths;
		cout << " Enter Average Rainfall for that Month (non negative numbers only): ";
		cin >> avgMonthlyRainfall;
	}
}
void scale (int nMonths[], int size)
{
	for (int index = 0; index < size; index++)
		nMonths[index] = round(nMonths[index]/100.0);
}

int round (double number)
{
	return static_cast<int>(floor(number + 0.5));
}

void graph (const int asteriskCount[], int previousMonths)
{
	int month = 1;
	cout << "Average Rainfall Figures: " << endl;
	for (int month = 1; month <= previousMonths; month++);
	{
		cout << "Month # " << currentMonth << " ";
		printAsterisk (asteriskCount [month - 1]);
		cout << endl;
	}
}
void printAsterisk (double rainfall)
{
	for (int count = 1; count <= rainfall; count++)
		cout << "*";
}
Well in your readInput1 function, you initialize currentmonth to 1, but you only enter the loop if currentmonth is less than or equal to 0. It is not, so you never enter the loop. That might mean that some of your data is garbage.
Ok, so I re-did everything and it shows up fine. I am a little confuse as to how to create a table and also show the difference between the current month and the previous months. Here is the new code:

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
#include <iostream>
#include <cmath>
using namespace std;
const int NUMBEROFMONTHS = 12;

void readValue (int a[], int currentMonth); //input for the current month
void inputData (int a[], int lastMonth); //the previous month data. Also user can enter the current month rainfall
void scale (int a[], int size);
void graph (const int asteriskCount[], int lastMonth);
void getTotal (int& sum); 
void printAsterisks (int n);

int main ()
{
	int produce[NUMBEROFMONTHS];

	cout << "This program displays the average rainfall for "
		 <<"current month and previous months" << endl;

	cout << "The user will go by number for the months." << endl; 
	cout << "Rainfall data will be enter by whole numbers." << endl;
	cout <<	"For example, the 7th month will be July. The user will only type in the average rainfall for month number 7." << endl;

	readValue (produce, NUMBEROFMONTHS);
	inputData (produce, NUMBEROFMONTHS);
	scale (produce, NUMBEROFMONTHS);
	graph (produce, NUMBEROFMONTHS);
	return 0;
}
void readValue (int a[], int currentMonth)
{
	if (currentMonth = 1)
	{
		cout << endl;
		cout << "You will enter rainfall for current month later on." << endl;
		cout << "Enter current month's number: ";
		cin >> currentMonth;
	}
}
void inputData (int a[], int lastMonth)
{
	for(int month = 1; month <= lastMonth; month++)
	{
		cout << endl;
		cout <<"Enter average rainfall in inches for Month #" << month << endl;
			 getTotal(a[month - 1]);
	}
}
void getTotal (int& sum)
{
	cout << "Append a negative number to go to the next month.\n";

	sum = 0;
	int next;
	cin >> next;
	while (next >= 0)
	{
		sum += next;
		cin >> next;
	}
}

void scale (int a[], int size)
{
	for (int index = 0; index < size; index++);
}
void graph (const int asteriskCount[], int lastMonth)
{
	char ans = 0;
	cout << "Enter 'T' to see table or 'G' for graph: " << endl;
	cin >> ans;
	if ((ans == 'G' || ans == 'g'))
	{
		cout << " Average Rainfall in Inches: ";
		for (int month = 1; month <= lastMonth; month++)
		{
			cout << "Month #" << month << " ";
			printAsterisks(asteriskCount[month - 1]);
			cout << endl;
		}
	}
	else{
		cout << "Table: " << endl;
	}
}
void printAsterisks(int n)
{
	for (int count = 1; count <= n; count++)
		cout << "*";
}

Topic archived. No new replies allowed.