Hello olivergwin,
In "main":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
int main()
{
string file_name;
vector<Temperature> temps;
Temperature temp;
temp.setDegree(75.0);
cout << temp.toString() << endl;
// 1. Call the function described in TASK 4 here
getFileName();
// 2. Call the function described in TASK 5 here
readTemp(temps);
cout << "The vector size is " << temps.size() << endl << endl;
// 3. Call the function described in TASK 6 here
// 4. Call the function described in TASK 7 here
return 0;
}
|
I added some blank lines to make it easier to read.
Lines 3 and 4 I switched the order, does not make any difference just a personal thing.
Line 4 creates a vector of classes, but it is empty at this point.
Not sure what lines 6 and 7 are for.
Line 16 calls the function passing the vector of classes. still empty at this point.
In the function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void readTemp(vector<Temperature>& temps)
{
int tempNum;
cout << "Number of temperatures you will enter: " << endl;
cin >> tempNum;
cout << "Enter temperatures (Fahrenheit): " << endl;
int j = 0;
for (int i = 0; i < tempNum; i++)
{
cin >> j; // <--- The "cin" ALWAYS needs a prompt.
if ((j >= 30) && (j <= 100))
{
//temps[i] = j; // <--- Commented to get ti to compile.
}
}
}
|
First in line 1 pass the vector by reference. It is also a good idea to pass "std::string"s by reference.
The problem here is that the vector is still empty.
In line 18 you are trying to access the empty vector by using a subscript, except there is nothing to access. The second part of that is and as
lastchance mentioned you would need to call a set function.
To start with you need to add a default ctor and an overloaded ctor to you class.
The "readTemp" function should collect any input into local variable(s) then in place of line put:
temps.emplace_back(variable(s)
. This will call the overloaded ctor of the class to create an object of the class before it adds it to the vector.
The function should be adding to the vector not trying to access something that is not there.
Other than adding some blank lines to your code that is as far as I have worked with.
Still need to make the changes and test, but I believe the concept is sound.
Andy