Sieve of Eratosthenes

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 13

#include "std_lib_facilities.h"
#include "math.h"

int main() {

int max = 100;
vector<int> seriesNum;

//fill series of numbers
for (int i = 2; i < max; i++) {
seriesNum.push_back(i);
}

//check for primes
for (int j = 2; j < 6; j++) {
for (int i = 0; i < seriesNum.size() - 1; i++) {
if (seriesNum[i] % j == 0 && seriesNum[i] != j) {
seriesNum[i] = 0;
}
}
}

//print primes
for (int i = 0; i < seriesNum.size(); i++) {
if (seriesNum[i] != 0) {
cout << seriesNum[i] << "\n";
}
}

keep_window_open(".");
}
Output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
49
53
59
61
67
71
73
77
79
83
89
91
97
99

prime number generator [max]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 12

#include "std_lib_facilities.h"

int main() {

int max = 100;
vector<int> seriesNum;

cout << "Enter max for prime number generator: \n";
cin >> max;

for (int i = 2; i < max; i++) {
int count = 0;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0) {
}
}

for (int i = 0; i < primeNum.size(); i++) {
}

keep_window_open(".");
}
Output:
Enter max for prime number generator:
25
2
3
5
7
11
13
17
19
23

prime number generator

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 11

#include "std_lib_facilities.h"

int main() {

int max = 100;
vector<int> seriesNum;

for (int i = 2; i < max; i++) {
int count = 0;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
count++;
}
}
if (count == 0){
}
}

for (int i = 0; i < primeNum.size(); i++) {
}

keep_window_open(".");
}
Output:
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

rock, paper, scissors game

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 10

#include "std_lib_facilities.h"

int main()
try
{
int rndNum;
string illegalRndInput;
string moveCheck = "noCorrectMove";
string exit;
string yourMove;
int computerMoveIndex;
bool again = true;

vector<string> Move;

Move.push_back("Rock");
Move.push_back("Paper");
Move.push_back("Scissors");

while (again == true) {

cout << "Enter a random integer to calculate the computers next move: \n";

while(cin){
if (cin >> rndNum) {
computerMoveIndex = rndNum % 3;
break;
}
else {
cin.clear();
cin >> illegalRndInput;
error("Illegal input for random integer: ", illegalRndInput);
}
}

cout << computerMoveIndex << "\n";

cin.clear();
cout << "Enter your own move: Rock, Paper or Scissors: \n";

if (cin >> yourMove) {
for (int i = 0; i < Move.size(); i++) {
if (Move[i] == yourMove) {
moveCheck = "moveCorrect";
cout << "Your move: " << yourMove << "\n";
cout << "Computer move: " << Move[computerMoveIndex] << "\n";
switch (i) {
case 0:
if (computerMoveIndex == 0) {
cout << "Its a tie!\n";
}
else if (computerMoveIndex == 1) {
cout << "Computer wins!\n";
}
else if (computerMoveIndex == 2) {
cout << "You win!\n";
}
break;

case 1:
if (computerMoveIndex == 0) {
cout << "You win!\n";
}
else if (computerMoveIndex == 1) {
cout << "Its a tie!\n";
}
else if (computerMoveIndex == 2) {
cout << "Computer wins!\n";
}
break;

case 2:
if (computerMoveIndex == 0) {
cout << "Computer wins!\n";
}
else if (computerMoveIndex == 1) {
cout << "You win!\n";
}
else if (computerMoveIndex == 2) {
cout << "Its a tie!\n";
}
break;
}
}
}
if (moveCheck != "moveCorrect"){
error("Illegal input for your move: ", yourMove);
}
}

cout << "Play again? y for yes n for no: \n";

break;
}
}
keep_window_open(".");
}

catch (runtime_error e) {
cout << e.what() << "\n";
keep_window_open(".");
}
catch (...) {
cout << "exiting\n";
keep_window_open(".");
}
Output:
Enter a random integer to calculate the computers next move:
130
1
Enter your own move: Rock, Paper or Scissors:
Paper
Computer move: Paper
Its a tie!
Play again? y for yes n for no:

chess board grain counter [int and double]

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 9

#include "std_lib_facilities.h"

