loops and temp



I need to represent each inputted amount of rainfall into a graph like this.. how would I do this with loops?
Last edited on
I think you need arrays

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

void Display(string month_name,int rainfall)
{
    cout << month_name;
    if(month_name != "February" && month_name != "September" && month_name != "November" && month_name != "December")
    {
        cout << "\t";
    }
    cout << "\t| ";
    for(int i = 0;i < rainfall;i++)
    {
        cout << '\xB2';
    }
    cout << endl;
}

int main(int nNumberofArgs, char* pszArgs[])
{
    int rainfall[16];
    int data;
    char* month_name[16];
    int total_rain = 0;
    int average_rain = 0;
    int highest_rain = 0;
    int lowest_rain = 0;

    month_name[1] = "January";
    month_name[2] = "February";
    month_name[3] = "March";
    month_name[4] = "April";
    month_name[5] = "May";
    month_name[6] = "June";
    month_name[7] = "July";
    month_name[8] = "August";
    month_name[9] = "September";
    month_name[10] = "October";
    month_name[11] = "November";
    month_name[12] = "December";

    cout << "This program collects rainfall data of 1 year and shows" << endl;
    cout << "the total, average, highest, and lowest rainfall." << endl;
    cout << "Enter rainfall in inches." << endl << endl;

    for(int month = 1;month < 13;month++)
    {
        cout << "Enter amount of rainfall for " << month_name[month] << ": ";
        cin >> data;
        total_rain += data;
        rainfall[month] = data;
    }

    average_rain = total_rain / 12;

    cout << endl << "Here is your data set: " << endl << endl;

    for(int relay = 1; relay < 13; relay++)
    {
        cout << "The amount of rainfall for month " << month_name[relay] << " is: " << rainfall[relay] << " inches" << endl;
    }


    for(int i = 1;i < 13;i++)
    {
        Display(month_name[i],rainfall[i]);
    }

    int nmax = 0;
    int nmin = 9999;
    int hm;
    int mm;
    int max_counter = 0;
    int min_counter = 0;
    int key = 0;

    range:
    for(int relay2 = 1;relay2 < 13;relay2++)
    {
        if(key == 1 && rainfall[relay2] == nmax)
        {
            nmax = rainfall[relay2];
            max_counter++;
            hm = relay2;
        }

        if(rainfall[relay2] > nmax)
        {
            nmax = rainfall[relay2];
            hm = relay2;
        }
    }

    for(int relay3 = 1;relay3 < 13;relay3++)
    {
        if(key == 1 && rainfall[relay3] == nmin)
        {
            nmin = rainfall[relay3];
            min_counter++;
            mm = relay3;
        }

        if(rainfall[relay3] < nmin)
        {
            nmin = rainfall[relay3];
            mm = relay3;
        }
    }

    if(key == 0)
    {
        key = 1;
        goto range;
    }


    highest_rain = nmax;
    lowest_rain = nmin;

    cout << endl;
    cout << "The total amount of rainfall for the year is: " << total_rain << " inches." << endl;
    cout << "The average amount of rainfall for the year is: " << average_rain << " inches." << endl;
    if(max_counter == 1)
    {
        cout << "The highest amount of rainfall was in the month of " << month_name[hm] << " with " << highest_rain << " inches." << endl;
    }
    else
    {
        cout << "The highest amount of rainfall was in many months with a record of: " << highest_rain << " inches." << endl;
    }
    if(min_counter == 1)
    {
        cout << "The lowest amount of rainfall was in the month of " << month_name[mm] << " with " << lowest_rain << " inches." << endl;
    }
    else
    {
        cout << "The lowest amount of rainfall was in many months with a record of: " << lowest_rain << " inches." << endl;
    }

    system("PAUSE");
    return 0;
}
Just say you studied on your own.
Given that you understand how to open/close the file and what it will look like.
You will need to loop through the file to get the city name and the 12 pieces of data which can be summed to a total as the loop progresses. Output the city name.(I'm assuming that this monthly rainfall will be presented as double type values) You can then calculate the yearly average and use the round() function to obtain the rounded integer.
Now output a '|' and this number of *'s on the same line as the city name.
Open the next file and repeat.
Maybe I'll just do that for you...So much file work these days...
ok lets see what you can do by research:

here's a starter

use

#include <fstream> // file IO

use

ifstream File ("filename"); // ifstream is input to program from file
// File is just like a class member
// "filename" is the file you want to open

do

File >> variable // Next line in file gets stored into whatever variable

there, that is basically it.
Topic archived. No new replies allowed.