I need some help on what I'm working on right now I'm not asking for an answer maybe some help or a push in the right direction. Here is what I'm asked to do
"Convert your program to run from an input file instead of console input." I'm suppose to have a .txt file thats suppose to have numbers for the average rainfall and input it into what I have so far. I really don't understand Inputs/Outputs of text files and all that I tried to look up help from sites with guides that didnt really help. Can someone please help me, here is what I have so far:
#include <iostream>
#include <fstream>
#include <cstdlib>
using std::ifstream;
using std::ofstream;
using std::cout;
using std::endl;
void turnToMonth(int month);
int main()
{
ifstream inStream;
ofstream outStream;
double rainfall[12];
double averages[12];
int currentMonth;
char tableOrGraph;
char yesOrNo;
inStream.open("currentrainfall.txt");
inStream.open("previousfainfall.txt");
outStream.open("outfile.txt");
cout << "Please enter average rainfall for each month" << endl;
for (int i=0; i<12; i++) {
turnToMonth(i);
cout << ": ";
cin >> averages[i]; }
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++) {
turnToMonth(month);
cout << ": ";
cin >> rainfall[month]; }
cout << "\nWould you like to see a table of the results\n"
<< "or a graph? Type 't' for the table or 'g' for the graph.";
cin >> tableOrGraph;
do {
if (tableOrGraph == 't') {
cout << "Rainfall table\n\n"
<< "Month\t\tRainfall (prev. 2 months)\tDeviation\n";
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++) {
turnToMonth(month);
cout << "\t\t" << rainfall[month] << "\t\t\t";
if (rainfall[month] > averages[i])
else if (rainfall[month] < averages[i])
cout << averages[i] - rainfall[month] << endl;
else
cout << "0\n"; } }
else if (tableOrGraph == 'g') {
cout << "\nRainfall graph\n\n"
<< "Month\n";
for (int month=currentMonth-1, i=0; i < 12; month=(month+1)%12, i++) {
cout << endl;
turnToMonth(month);
cout << "\nAverage rainfall: ";
for (i; averages[i] > 0; averages[i]--)
cout << "*";
cout << "\nActual rainfall: ";
for (i; rainfall[month] > 0; rainfall[month]--)
cout << "*"; } }
cout << "\n" << "Would you like to see another table or graph? 'y' for yes and 'n' for no.";
cin >> yesOrNo; }
while (yesOrNo == 'y');
return 0; }
void turnToMonth(int month) {
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; } }