int main() {

cout << "Grains on chess board as integers: \n\n";
int squareNumi = 1;
int grainCounti = 1;
int sumGrainCounti = 0;

for (int i = 1; i < 65; i++) {
squareNumi = i;
grainCounti *= 2;
sumGrainCounti += grainCounti;

cout << "Square number: " << squareNumi << " / Grain number on square: " << grainCounti << " / Total grain number: " << sumGrainCounti << '\n';
}

cout << "\n\nGrains on chess board as integers: \n\n";
double squareNumd = 1;
double grainCountd = 1;
double sumGrainCountd = 0;

for (int i = 1; i < 65; i++) {
squareNumd = i;
grainCountd *= 2;
sumGrainCountd += grainCountd;

cout << "Square number: " << squareNumd << " / Grain number on square: " << grainCountd << " / Total grain number: " << sumGrainCountd << '\n';
}

keep_window_open(".");

}
Output:
Grains on chess board as integers:

Square number: 1 / Grain number on square: 2 / Total grain number: 2
Square number: 2 / Grain number on square: 4 / Total grain number: 6
Square number: 3 / Grain number on square: 8 / Total grain number: 14
Square number: 4 / Grain number on square: 16 / Total grain number: 30
Square number: 5 / Grain number on square: 32 / Total grain number: 62
Square number: 6 / Grain number on square: 64 / Total grain number: 126
Square number: 7 / Grain number on square: 128 / Total grain number: 254
Square number: 8 / Grain number on square: 256 / Total grain number: 510
Square number: 9 / Grain number on square: 512 / Total grain number: 1022
Square number: 10 / Grain number on square: 1024 / Total grain number: 2046
Square number: 11 / Grain number on square: 2048 / Total grain number: 4094
Square number: 12 / Grain number on square: 4096 / Total grain number: 8190
Square number: 13 / Grain number on square: 8192 / Total grain number: 16382
Square number: 14 / Grain number on square: 16384 / Total grain number: 32766
Square number: 15 / Grain number on square: 32768 / Total grain number: 65534
Square number: 16 / Grain number on square: 65536 / Total grain number: 131070
Square number: 17 / Grain number on square: 131072 / Total grain number: 262142
Square number: 18 / Grain number on square: 262144 / Total grain number: 524286
Square number: 19 / Grain number on square: 524288 / Total grain number: 1048574
Square number: 20 / Grain number on square: 1048576 / Total grain number: 2097150
Square number: 21 / Grain number on square: 2097152 / Total grain number: 4194302
Square number: 22 / Grain number on square: 4194304 / Total grain number: 8388606
Square number: 23 / Grain number on square: 8388608 / Total grain number: 16777214
Square number: 24 / Grain number on square: 16777216 / Total grain number: 33554430
Square number: 25 / Grain number on square: 33554432 / Total grain number: 67108862
Square number: 26 / Grain number on square: 67108864 / Total grain number: 134217726
Square number: 27 / Grain number on square: 134217728 / Total grain number: 268435454
Square number: 28 / Grain number on square: 268435456 / Total grain number: 536870910
Square number: 29 / Grain number on square: 536870912 / Total grain number: 1073741822
Square number: 30 / Grain number on square: 1073741824 / Total grain number: 2147483646
Square number: 31 / Grain number on square: -2147483648 / Total grain number: -2
Square number: 32 / Grain number on square: 0 / Total grain number: -2
Square number: 33 / Grain number on square: 0 / Total grain number: -2
Square number: 34 / Grain number on square: 0 / Total grain number: -2
Square number: 35 / Grain number on square: 0 / Total grain number: -2
Square number: 36 / Grain number on square: 0 / Total grain number: -2
Square number: 37 / Grain number on square: 0 / Total grain number: -2
Square number: 38 / Grain number on square: 0 / Total grain number: -2
Square number: 39 / Grain number on square: 0 / Total grain number: -2
Square number: 40 / Grain number on square: 0 / Total grain number: -2
Square number: 41 / Grain number on square: 0 / Total grain number: -2
Square number: 42 / Grain number on square: 0 / Total grain number: -2
Square number: 43 / Grain number on square: 0 / Total grain number: -2
Square number: 44 / Grain number on square: 0 / Total grain number: -2
Square number: 45 / Grain number on square: 0 / Total grain number: -2
Square number: 46 / Grain number on square: 0 / Total grain number: -2
Square number: 47 / Grain number on square: 0 / Total grain number: -2
Square number: 48 / Grain number on square: 0 / Total grain number: -2
Square number: 49 / Grain number on square: 0 / Total grain number: -2
Square number: 50 / Grain number on square: 0 / Total grain number: -2
Square number: 51 / Grain number on square: 0 / Total grain number: -2
Square number: 52 / Grain number on square: 0 / Total grain number: -2
Square number: 53 / Grain number on square: 0 / Total grain number: -2
Square number: 54 / Grain number on square: 0 / Total grain number: -2
Square number: 55 / Grain number on square: 0 / Total grain number: -2
Square number: 56 / Grain number on square: 0 / Total grain number: -2
Square number: 57 / Grain number on square: 0 / Total grain number: -2
Square number: 58 / Grain number on square: 0 / Total grain number: -2
Square number: 59 / Grain number on square: 0 / Total grain number: -2
Square number: 60 / Grain number on square: 0 / Total grain number: -2
Square number: 61 / Grain number on square: 0 / Total grain number: -2
Square number: 62 / Grain number on square: 0 / Total grain number: -2
Square number: 63 / Grain number on square: 0 / Total grain number: -2
Square number: 64 / Grain number on square: 0 / Total grain number: -2

