### Debug buggy program

I am tring to figure out why this program wont run????

// Monthly Sales Analysis

#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
void inputData(double[]);
int lowMonth(double[]);
int highMonth(double[]);
double averageSales(double[]);

const char * monthArray[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
const int NUM_MONTHS = 12;

int main()
{
double salesArray[NUM_MONTHS];
int low, high;
double average;

// Input the monthly sales data
inputData(salesArray);

// Find the month with the highest sales
high = highMonth(salesArray);

// Find the month with the lowest sales
low = lowMonth(salesArray);

// Calculate the average monthly sales
average = averageSales(salesArray);

// Display results
cout << "The highest sales were in " << monthArray[high] << " with \$" << setprecision(2) << fixed << salesArray[high] << endl;
cout << "The lowest sales were in " << monthArray[low] << " with \$" << setprecision(2) << fixed << salesArray[low] << endl;
cout << "The average monthly sales were \$" << setprecision(2) << fixed << average << endl;
cin.get();
cin.get();
}

// This functions requests the monthly sales data from the user
void inputData(double sales[])
{
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Please enter the sales in dollars for " << monthArray[i] << " ";
cin >> sales[NUM_MONTHS];
}
return;
}

// This function determines which month had the highest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int highMonth(double sales[])
{
double highest = -1;
int highIndex = -1;

for (int i = 0; i < NUM_MONTHS; i++)

if (sales[i] > highest);
}
Because there is no function lowMonth that takes a double pointer as input, and no function averageSales that takes a double pointer as input.

Write the functions.
Last edited on
thank you I will do that. ;)
Ok maybe I am not putting the lowMonth function and averageSales in the right spot under which paragraph should it go?
A function definition can go absolutely anywhere you like so long as it is not inside another function.
// Monthly Sales Analysis

#include <iostream>
#include <iomanip>
using namespace std;

// Function Prototypes
void inputData(double[]);
int lowMonth(double[]);
int highMonth(double[]);
double averageSales(double[]);

const char * monthArray[] = {"January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"};
const int NUM_MONTHS = 12;

int main()
{
double salesArray[NUM_MONTHS];
int low, high;
double average;

// Input the monthly sales data
inputData(salesArray);

// Find the month with the highest sales
high = highMonth(salesArray);

// Find the month with the lowest sales
low = lowMonth(salesArray);

// Calculate the average monthly sales
average = averageSales(salesArray);

// Display results
cout << "The highest sales were in " << monthArray[high] << " with \$" << setprecision(2) << fixed << salesArray[high] << endl;
cout << "The lowest sales were in " << monthArray[low] << " with \$" << setprecision(2) << fixed << salesArray[low] << endl;
cout << "The average monthly sales were \$" << setprecision(2) << fixed << average << endl;
cin.get();
cin.get();
}

// This functions requests the monthly sales data from the user
void inputData(double sales[])
{
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Please enter the sales in dollars for " << monthArray[i] << " ";
cin >> sales[NUM_MONTHS];
}
return;
}

// This function determines which month had the highest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int highMonth(double sales[])
{
double highest = -1;
int highIndex = -1;

for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] > highest)
{
highest = sales[i];
highIndex = i;
}
}
return highIndex;
}

// This function determines which month had the lowest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int lowMonth(double sales[])
{
double lowest = -1;
int lowIndex = -1;

for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] < lowest)
{
lowest = sales[i];
lowIndex = i;
}
}
return lowIndex;
}

// This function computes the average monthly sales
double averageSales(double sales[])
{
double sum = 0;

for (int i = 1; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
return sum / NUM_MONTHS;
}
In the function in which you input the data, you are storing all the data like this
`cin >> sales[NUM_MONTHS];`
This is off the end of your array (remember that the array elements are numbered zero to NUM_MONTHS - 1) and all writing over each other. I expect you meant:
`cin >> sales[i];`

In your lowmonth function, the benchmark (beginning) low score is minus one. What happens if no month has sales below minus one?
Last edited on
ok I thought I had it but I put 0 instead of -1 on my lowmonth function and now my low month is the same as my high month. ?
Topic archived. No new replies allowed.