Can someone tell me why my code isnt working

//contents of Numbers.h

#ifndef NUMBERS_H
#define NUMBERS_H

#include <iostream>

using namespace std;

class Numbers {
private:
double *arr;
int size;
public:
//constructor
Numbers(int n) {
//check for input
if (n < 1) {
cout << "There should be at least 1 element";
cout << " in array!\n";
//terminate program immediately
exit(EXIT_FAILURE);
}
//otherwise
size = n;
//dynamically allocate array of
//doubles of size n
arr = new double[size];

//initialize all of the values to 0
//so they don't contain "garbage"
for (int i = 0; i < size; i++) {
*(arr + i) = 0;
}
}

//function to store a number in any
//position in the array
void storeAtPosition(int pos, double d) {
//perform bounds checking
if (pos <= 0 || pos > size) {
cout << "Position should be between 1 ";
cout << "and " << size << endl;
return;
}
//subtract 1 from pos to save it
//to the correct position in array
*(arr + pos - 1) = d;
}

//function to get element at any position
double getElementAt(int pos) const {
//perform bounds checking
while (pos <= 0 || pos > size) {
cout << "Position should be between 1 ";
cout << "and " << size << endl;
cin >> pos;
}
//subtract 1 from pos to get to
//the correct position in array
return *(arr + pos - 1);
}

//function to get highest element in array
double getHighest() const {
double highest;
//initialize it to first element
highest = *(arr);
//step though all elements of array
//and update highest as necessary
for (int i = 0; i < size; i++) {
if (*(arr + i) > highest)
highest = *(arr + i);
}

//now highest should hold the
//highest value in the array
return highest;
}

//function to get lowest element in array
double getLowest() const {
double lowest;
//initialize it to first element
lowest = *(arr);
//step though all elements of array
//and update highest as necessary
for (int i = 0; i < size; i++) {
if (*(arr + i) < lowest)
lowest = *(arr + i);
}

//now lowest should hold the
//lowest value in the array
return lowest;
}

//function to return total
double getAverage() const {
double average, total = 0;

//step through the array and get total
for (int i = 0; i < size; i++)
total += *(arr + i);

//get average by typecasting to double
//to prevent integer division
average = (double)total / size;

return average;
}

//destructor
~Numbers() {
//free up allocated memory
delete[] arr;
}
};

#endif


//contents of main.cpp

#include <iostream>
#include <string>
#include <iomanip>

#include "Numbers.h"

using namespace std;

int main()
{
//create a Numbers object
cout << "Now creating an object that will hold";
cout << " 5 numbers...\n";
Numbers obj1(5);
cout << "Done!\n";

//populate array with values
cout << "\nNow populating array with values:\n";
for (int i = 1; i <= 5; i++) {
cout << "Now writing " << ((17 * i) % (i*i))*1.0;
cout << " at position " << i << "...\n";
obj1.storeAtPosition(i, ((17 * i) % (i*i))*1.0);
}

//variables to hold user choices
int pos;
double num;
cout << "\nNow modify one of the values.\n";
cout << "Enter the number you want to store: ";
cin >> num;
cout << "Now enter the position in which to store it: ";
cin >> pos;
obj1.storeAtPosition(pos, num);
cout << "Done!\n";

//display contents of array in object
cout << "\nNow displaying contents of array...\n";
for (int i = 1; i <= 5; i++) {
cout << "Position " << i << ": ";
cout << obj1.getElementAt(i) << endl;
}

//get highest, lowest and average
cout << fixed << setprecision(2);
cout << "\nHighest value: " << obj1.getHighest();
cout << "\nLowest value: " << obj1.getLowest();
cout << "\nAverage: " << obj1.getAverage();

//return 0 to mark successful termination
return 0;
}


It keeps saying that "Numbers.h" isnt being found, but I don't have any other errors. Can someone help me out with this?

I am using Visual Basic btw
I copied and pasted your code all as one slab and got it to run as shown below. All I did was comment out the #include ... at the ************ line

What you should do is put the numbers.h (and numbers.cpp file if it exists) parts in separate and program accessible file(s). That is if you haven't already done that in which case the reason it doesn't work is the file(s) aren't in the program include path - either make the IDE project changes to the include directory or hard-code the full path in your #include line.

BTW You aren't using Visual Basic.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//contents of Numbers.h

#ifndef NUMBERS_H
#define NUMBERS_H

#include <iostream>

