Can't seem to clear my dynamic array TIME SENSITIVE

EDIT: added the requirements/outlines of my code in another comment. If anymore information is required feel free to ask.

This is currently the code I'm working with. I've got it to accomplish everything required of me so far, however, when I use option 1 more than once without exiting the program it doesn't seem to store the correct values or amount of values in the array. I've tried using the delete[] command in several places (at the beginning of case 1 in the switch, at the start of function option1, and just before the first fin.open) to no success. I'm not quite sure what the issue with my code is and why it isn't working.

For example, if I read in a file test.txt that only has 7 inside of it I will receive the proper responses. However, if I read that same test.txt file twice in a row I get much larger numbers where I should always just be receiving 7 back.
If I continue to do this over and over again the numbers get exponentially larger.

I'd fiddle with it all night, but its 3 am and I work at 7. Hate to quit on my work, but I just don't have the time.

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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<fstream>
#include<string>
#include<iomanip>
#include<cmath>
using namespace std;
void option1();
void option2();
void option3();

int *array;
int tmp, count;


int main()
{
    char selection;
    do {
        cout << "Welcome to the Menu \n";
        cout << "=================================================================\n";
        cout << "1.  Read integers from a file into an array.\n";
        cout << "2.  Display the product of all integers.\n";
        cout << "3.  Calculate the average and standard deviation of all integers.\n";
        cout << "0.  Exit\n";
        cout << "=================================================================\n";
        cout << "Enter selection: ";
        cin >> selection;
        cout << endl;
        switch (selection) {
            case '1': option1(); break;
            case '2': option2(); break;
            case '3': option3(); break;
        }
    } while (selection != '0');
    return 0;
}

void option1()
{
    ifstream fin;
    string infile = "";
    int err = 1;
    while(err){
        err = 0;
        cout << "Enter the file name: ";
        cin >> infile;
        fin.open(infile.c_str());
        if(fin.fail()){
            err = 1;
        }
        if(!err){
            while (fin >> tmp){
                count++;
            }
            array = new int[count];
            fin.close();
            fin.open(infile.c_str());
            int i = 0;
            while(fin >> tmp){
                array[i++]=tmp;
            }
            fin.close();
            cout << endl;
        }
        if(err) {
            cout << "Invalid file name." << endl;
            cout << endl;
            return;
        }
    }
    return;

}

void option2()
{
    double product = 1;
    if(array == NULL){
        cout << "Please select and complete Option 1 before attempting Option 2\n";
        cout << endl;
        return;
    }
    for (int i = 0; i < count; i++){
        product = (double)array[i] * product;
    }
    cout << "The product of all integers in the array is: " << scientific << product << "\n";
    cout << endl;
    return;
}

void option3()
{
    double sum = 0.0;
    if(array == NULL){
        cout << "Please select and complete Option 1 before attempting Option 3\n";
        cout << endl;
        return;
    }
    for (int i = 0; i < count; i++){
        sum = (double)array[i] + sum;
    }
    double avg = sum / count;
    if(count == 1){
        cout << "The average of all integers in the array is: " << fixed << avg << "\n";
        cout << "The standard deviation cannot be calculated whilst there is only one value in the array.\n";
        cout << endl;
        return;
    }
    double deviation;
    double sum2 = 0;
    for(int i = 0; i < count; i++){
        sum2 += pow((array[i]-avg),2);
    }
    deviation = sqrt(sum2/count);
    cout << "The average of all integers in the array is: " << fixed << avg << "\n";
    cout << "The standard deviation of all integers in the array is: " << deviation << "\n";
    cout << endl;
    return;
}
Last edited on
Here are the required elements of my array:
Proper indenting.
Please use only one array. No duplicate arrays are necessary.
Options 2 and 3 MUST check for an empty array as described in the previous paragraph.
Option 1 MUST prompt the user for a filename. You CANNOT hardcode a filename into your program.
Option 1 MUST print out an error message and continue to the next menu iteration if the input file cannot be opened.
If file open fails, do NOT exit the program.
Close the input file after you finish extracting all of the integers.
If the size of the array (number of elements in the array) is 1 when Option 3 is selected, print an error message instead of calculating the deviation (there must be at least 2 elements in order to calculate the deviation). Still calculate and display the average.

"The user might select the menu options in any sequence, and more than once, and the program must still operate perfectly.
When an assignment says to input integers, the array declared must be an integer array."
^^
This is the reason I didn't declare my array as a double which I believe might have made my code a bit more efficient.
Last edited on
Topic archived. No new replies allowed.