week day values

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

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

#include "std_lib_facilities.h"

int main()
try
{
vector<string> dayVec = { "Monday", "monday", "Mon",
"Tuesday", "tuesday", "Tue",
"Wednesday", "wednesday", "Wed",
"Thursday", "thursday", "Thu",
"Friday", "friday", "Fri",
"Saturday", "saturday", "Sat",
"Sunday", "sunday", "Sun" };
vector<int> dayVal = { 0,0,0,0,0,0,0 };
string day;
int value;
string falsevalue;
bool check = true;

cout << "Enter pairs of week-days and values seperated by blank space: \n";
cout << "Enter exit 0 to end entry.\n";

while(check){
if(cin >> day >> value){
for(int i = 0; i < dayVec.size(); i++){
if (day == dayVec[i]) {
dayVal[i / 3] += value;
}
}
if (day == "exit" && value == 0) {
break;
check = true;
}
}
else {
cin.clear();
cin >> falsevalue;
error("Entered value illegal: ", falsevalue);
}
}

for (int i = 0; i < dayVal.size(); i++) {
cout << dayVec[i*3] << ": " << dayVal[i] << "\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 pairs of week-days and values seperated by blank space:
Enter exit 0 to end entry.
Mon 3 Wednesday 6 friday 3 exit 0
Monday: 3
Tuesday: 0
Wednesday: 6
Thursday: 0
Friday: 3
Saturday: 0
Sunday: 0
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