Debugging A C++ Program

Hi,I have this assignment for my programming class and I am having trouble with it. It is intended to be a program that inputs monthly sales data and displays the high, low and average sales. Unfortunately, it doesn't work. There are three logical errors in it. Can anyone help me out, please. I'd really appreciate it.
Here is the code:

#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;
}

you have a hell of a lot of code here. I've looked it over, and see a few things. First, you need to make sure that in your output statements, you have all the functions declared properly. Also, it's my preference to use void functions when you can. it makes the main function a little easier to read, and all your cout statements can be broken up into their respective functions. also, you use monthArray[high]/[low] but your functions are called lowMonth[]/highMonth[], and i dont even see a function that could be called salesArray. Like i said, consier using void functions. take a step away, and re look at all your code and function names.
Unfortunately, it doesn't work. There are three logical errors in it.

It would really help if you told us what you think these logical errors are.

Does the code compile without warnings and errors? If not please post the complete error messages.
Use code tags please

1
2
3
4
5
6
7
8
9
10
11
12
int highMonth(double sales[])
{
	int highIndex = 0; 
	for (int i = 1; i < NUM_MONTHS; i++)
	{
		if (sales[i] > sales[highIndex])
		{
			highInedx=i;
		}
	}
	return highIndex;
}


This is to find index of highest sales in the sales array.
Similar thing is for finding lowest sales with simple changes
1
2
3
4
5
6
7
8
9
10
11
// 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;
}


Why is the loop starting at 1? You will miss the first month. Same for vichu8888 code - A copy / paste error?

Speaking of debuggers - do you know how to use one? If you are using an IDE, it should be pretty easy.

You can set breakpoints, have a watch list of variable values, step through the code 1 line at a time. Only problem is the code has to compile first though. :+D
No ideasman, its not a typo. I have assigned the first element index to high index before for loop. Suppose if the first element is the highest elemen then index zero will be returned.

In simple words, for me first element is the highest element. With that im comparing with others
Last edited on
@vichu8888

Right Oh. ;D
If you don't have a debugger, the addition of a few cout statements will help enormously.

For example, you might add a simple loop like this in main():

1
2
    for (int i = 0; i < NUM_MONTHS; i++)
        cout << i << " " << monthArray[i] << " " << salesArray[i] << endl;

... purely to assist with debugging. This will display the values that have been stored in the array.

You can add extra cout statements anywhere in the program, in order to monitor the values of variables in the areas which don't seem to behave correctly.
Topic archived. No new replies allowed.