Grains on chess board as integers:

Square number: 1 / Grain number on square: 2 / Total grain number: 2
Square number: 2 / Grain number on square: 4 / Total grain number: 6
Square number: 3 / Grain number on square: 8 / Total grain number: 14
Square number: 4 / Grain number on square: 16 / Total grain number: 30
Square number: 5 / Grain number on square: 32 / Total grain number: 62
Square number: 6 / Grain number on square: 64 / Total grain number: 126
Square number: 7 / Grain number on square: 128 / Total grain number: 254
Square number: 8 / Grain number on square: 256 / Total grain number: 510
Square number: 9 / Grain number on square: 512 / Total grain number: 1022
Square number: 10 / Grain number on square: 1024 / Total grain number: 2046
Square number: 11 / Grain number on square: 2048 / Total grain number: 4094
Square number: 12 / Grain number on square: 4096 / Total grain number: 8190
Square number: 13 / Grain number on square: 8192 / Total grain number: 16382
Square number: 14 / Grain number on square: 16384 / Total grain number: 32766
Square number: 15 / Grain number on square: 32768 / Total grain number: 65534
Square number: 16 / Grain number on square: 65536 / Total grain number: 131070
Square number: 17 / Grain number on square: 131072 / Total grain number: 262142
Square number: 18 / Grain number on square: 262144 / Total grain number: 524286
Square number: 19 / Grain number on square: 524288 / Total grain number: 1.04857e+06
Square number: 20 / Grain number on square: 1.04858e+06 / Total grain number: 2.09715e+06
Square number: 21 / Grain number on square: 2.09715e+06 / Total grain number: 4.1943e+06
Square number: 22 / Grain number on square: 4.1943e+06 / Total grain number: 8.38861e+06
Square number: 23 / Grain number on square: 8.38861e+06 / Total grain number: 1.67772e+07
Square number: 24 / Grain number on square: 1.67772e+07 / Total grain number: 3.35544e+07
Square number: 25 / Grain number on square: 3.35544e+07 / Total grain number: 6.71089e+07
Square number: 26 / Grain number on square: 6.71089e+07 / Total grain number: 1.34218e+08
Square number: 27 / Grain number on square: 1.34218e+08 / Total grain number: 2.68435e+08
Square number: 28 / Grain number on square: 2.68435e+08 / Total grain number: 5.36871e+08
Square number: 29 / Grain number on square: 5.36871e+08 / Total grain number: 1.07374e+09
Square number: 30 / Grain number on square: 1.07374e+09 / Total grain number: 2.14748e+09
Square number: 31 / Grain number on square: 2.14748e+09 / Total grain number: 4.29497e+09
Square number: 32 / Grain number on square: 4.29497e+09 / Total grain number: 8.58993e+09
Square number: 33 / Grain number on square: 8.58993e+09 / Total grain number: 1.71799e+10
Square number: 34 / Grain number on square: 1.71799e+10 / Total grain number: 3.43597e+10
Square number: 35 / Grain number on square: 3.43597e+10 / Total grain number: 6.87195e+10
Square number: 36 / Grain number on square: 6.87195e+10 / Total grain number: 1.37439e+11
Square number: 37 / Grain number on square: 1.37439e+11 / Total grain number: 2.74878e+11
Square number: 38 / Grain number on square: 2.74878e+11 / Total grain number: 5.49756e+11
Square number: 39 / Grain number on square: 5.49756e+11 / Total grain number: 1.09951e+12
Square number: 40 / Grain number on square: 1.09951e+12 / Total grain number: 2.19902e+12
Square number: 41 / Grain number on square: 2.19902e+12 / Total grain number: 4.39805e+12
Square number: 42 / Grain number on square: 4.39805e+12 / Total grain number: 8.79609e+12
Square number: 43 / Grain number on square: 8.79609e+12 / Total grain number: 1.75922e+13
Square number: 44 / Grain number on square: 1.75922e+13 / Total grain number: 3.51844e+13
Square number: 45 / Grain number on square: 3.51844e+13 / Total grain number: 7.03687e+13
Square number: 46 / Grain number on square: 7.03687e+13 / Total grain number: 1.40737e+14
Square number: 47 / Grain number on square: 1.40737e+14 / Total grain number: 2.81475e+14
Square number: 48 / Grain number on square: 2.81475e+14 / Total grain number: 5.6295e+14
Square number: 49 / Grain number on square: 5.6295e+14 / Total grain number: 1.1259e+15
Square number: 50 / Grain number on square: 1.1259e+15 / Total grain number: 2.2518e+15
Square number: 51 / Grain number on square: 2.2518e+15 / Total grain number: 4.5036e+15
Square number: 52 / Grain number on square: 4.5036e+15 / Total grain number: 9.0072e+15
Square number: 53 / Grain number on square: 9.0072e+15 / Total grain number: 1.80144e+16
Square number: 54 / Grain number on square: 1.80144e+16 / Total grain number: 3.60288e+16
Square number: 55 / Grain number on square: 3.60288e+16 / Total grain number: 7.20576e+16
Square number: 56 / Grain number on square: 7.20576e+16 / Total grain number: 1.44115e+17
Square number: 57 / Grain number on square: 1.44115e+17 / Total grain number: 2.8823e+17
Square number: 58 / Grain number on square: 2.8823e+17 / Total grain number: 5.76461e+17
Square number: 59 / Grain number on square: 5.76461e+17 / Total grain number: 1.15292e+18
Square number: 60 / Grain number on square: 1.15292e+18 / Total grain number: 2.30584e+18
Square number: 61 / Grain number on square: 2.30584e+18 / Total grain number: 4.61169e+18
Square number: 62 / Grain number on square: 4.61169e+18 / Total grain number: 9.22337e+18
Square number: 63 / Grain number on square: 9.22337e+18 / Total grain number: 1.84467e+19
Square number: 64 / Grain number on square: 1.84467e+19 / Total grain number: 3.68935e+19

