# Need Help with program

This topic is 2083 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Alright, this is homework and I'm having a problem with it running.

SnowData.cpp:

#include <string>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "SnowData.h"

using namespace std;

double SnowData::getAverage()
{
double total;
for(int i=0; i < 6; i++)
{
total = inches[i];
inches[i] = inches[i+1];
}
double average = total/7;
return (average);

}

void SnowData::print()
{
for (int i = 0; i < 7; i++)
{
cout << SnowData::monthname;
cout << " " << date[i] << setw(10) << inches[i] << endl;
}
}
//SnowData snow_data("January", sdate, snow_inches);
/*	int date[7];
//int enddate;
double inches[7];
string monthname;
int startdate;*/
SnowData::SnowData(string mname, int snowdate[7], double snowinches[7])
{
monthname = mname;
for (int i=0; i < 7; i++)
{
date[i] = snowdate[i];
}

for (int i=0; i < 7; i++)
{
inches[i] = snowinches[1];
}
}

void SnowData::sortByInches()
{
for (int j = 0; j < 7; j++)
{
for (int i = 0; i < 7; i++)
{
if (inches[i] > inches[i+1])
{
double temp = inches[i];
int temp1 = date[i];
inches[i] = inches[i+1];
date[i] = date[i+1];
inches[i+1] = temp;
date[i+1] = temp1;
}
}
}
}
void SnowData::sortByDate()
{
for (int j = 0; j < 6; j++)
{
for (int i = 0; i < 6; i++)
{
if (date[i] > date[i+1])
{
double temp = date[i];
int temp1 = inches[i];
date[i] = date[i+1];
inches[i] = inches[i+1];
date[i+1] = temp;
inches[i+1] = temp1;
}
}
}
}


SnowData.h

#include <string>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
class SnowData
{
private:
int date[7];
//int enddate;
double inches[7];
string monthname;
int startdate;
public:
double getAverage();
void print();
SnowData( string, int[7], double[7]);
void sortByDate();
void sortByInches();
};


ClassTester.cpp:

#include<iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
#include "SnowData.h"

const int DAYS_IN_PERIOD = 7;

int main()
{
double snow_inches[7];
int sdate[7];
for(int i=0; i < 7; i++)
{
cin >> snow_inches[i]; // = (double(rand() % 60)) / 3;
}
cout << "Enter the Start Date: ";
cin >> sdate[0];

int x = sdate[0];

for(int i=0; i < 7; i++)
{
sdate[i] = x++;
// = (double(rand() % 60)) / 3;
}
SnowData snow_data("January", sdate, snow_inches);

snow_data.sortByInches();
cout << "--------- Sorted by Inches ---------\n";
snow_data.print();

snow_data.sortByDate();
cout << "---------- Sorted by Date -------\n";
snow_data.print();

cout << "Average snowfall = " << setprecision(2)
<< snow_data.getAverage() << " inches\n";
system("Pause");
return 0;
}


We had to run it like this in three files (2 sources and a header) to illustrate a point. When it runs, it only displays the second element in the inches[] array, and it also averages incorrectly. I feel like it something simple that I'm missing, a > sign that needs to be switched or something, but I can't get it. Btw, there is some useless data in there (like DAYS_IN_PERIOD,) because the method I chose to do things let me enter user data instead of randomizing things and automated the date filling in process. But it was originally my teachers tester class so I had to comment it out.

P.S. If gamedevs policy is not help with homework period, then I apologize in advanced, but I led to understand that help with errors was another thing.

##### Share on other sites

Nevermind, I had a 1 instead of an i and I needed to use += in the average function. I knew it was simple lol.

• 18
• 29
• 11
• 21
• 16