using namespace std;

class Numbers {
private:
    double *arr;
    int size;
public:
    //constructor
    Numbers(int n) {
        //check for input
        if (n < 1) {
            cout << "There should be at least 1 element";
            cout << " in array!\n";
            //terminate program immediately
            exit(EXIT_FAILURE);
        }
        //otherwise
        size = n;
        //dynamically allocate array of
        //doubles of size n
        arr = new double[size];
        
        //initialize all of the values to 0
        //so they don't contain "garbage"
        for (int i = 0; i < size; i++) {
            *(arr + i) = 0;
        }
    }
    
    //function to store a number in any
    //position in the array
    void storeAtPosition(int pos, double d) {
        //perform bounds checking
        if (pos <= 0 || pos > size) {
            cout << "Position should be between 1 ";
            cout << "and " << size << endl;
            return;
        }
        //subtract 1 from pos to save it
        //to the correct position in array
        *(arr + pos - 1) = d;
    }
    
    //function to get element at any position
    double getElementAt(int pos) const {
        //perform bounds checking
        while (pos <= 0 || pos > size) {
            cout << "Position should be between 1 ";
            cout << "and " << size << endl;
            cin >> pos;
        }
        //subtract 1 from pos to get to
        //the correct position in array
        return *(arr + pos - 1);
    }
    
    //function to get highest element in array
    double getHighest() const {
        double highest;
        //initialize it to first element
        highest = *(arr);
        //step though all elements of array
        //and update highest as necessary
        for (int i = 0; i < size; i++) {
            if (*(arr + i) > highest)
                highest = *(arr + i);
        }
        
        //now highest should hold the
        //highest value in the array
        return highest;
    }
    
    //function to get lowest element in array
    double getLowest() const {
        double lowest;
        //initialize it to first element
        lowest = *(arr);
        //step though all elements of array
        //and update highest as necessary
        for (int i = 0; i < size; i++) {
            if (*(arr + i) < lowest)
                lowest = *(arr + i);
        }
        
        //now lowest should hold the
        //lowest value in the array
        return lowest;
    }
    
    //function to return total
    double getAverage() const {
        double average, total = 0;
        
        //step through the array and get total
        for (int i = 0; i < size; i++)
        total += *(arr + i);
        
        //get average by typecasting to double
        //to prevent integer division
        average = (double)total / size;
        
        return average;
    }
    
    //destructor
    ~Numbers() {
        //free up allocated memory
        delete[] arr;
    }
};

#endif


//contents of main.cpp

#include <iostream>
#include <string>
#include <iomanip>

//#include "Numbers.h" /// ************************************

using namespace std;

int main()
{
    //create a Numbers object
    cout << "Now creating an object that will hold";
    cout << " 5 numbers...\n";
    Numbers obj1(5);
    cout << "Done!\n";
    
    //populate array with values
    cout << "\nNow populating array with values:\n";
    for (int i = 1; i <= 5; i++) {
        cout << "Now writing " << ((17 * i) % (i*i))*1.0;
        cout << " at position " << i << "...\n";
        obj1.storeAtPosition(i, ((17 * i) % (i*i))*1.0);
    }
    
    //variables to hold user choices
    int pos;
    double num;
    cout << "\nNow modify one of the values.\n";
    cout << "Enter the number you want to store: ";
    cin >> num;
    cout << "Now enter the position in which to store it: ";
    cin >> pos;
    obj1.storeAtPosition(pos, num);
    cout << "Done!\n";
    
    //display contents of array in object
    cout << "\nNow displaying contents of array...\n";
    for (int i = 1; i <= 5; i++) {
        cout << "Position " << i << ": ";
        cout << obj1.getElementAt(i) << endl;
    }
    
    //get highest, lowest and average
    cout << fixed << setprecision(2);
    cout << "\nHighest value: " << obj1.getHighest();
    cout << "\nLowest value: " << obj1.getLowest();
    cout << "\nAverage: " << obj1.getAverage();
    
    //return 0 to mark successful termination
    return 0;
}



Now creating an object that will hold 5 numbers...
Done!

Now populating array with values:
Now writing 0 at position 1...
Now writing 2 at position 2...
Now writing 6 at position 3...
Now writing 4 at position 4...
Now writing 10 at position 5...

Now modify one of the values.
Enter the number you want to store: 
Yeah i am lol but yeah thank you for fixing it. It works for me now
Topic archived. No new replies allowed.