chess board grain number calculator

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 8

#include "std_lib_facilities.h"

int main() {

int squareNum = 1;
int grainCount = 1;
int sumGrainCount = 0;

for (int i = 1; i < 65; i++) {
squareNum = i;
grainCount *= 2;
sumGrainCount += grainCount;

cout << "Square number: " << squareNum << " / Grain number on square: " << grainCount << " / Total grain number: " << sumGrainCount << '\n';
}

keep_window_open(".");
}
Output:
Square number: 1 / Grain number on square: 2 / Total grain number: 2
Square number: 2 / Grain number on square: 4 / Total grain number: 6
Square number: 3 / Grain number on square: 8 / Total grain number: 14
Square number: 4 / Grain number on square: 16 / Total grain number: 30
Square number: 5 / Grain number on square: 32 / Total grain number: 62
Square number: 6 / Grain number on square: 64 / Total grain number: 126
Square number: 7 / Grain number on square: 128 / Total grain number: 254
Square number: 8 / Grain number on square: 256 / Total grain number: 510
Square number: 9 / Grain number on square: 512 / Total grain number: 1022
Square number: 10 / Grain number on square: 1024 / Total grain number: 2046
Square number: 11 / Grain number on square: 2048 / Total grain number: 4094
Square number: 12 / Grain number on square: 4096 / Total grain number: 8190
Square number: 13 / Grain number on square: 8192 / Total grain number: 16382
Square number: 14 / Grain number on square: 16384 / Total grain number: 32766
Square number: 15 / Grain number on square: 32768 / Total grain number: 65534
Square number: 16 / Grain number on square: 65536 / Total grain number: 131070
Square number: 17 / Grain number on square: 131072 / Total grain number: 262142
Square number: 18 / Grain number on square: 262144 / Total grain number: 524286
Square number: 19 / Grain number on square: 524288 / Total grain number: 1048574
Square number: 20 / Grain number on square: 1048576 / Total grain number: 2097150
Square number: 21 / Grain number on square: 2097152 / Total grain number: 4194302
Square number: 22 / Grain number on square: 4194304 / Total grain number: 8388606
Square number: 23 / Grain number on square: 8388608 / Total grain number: 16777214
Square number: 24 / Grain number on square: 16777216 / Total grain number: 33554430
Square number: 25 / Grain number on square: 33554432 / Total grain number: 67108862
Square number: 26 / Grain number on square: 67108864 / Total grain number: 134217726
Square number: 27 / Grain number on square: 134217728 / Total grain number: 268435454
Square number: 28 / Grain number on square: 268435456 / Total grain number: 536870910
Square number: 29 / Grain number on square: 536870912 / Total grain number: 1073741822
Square number: 30 / Grain number on square: 1073741824 / Total grain number: 2147483646
Square number: 31 / Grain number on square: -2147483648 / Total grain number: -2
Square number: 32 / Grain number on square: 0 / Total grain number: -2
Square number: 33 / Grain number on square: 0 / Total grain number: -2
Square number: 34 / Grain number on square: 0 / Total grain number: -2
Square number: 35 / Grain number on square: 0 / Total grain number: -2
Square number: 36 / Grain number on square: 0 / Total grain number: -2
Square number: 37 / Grain number on square: 0 / Total grain number: -2
Square number: 38 / Grain number on square: 0 / Total grain number: -2
Square number: 39 / Grain number on square: 0 / Total grain number: -2
Square number: 40 / Grain number on square: 0 / Total grain number: -2
Square number: 41 / Grain number on square: 0 / Total grain number: -2
Square number: 42 / Grain number on square: 0 / Total grain number: -2
Square number: 43 / Grain number on square: 0 / Total grain number: -2
Square number: 44 / Grain number on square: 0 / Total grain number: -2
Square number: 45 / Grain number on square: 0 / Total grain number: -2
Square number: 46 / Grain number on square: 0 / Total grain number: -2
Square number: 47 / Grain number on square: 0 / Total grain number: -2
Square number: 48 / Grain number on square: 0 / Total grain number: -2
Square number: 49 / Grain number on square: 0 / Total grain number: -2
Square number: 50 / Grain number on square: 0 / Total grain number: -2
Square number: 51 / Grain number on square: 0 / Total grain number: -2
Square number: 52 / Grain number on square: 0 / Total grain number: -2
Square number: 53 / Grain number on square: 0 / Total grain number: -2
Square number: 54 / Grain number on square: 0 / Total grain number: -2
Square number: 55 / Grain number on square: 0 / Total grain number: -2
Square number: 56 / Grain number on square: 0 / Total grain number: -2
Square number: 57 / Grain number on square: 0 / Total grain number: -2
Square number: 58 / Grain number on square: 0 / Total grain number: -2
Square number: 59 / Grain number on square: 0 / Total grain number: -2
Square number: 60 / Grain number on square: 0 / Total grain number: -2
Square number: 61 / Grain number on square: 0 / Total grain number: -2
Square number: 62 / Grain number on square: 0 / Total grain number: -2
Square number: 63 / Grain number on square: 0 / Total grain number: -2
Square number: 64 / Grain number on square: 0 / Total grain number: -2

