Help with 2 dimension array of char

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
#include<iostream>
using namespace std;


int main(){

	int  size_col = 12;
	int  size_row = 12;
	char months_array[12][12] = 
	{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October"
	, "November", "December" };

	int const rain_size = 12;
	double rainfall_array[rain_size];
	int count1, count2;
	double sum_total = 0, average = 0;
	char highest, lowest;
	double check_low, check_high;


	for (count1 = 0; count1 < size_col; count1++){
		cout << "Enter the rainfall for " << months_array[count1]<< endl;
		cin >> rainfall_array[count1];
		if (rainfall_array[count1] < 0){
			do {
				cout << "Invalid input, enter a positive number" << endl;
				cin >> rainfall_array[count1];
			} while (rainfall_array < 0);
		}

	}

	check_low = rainfall_array[0]; //to compare the first array with others
	check_high = rainfall_array[0];
	for (count2 = 0; count2 < rain_size; count2++){
		sum_total = rainfall_array[count2] + sum_total;
		average = (sum_total / rain_size);
		
		if (rainfall_array[count2] < check_low){
			lowest = months_array[count2];    //This is the problem
		}

		if (rainfall_array[count2] > check_high){
			highest = months_array[count2];   //and here
		}
	}
	
	cout << "The total rainfall of the year is " << sum_total << endl;
	cout << "The average rainfall of the year is " << average << endl;
	cout << "The month with the highest rain fall is " << highest << endl;
	cout<< "The month with the lowest rain fall is " << lowest << endl;

	system("pause");
	return 0;
}



Hello everyone,
I am having problems with passing a block of a char to a variable,
so i could print it out.
It gives me an error: value of type *char cannot be assigned to a entity type char.

It you could help me it would be appreciated, thanks.
lowest and highest are each defined as a single character. But you want them to contain a string of several characters.

The easiest solution is to use a std::string.
 
#include <string> 

1
2
3
string lowest;

lowest = months_array[count2];


Alternatively, keep using character arrays, but then you must use the strcpy() function,
 
#include <cstring> 

1
2
3
char highest[12];

strcpy(highest, months_array[count2] );

http://www.cplusplus.com/reference/cstring/strcpy/

In C++ std::string is preferred, generally it's both safer and easier to use than a plain character array.
Last edited on

You could make it a one-dimensional array of char* :

1
2
3
char* months_array[] = 
	{ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October"
	, "November", "December" };
Last edited on
Please try to keep the tone civil. Note that there's a difference between writing code which will merely compile without errors, as compared to code which executes and gives the correct results.

@jcra1992
There are a couple of other changes required in order to get the correct output. At lines 40 and 44 where the values of lowest and highest are changed, the variables check_low and check_high should also be updated with the value for the current month. Similarly, both lowest and highest should be assigned an initial value at line 34, or else the output will not be correct if either the highest or lowest rainfall happens to occur in the first month.
Last edited on
Let's say the highest rainfall occurs in December. What will be the value of months_array[count2][12] in that case?
Hint 1: how many letters does the word "December" have.
Hint 2: what is the valid range of subscripts for an array of 12 elements?
Last edited on
closed account (48T7M4Gy)
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
#include<iostream>
using namespace std;


int main()
{
    int  size_col = 12;
    int  size_row = 12;

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

    int const rain_size = 12;
    double rainfall_array[rain_size];
    int count1, count2;
    double sum_total = 0, average = 0;
    char* highest;
    char* lowest;
    double check_low, check_high;


    for (count1 = 0; count1 < size_col; count1++)
    {
        bool haveError = false;
        do
        {
            cout << "Enter the rainfall for " << months_array[count1]<< endl;
            cin >> rainfall_array[count1];

            if (rainfall_array[count1] < 0)
            {
                haveError = true;
                cout << "Invalid input, enter a positive number" << endl;
            }
            else haveError = false;
        } while ( haveError == true);
    }

    check_low = rainfall_array[0]; //to compare the first array with others
    check_high = rainfall_array[0];

    int lowestMonthNo= 0;
    int highestMonthNo=0;

    for (count2 = 0; count2 < rain_size; count2++)
    {
        sum_total = rainfall_array[count2] + sum_total;
        average = (sum_total / rain_size);

        if (rainfall_array[count2] < check_low)
        {
            check_low = rainfall_array[count2];
            lowestMonthNo = count2;
        }

        if (rainfall_array[count2] >= check_high)
        {
            check_high = rainfall_array[count2];
            highestMonthNo = count2;
        }
    }

    cout << "The total rainfall of the year is " << sum_total << endl;
    cout << "The average rainfall of the year is " << average << endl;
    cout << "The month with the highest rain fall is " << months_array[highestMonthNo] << endl;
    cout << "The month with the lowest rain fall is " << months_array[lowestMonthNo] << endl;

    return 0;
}
Last edited on
Nice work, Arthur. Not only do you report Chervil - who's been nothing but civil and helpful on this thread - but you also delete your own insulting posts attacking him, to hid the evidence of your behaviour.

You're a class act.
I'll take just some bits:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
check_low = rainfall_array[0];
int lowestMonthNo= 0;
// assert(  rainfall_array[lowestMonthNo] ==  rainfall_array[0] )
// two variables to track the lowest. One is more useful than the other.

for ( count2 = 0; count2 < rain_size; count2++ )
  {
    sum_total = rainfall_array[count2] + sum_total;

    average = (sum_total / rain_size);

    // assert( check_low == rainfall_array[lowestMonthNo] )
    if ( rainfall_array[count2] < check_low ) // rainfall_array[count2] < rainfall_array[lowestMonthNo]
     {
        check_low = rainfall_array[count2];
        lowestMonthNo = count2;
     }
  }
// average can be calculated after the loop 

Topic archived. No new replies allowed.