I am having problems with the array because it crashes and it shows 0 for all months.
Below is my code
#include <iostream> // Setup standard output.
#include <fstream> // Required for input / outputting files.
#include <iomanip> // Required for numeric formatting function.
#include <string> // Required for strings.
using namespace std;
const int MONTH = 3;
const int DAY = 30;
int main()
{
string weather[MONTH][DAY] = {"June", "July", "August"};
int totalRainy,
totalSunny,
totalCloudy;
ifstream datafile;
// Name of program.
cout << " Rain or Shine\n";
cout << "---------------\n\n";
datafile.open("RainOrShine.dat");
if (!datafile)
cout << "Error opening data file.\n";
else
{
cout << "Weather Report\n\n";
for (int months = 0; months <= MONTH; months++)
{ totalRainy = 0,
totalSunny = 0,
totalCloudy = 0;
cout << weather[months][DAY];
datafile >> weather[months][DAY];
if ('R')
totalRainy += weather[months][DAY];
if ('S')
totalSunny += weather[months][DAY];
if ('C')
totalCloudy += weather[months][DAY];
cout << endl;
cout << " Total rainy days: " << totalRainy << endl;
cout << " Total sunny days: " << totalSunny << endl;
cout << " Total cloudy days: " << totalCloudy << endl << endl;
}
datafile.close();
for (int months = 0; months <= MONTH; months++)
{ totalRainy += weather[months][DAY];
totalSunny += weather[months][DAY];
totalCloudy += weather[months][DAY];
}
int highest = 0;
for (int count = 1; count < weather[MONTH][DAY]; count++)
{
if (weather[count] > weather[highest])
highest = count;
}
// Displays the total of all 3 months.
cout << "Total rainy days for all 3 months: " << totalRainy << endl;
cout << "Total sunny days for all 3 months: " << totalSunny << endl;
cout << "Total cloudy days for all 3 months: " << totalCloudy << endl;
cout << "The month with the most rainy days is " << highest << endl;
}
// Stops the program from closing automatically.
system("pause");
return 0;
}
This post has been edited by turner2108: 28 June 2009 - 03:23 PM

New Topic/Question
Reply




MultiQuote





|