spelled out number calculator

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 7

#include "std_lib_facilities.h"

vector<string> numbers;

void initNumbers() {
numbers.push_back("zero");
numbers.push_back("one");
numbers.push_back("two");
numbers.push_back("three");
numbers.push_back("four");
numbers.push_back("five");
numbers.push_back("six");
numbers.push_back("seven");
numbers.push_back("eight");
numbers.push_back("nine");
}

int getNum() {
int vali = numbers.size();
string vals;

// getInt
if (cin >> vali) {
if (vali <= 10) {
return vali;
}
else {
error("Entered integer is too high. You entered: ", vali);
}
}

cin.clear();
// getString
string s = "noNumber";
cin >> vals;
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] == vals) {
return i;
s = vals;
}
}
if (s == "noNumber") {
error("Illegal entered number. You entered: ", vals);
}
}

int main()
try{

initNumbers();
int value1 = 0;
int value2 = 0;
char operat = ' ';

while (cin) {

cout << "Enter two intgeres followed by a operator seperated by spaces.\n";
//cout << "Enter one integer as digit or written out: \n";
value1 = getNum();

//cout << "Enter another integer as digit or written out: \n";
value2 = getNum();

//cout << "Enter an ooperator: \n";
cin >> operat;

if (operat == '+') {
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 + value2 << "\n";
}
if (operat == '-') {
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 - value2 << "\n";
}
if (operat == '/') {
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 / value2 << "\n";
}
if (operat == '*')
{
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 * value2 << "\n";
}
}

keep_window_open(".");
}

