C++ Array Project

Hello,
I have recieved a project for my computer science class in which i am having a little trouble completing. I have alot of the code completed and it compliles, but it won't operate the way it is suppose to. Any help is much appreciated.

Create a new project in Code Blocks for a program that will:
Read in a file of sales dollar amounts (doubles) and store them in an array.
The maximum number of amounts that the file could
contain is 20, but it could have less, and
zero (0.00) marks the end of the valid data.

Your program should read in the data and :
Determine the smallest amount in the file.
Determine the largest amount in the file
Sort the amounts from lowest to highest.

2. The program will output to a file the following items:
"The number of sales amounts in the file is "
"The lowest sales amount is "
"The highest sales amount is "
"Here is the list of amounts sorted from lowest to highest:"

3. Create and use a test file with the following values in the order that they appear below:
29.95, 3.55, 34.70, 12.34, 2.67, 33.89, 24.99, 18.45, 4.99, 9.95, 0.00

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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122

#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

void FillArray(double a[], int arraySize, int number_used);
void Sort(double a[], int number_used);
int IndexOfSmallest(const double a[], int start_index, int number_used);
void swap_values(double& v1, double& v2);
void Output();

ifstream InStream;
ofstream OutStream;

int number_used;
double Arrow, Sorted, Small, Large;
double Array[20];

int main()

{
    int arraySize = 20;

    InStream.open("salesAmounts.txt");
    if (InStream.fail())
    {
    cout << "Input file opening failed.\n";
    exit(1);;
    }

    OutStream.open("fileStauffer.txt");
    if (OutStream.fail())
    {
    cout << "Input file opening failed.\n";
    exit(1);
    }

    FillArray(Array, arraySize, number_used);
    Sort(Array, number_used);
    Output();

 InStream.close( ); // Close Files Outta Loop
 OutStream.close( );

 cout << "The program has completed successfully.\n";
 return 0;

 } // End Main

 //Functions
 void FillArray(double a[], int arraySize, int number_used)
 {
  int Next, index = 0;
  InStream >> Next;
  while ((Next >= 0) && (index < arraySize))
  {
  a[index] = Next;
  index++;
  InStream >> Next;
  }
  number_used = index;
 }

 void Sort(double a[], int number_used)
 {
  int IndexNextSmallest;

    for (int index = 0; index < number_used - 1; index++)
    {
        IndexNextSmallest = IndexOfSmallest(a, index, number_used);
        swap_values(a[index], a[IndexNextSmallest]);
    }
 }

int IndexOfSmallest (const double a[], int start_index, int number_used)
{
 int index_of_min = start_index;

 int min = a[start_index];

 for (int index = start_index + 1; index < number_used; index++)
    if (a[index] < min)
   {
    min = a[index];
    index_of_min = index;
   }

   return index_of_min;
}

void swap_values(double& v1, double& v2)
{
    double temp;

    temp = v1;

    v1 = v2;

    v2 = temp;
}

void Output() // Output Section

{
 OutStream.setf(ios::fixed);

 OutStream.setf(ios::showpoint);

 OutStream.precision(2);

 OutStream << "The Number Of Sales Amounts In The File Is " << number_used << endl;

 OutStream << "The Lowest Sales Amount Is $ " << Array[0] << endl;

 OutStream << "The Highest Sales Amount Is $" << Array[number_used] << endl;

 //  this should be a loop ouputing all values
 OutStream << "List Of Sorted Numbers " << Sorted << endl << endl;
}
Line 117: Subscript should be number_used-1.

Line 120: Sorted is never set. What you want to do here is simply iterate through Array, outputting one number at a time.

but it won't operate the way it is suppose to.

That's not very helpful. Please describe what it is or is not doing.
I got in contact with my professor and he pointed out a couple small errors i edited and that the output needs work, what that means idk, but it has to do with //this should be a loop outputing all values
Last edited on
Topic archived. No new replies allowed.