Calculating the total and displaying the total.

Im having trouble calculating and displaying the totals of rainy, cloudy, and sunny. Can someone help me here?

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

int main ()
{
    ifstream weather;
    weather.open(rainOrShine.txt");

    char rainyshine[3][30];
        for(int j=0; j<3; j++)
            {
                char ch;
                for(int i=0; i <= 30; i++)
                    {
                        weather.get(ch);
                        if(ch==32)
                        break;

                            else
                                rainyshine[j][i]=ch;
                    }

            }

    string months[]= {"June","July","August"};
    int maxRainy=0;
    string maxRainMonth="";
        cout<<" \t"<<"Rainy"<<"\t"<<"Cloudy"<<"\t"<<"Sunny"<<endl;

            for(int j=0; j<3; j++)
                {
                    int rainy=0;
                    int cloud=0;
                    int sunny=0;

                    for(int i=0; i < 30; i++)
                        {
                            if(rainyshine[j][i]=='C')
                                cloud++;
                            else if(rainyshine[j][i]=='R')
                                rainy++;
                            else if(rainyshine[j][i]=='S')
                                sunny++;

                        }




                        if(maxRainy<rainy)
                            {
                                maxRainy=rainy;
                                maxRainMonth=months[j];

                            }

                        cout<<months[j]<<"\t"<<rainy<<"\t"<<cloud<<"\t"<<sunny<<endl;

                }




                        cout<<endl<<"The Rainiest month is : "<<maxRainMonth<<" with "<<maxRainy<<" amount of rain."<<endl;

    return 0;
} 
So far I can see a missing quote on line 9, and on line 15

for(int i=0; i <= 30; i++)

i will iterate to 30, and 30 would be out of bounds, an array starts from 0, so stop it when it gets to 29

for(int i = 0; i < 30; i++)
Without knowing the format of the input file, I can't comment on whether you're reading the file correctly.

Have you tried displaying the contents of rainyshine to confirm you're reading the file correctly?

Your code won't compile.
Line 9: You're missing an opening ".

You have a problem with your loops at lines 12-25.
line 13: rainyshine is an uninitialized array. i.e. it contains garbage when main() is entered.

Line 18: You're checking for a space and if you read a space, you skipping initializing rainyshine, thereby leaving garbage in that cell.

Last edited on
Topic archived. No new replies allowed.