catch (runtime_error e) {	// this code is to produce error messages; it will be described in Chapter 5
cout << e.what() << '\n';
keep_window_open(".");	// For some Windows(tm) setups
}
catch (...) {	// this code is to produce error messages; it will be described in Chapter 5
cout << "exiting\n";
keep_window_open(".");	// For some Windows(tm) setups
}
Output:
Enter two intgeres followed by a operator seperated by spaces.
two 3 +
2 + 3 equals to: 5
Enter two intgeres followed by a operator seperated by spaces.

digit and spelled out integers

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 6

#include "std_lib_facilities.h"

vector<string> numbers;

void initNumbers(){
numbers.push_back("zero");
numbers.push_back("one");
numbers.push_back("two");
numbers.push_back("three");
numbers.push_back("four");
numbers.push_back("five");
numbers.push_back("six");
numbers.push_back("seven");
numbers.push_back("eight");
numbers.push_back("nine");
}

void getNum() {
int vali = numbers.size();
string vals;

// getInt
if (cin >> vali) {
if (vali <= 10) {
cout << numbers[vali] << "\n";
return;
}
else {
error("Entered integer is too high. You entered: ", vali);
}
}

cin.clear();
// getString
string s = "noNumber";
cin >> vals;
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] == vals) {
cout << i << "\n";
s = vals;
}
}
if(s == "noNumber") {
error("Illegal entered number. You entered: \n", vals);
}
}

int main()
try
{
initNumbers();

cout << "Please enter a number as integer between 0 and 9 or string as 'zero' to 'nine'\n";

while (cin){
getNum();
}

keep_window_open(".");
}

catch (runtime_error e) {
cout << e.what() << '\n';
keep_window_open(".");
}
catch (...) {
cout << "exiting\n";
keep_window_open(".");
}
Output:
Please enter a number as integer between 0 and 9 or string as 'zero' to 'nine'
one
1
two
2
three
3
4
four
5
five
6
six

simple calculator

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 5

#include "std_lib_facilities.h"

int main() {

double value1 = 0.0;
double value2 = 0.0;
char operat = ' ';

cout << "Please enter two double values and an operator in a row with space in between.\n";

while (cin >> value1 >> value2 >> operat)
{
if (operat == '+')
{
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 + value2 << "\n";
}
if (operat == '-')
{
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 - value2 << "\n";
}
if (operat == '/')
{
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 / value2 << "\n";
}
if (operat == '*')
{
cout << value1 << " " << operat << " " << value2 << " equals to: " << value1 * value2 << "\n";
}
}
return 0;

keep_window_open("-");
}
Output:
Please enter two double values and an operator in a row with space in between.
6.3 8.9 *
6.3 * 8.9 equals to: 56.07

numbers guessing game

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

//	Philipp Siedler
//	Bjarne Stroustrup's PP
//	Chapter 4 Exercise 4

#include "std_lib_facilities.h"

int main() {

int max = 100;
int min = 0;
int check = 0;

while (check == 0) {
cout << "Is your number smaller, larger or equal to " << (max + min) / 2 <<"? Enter s for smaller, l for larger or e for equal.\n";
max = (max + min) / 2;
}
else if (answer == "l") {
min = (max + min) / 2;
}
else if (answer == "e") {
cout << "Your number is: " << (max + min) / 2 << "\n";
check = 1;
}
}
keep_window_open();
}
Output:
Is your number smaller, larger or equal to 50? Enter s for smaller, l for larger or e for equal.
s
Is your number smaller, larger or equal to 25? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 37? Enter s for smaller, l for larger or e for equal.
s
Is your number smaller, larger or equal to 31? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 34? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 35? Enter s for smaller, l for larger or e for equal.
l
Is your number smaller, larger or equal to 36? Enter s for smaller, l for larger or e for equal.
e