Hey, Iv decided to tackle a bit off C++ before I start it in college in fall and Im sort of stuck on a problem. The book im reading is Accelerated C++ and its exercise 3.2.
Basically the question asks to get values from the user and take for at a time and give the sum of it...I have taken a few liberties since the actual question is very vague. My idea is that the user enters enough numbers to be divisible by 4 after the vector gets sorted. So you basically sort the vector and take the first 4 and the the second 4 and so on until you reach the end of the vector. I am not going to divide by 4 yet just sum the values right now.
My question is,
a) how can I terminate the second loop
b) will my variable z go out of scope before it goes through the whole vector.
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::sort;
using std::cout; using std::streamsize;
using std::endl; using std::string;
using std::setprecision; using std::vector;
int main()
{
// ask for and read the value
cout << "Please enter an integer value: ";
vector<int> value;
double x;
while (cin >> x)
value.push_back(x);
// check that a value has been entered
typedef vector<double>::size_type vec_sz;
vec_sz size = value.size();
if (size == 0) {
cout << endl << "You must enter your grades. "
"Please try again." << endl;
return 1;
}
// sort the grades
sort(value.begin(), value.end());
// compute the values taking the largest 4 and so on
int y = 0;
int i, z;
vector<int> sum;
while(z != size){
while(y != 4){
y++;
sum[i] += value[z];
z++;
}
i++;
}
// Output and create a vector for sum
cout << sum[i] << endl;
return 0;
}