Having trouble finding my errors.

I am trying to get this programming running to turn in for an assignment however I am banging my head against the wall at this point trying to figure out why I cannot get it to work properly... Any help would be greatly appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Monthly Sales Analysis

#include <iostream>
#include <iomanip>
#include <cmath>
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 sales[NUM_MONTHS];
	int low, high;
	double average;

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

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

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

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

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

	cin.clear();
	cin.ignore(255, '\n');
	cin.get();
    return 0;
}

// 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[])
{
	int highest;
	highest = sales[0];
	for (int i = 0; i < NUM_MONTHS; i++)
	{
		if (sales[i] > highest)
		    highest = sales[i];
	        

	}	
	return highest;
}

// 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[])
{
	
	int lowest;
	lowest = sales[0];
	
	for (int i = 0; i < NUM_MONTHS; i++)
	{
			if (sales[i] < lowest)
		    lowest = sales[i];
			
	}
			return lowest;
}

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

	for (int i = 0; i < NUM_MONTHS; i++)
	{
		sum += sales[i];
	}
	return average = sum / NUM_MONTHS;
}
Last edited on
Please use code tags <>
@Angeljruiz: Fixed. Sorry my first post on here...
No problem :P Also whats a matter with it?
The main problem is that the functions for high and low are returning a 0 value. I believe it stems from a problem getting the CIN values into the sales[] but cannot seem to fix it.
When you get input you are always putting it into sales[NUM_MONTHS] when you should be putting sales[i]
Makes sense and I have tried that but when I do that I then get an Unhandled exception at line 38.
Its because in you high function you are setting highest to the highest dollars made, not the actual position. So lets say the highest sales was 5000, when you try to print it it would be sales[5000].
Instead in your high and low functions set highest to i
Last edited on
Ok I see. I am making some headway now, Thank you much for your feedback, it is truly appreciated.
I think you need to use this one... <>
There is no <> operator in c++. You shouldnt give advice if you have no idea what you're talking about
@Edmundce (1)

Don't do pointless posts like this - you could get reported to admin, and get your account closed. I am noting all the user names for those who make posts like this.
Topic archived. No new replies allowed.