Intro C++ project using arrays, etc. Needing guidance in theory and coding

Hi! I'm a novice programmer working on a simple project for my C++ class.
I'm having trouble understanding how to make my program do what I want it to do. Am I on the right track? Or even kind of close?

The full instructions:
Specifications:
1. Use an array to store the sales data. Sales data should be stored as a double.
2. The company has at most 20 locations. (Max capacity of the array)
3. The input file contains a sales report that list the number of stores reporting data and the sales data.
4. Sample Input file format: (samplefile1 and samplefile2)
3
27648.92 234.83 23458.32

The '3' means that this file contains data for three locations. The numbers represent the sales for each location.
5. The main function should be modular so the main tasks must be handled by functions. Note: this is only an outline of the function requirements. You will need to decide on function names, parameter lists and return types on your own.
6. The tasks are:
a. Ask the user for the file name that contains the sales data. Fill the array with the sales data from a file.
b. Total the sales data.
c. Average the sales data.
d. Determine the highest sales in the report.
e. Determine the lowest sales from the report
7. The main function should output the data to the user.
8. Add code to the main function to allow the user to rerun the program.
9. Sample output:
Enter file name: sales.dat
The total sales are $755260.00
The average sales amount is $ 75526.00
The highest sales amount is $124569.00
The lowest sales amount is $ 35678.00

Any help is so appreciated! I am adding a double major in computer science and really love coding - when I understand it....

Best,
Sam

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
#include<fstream>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;

int main( )
{
   int i = 0;
   double number;
   int size = 20;
   double data[size];

   ifstream input;
   string filename;
   cout << "Enter file name: \n";
   cin >> filename;
   
   input.open(filename.c_str());
   while(input.fail())
      {
         cout<< "Incorrect.";
      }

   while (input >> number)
      {
         data[i] = number;
         i++;
      }
      
   for(int i = 0; i < size; i++)
      {
         data[i] = 0;
      }
}  

double getSalesData(int size, double data[])
{
   int i = 0;
   double number;
   
   for(int i = 0; i < size; i++)
   {
      data[i] = 0;
   }
}

double getAvg(int size, double data[])
{
   double total = 0;
   double avg = 0;
   
   for (int i=1; i < size; i++)
   {
      if(data[i] >=0)
      {
         total = total + data[i];
      }
   }
   
   avg = total / data[0];
   cout << avg;
   return avg;
   
}

double getHigh(int size, double data[])
{
   double high = 0;
   
   for(int i = 1; i < size; i++)
   {
      if(data[i] > high)
      {
         high = data[i];
      }
   }
   
  return high; 
  cout << high;
}  
Some suggestions:
1. Make size a constant const int size = 20;
2. As your program curently is it would probably be preferable to use a if statment if the file fails to open
1
2
3
4
if(input.fail())
      {
         cout<< "File failed to open";
      }

3. currently i dont believe your data input will work correctly. In order to bring in the data from the file you need to bring in the first number to the program them that number tells you how many numbers to put in the array. When that runs out you bring in the next number to tell you how many more need to go in the array. from your example you dont want the number 3 in your array you want 27648.92 234.83 23458.32 Sense you may not have 20 stores everytime you should alse have a runging total of the number of stores you put in the array, this will help you later when you find the average.
4. not sure what the function getSalesData is doing because it looks to just fill the arry with 0's
5. your getAvg function is close, you just need to divide total by size not date[0]. should look like avg = total / size;
6. your getHigh function looks good except your use cout after returning

Keep working at it!
Topic archived. No new replies allowed.