sum of integer range

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 5 Exercise 8
Using std_lib_facilities.h by Bjarne Stroustrup.

[code language=”cpp”]
// Philipp Siedler
// Bjarne Stroustrup’s PP
// Chapter 5 Exercise 8

//input: 5
//input: 3 5 6 3 34 54x

#include "std_lib_facilities.h"

int main()
try
{
vector<int> numVec;

int numCount = 0;
int value;
int numSum = 0;
string check;

cout << "Enter number of integers to be read:\n";
if(cin >> numCount){}
else {
cin.clear();
cin >> check;
error("Entered value does not match: ", check);
}

cout << "Enter some values to be stored seperated by a blank space "
"ending with a ‘x’: \n";
for (int i = 0; i < numCount; i++) {
if (cin >> value) {
numVec.push_back(value);
numSum += value;
}
else {
cin.clear();
cin >> check;
if (check == "x") {
break;
}
else{
error("Input is no value: ", check);
}
}
}

if (numVec.size() < numCount) {
error("The entered number of integers to be read is larger than the vector size.\n");
}

for (int i = 0; i < numVec.size(); i++) {
cout << numVec[i] << "\n";
}
cout << "sum = " << numSum << "\n";

keep_window_open(".");
}
catch (runtime_error e) {
cout << e.what() << "\n";
keep_window_open(".");
}
catch (…) {
cout << "Extiting" << "\n";
keep_window_open(".");
}
[/code]

Output:
Enter number of integers to be read:
5
Enter some values to be stored seperated by a blank space ending with a 'x':
3 5 6 3 34 54x
3
5
6
3
34
sum = 51
Please enter . to exit

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124