Can someone please clarify what I am suppose to do in this program?

I am confused, and I don't know how to approach this problem. I am confused when the problem is asking me that, "input is taken from a file and the output is sent to a file". I don't know how to change it so I can get rid of user input. Please help. This is the question. Version 3 is based of Version 1.

Version 1 (all interactive). Write a program that reads in the average
monthly rainfall for a city for each month of the year and then reads in
the actual monthly rainfall for each of the previous 12 months. The
program then prints out a nicely formatted table showing the rainfall for
each of the previous 12 months as well as how much above or below
average the rainfall was for each month. The average monthly rainfall is
given for the months January, February, and so forth, in order. To obtain
the actual rainfall for the previous 12 months, the program first asks what
the current month is and then asks for the rainfall figures for the previous
12 months. The output should correctly label the months.
There are a variety of ways to deal with the month names. One
straightforward method is to code the months as integers and then do
a conversion before doing the output. A large switch statement is
acceptable in an output function. The month input can be handled in
any manner you wish, as long as it is relatively easy and pleasant for
the user.
After you have completed this program, produce an enhanced version
that also outputs a graph showing the average rainfall and the actual
rainfall for each of the previous 12 months. The graph should be similar
to the one shown in Display 7.8, except that there should be two bar
graphs for each month and they should be labeled as the average rainfall
and the rainfall for the most recent month. Your program should ask the user whether she or he wants to see the table or the bar graph and then
should display whichever format is requested. Include a loop that allows
the user to see either format as often as the user wishes until the user
requests that the program end.

Version 3 (all I/O with files). This version is like Version 1 except that
input is taken from a file and the output is sent to a file. Since there is
no user to interact with, there is no loop to allow repeating the
display; both the table and the graph are output to the same file. If this
is a class assignment, ask your instructor for instructions on what file
names to use.
This is what I have for version 1. Please help.
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
  #include <iostream>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;

void printMonth(int month);
// PRECONDITION: month holds an integer 1-12
// POSTCONDITION: the corresponding month (Jan, Feb, ..., Dec) has been printed to the standard output.

int main()
{
    double rainfall[12]; //this year's rainfall for each month
    double averages[12]; //average rainfalls for each month
    int currentMonth; //what month is it? 1-based

    // Get the average rainfall for each month, Jan-Dec

    cout << "Please enter average rainfall for each month" << endl;

    for (int i=0; i<12; i++)
        {
        printMonth(i);
        cout << ": ";
        cin >> averages[i];
        }

    // Get the actual rainfall for the previous year; first have to ask what month it is to know what month to start with.
    cout << "What is the number of the current month? Jan=1, Feb=2, etc." << endl;
    cin >> currentMonth;
    cout << "Please enter the rainfall for each month in the previous year" << endl;

    int count = 0;

    for (int month=currentMonth-1; count < 12; month=(month+1)%12, count++)
        { printMonth(month);
        cout << ": ";
        cin >> rainfall[month];

        }
    return 0;
}

// Print table showing avgs, actual rainfalls, and deviations from avg for previous 12 months.

void print_month(std::string name, double rainfall,double averages,int currentMonth)
{
std::cout.setf(std::ios_base::left);
std::cout << setw(20) << name << setw(20)<< rainfall << setw(20) << averages << setw(20)<< currentMonth;

}

 void printMonth(int month)
{
cout.width(8);

switch(month)
        {
        case 0:
        cout << "Jan";
        break;

        case 1:
        cout << "Feb";
        break;

        case 2:
        cout << "March";
        break;

        case 3:
        cout << "April";
        break;

        case 4:
        cout << "May";
        break;

        case 5:
        cout << "June";
        break;

        case 6:
        cout << "July";
        break;

        case 7:
        cout << "Aug";
        break;

        case 8:
        cout << "Sept";
        break;

        case 9:
        cout << "Oct";
        break;

        case 10:
        cout << "Nov";
        break;

        case 11:
        cout << "Dec";
        break;
}
}
Last edited on
Last edited on
1
2
3
4
std::ofstream ofs("test.txt");

ofs << "Hello\n";
std::cout << "Hello\n";

Q: What is the difference between lines 3 and 4?

Line 3 writes text to an ofstream object.
Line 4 writes text to an ofstream object.

A: Different ofstream objects.
Hi there, thanks for both your responses.
I know how to open a file in a program, but I am confused on how to rewrite my program for Version 3.
I thought that I probably only had to output the graph of the rainfall averages into the program, but how can I incorporate my use of arrays?
Topic archived. No new replies allowed.