Cannot find error in program

I am new to C++ and was debugging the program below. I believe the error is in the if statements when finding the lowest sales data, but I can't seem to get it to work. I was wondering if you guys had any suggestions on what to do on this.

Thanks!

#include "stdafx.h"

//This program inputs monthly sales data and displays the high, low and average sales. This program was debugged and now runs correctly.
//Frankie Romero, Intro. to Programming
// 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[i];
}
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 = 0; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
return sum / NUM_MONTHS;
}
This function

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

is incorrect. For positive values of array's elements it will always return -1.

First of all it is better to redeclare it the following way

int lowMonth( const double sales[], int n );
that is that it has two parameters: array and its size.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int lowMonth( const double sales[], int n )
{
   int lowIndex = 0;
 
   for ( int i = 1; i < n; i++ )
   {
      if ( sales[i] < sales[lowIndex] )
      {
          lowIndex = i;
      }
   }
 
   return lowIndex;
}


Try to rewrite functions highMonth and averageSales the same way.
Last edited on
The thought behind -1 is the intern value (111....111) so it is the largest number compared to an unsigned int. That might work.

Try

int minusone = -1;
unsigned int zero = 0;
if(minusone > zero)
cout << minusone << " is larger than " << zero << endl;


But you might want to check sales[0] also. So better change int i = 1; to int i = 0;
Those suggestions caused errors in the program. Anytime you change the lowest or lowIndex, say to 0 (like the example below), it returns only the number that is stored in array 0. I need it to return the lowest number in the 12 arrays.

int lowMonth(double sales[])
{
double lowest = 0;--- Error could be here
int lowIndex = 0;----- Here

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

The highMonth is basically the same thing, just with the less than/greather than symbol the opposite way, and in my C++ book they say to find the highest and lowest of a numeric value, you use the high and low functions the same to return the highest and lowest array. I did some debugginig on it (not very good at it) and it said the possible error was at the if (sales[i] < lowest), but I'm not sure why that would be? Thank you guys for helping me figure this out, I appreciate all the responses.
Last edited on
I showed your invalid code and also showed the correct function. What is the problem?! Are you capable to read what I wrote to you?! It is not my suggestion that causes an error, it is you who cannot even to rewrite correctly what was suggested by me.
Last edited on
I never meant to say you were wrong. I said that when i tried to code your suggestion I received errors. I know it was my error in not coding it right, i was just looking for more insight on it, never meant to offend you. This is what I did to resolve the coding issue, just depends on if they accept this way or not.

int lowMonth(double sales[])
{
double lowest;
int lowIndex = 0;

lowest = sales[0];
for (int i = 1; i < NUM_MONTHS; i++)
{
if (sales[i] < lowest)
{
lowest = sales[i];
lowIndex = i;
}
}
return lowIndex;
}
Last edited on
Topic archived. No new replies allowed.