1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <string>
#include <iomanip>
int main()
{
const int ndays = 7 ;
const int nmonkeys = 3 ;
const std::string day_names[ndays] { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" } ;
double food[nmonkeys][ndays] = // in actual code, accept from user input
{
{ 1.2, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8 },
{ 3.4, 4.5, 5.6, 6.7, 7.8, 1.2, 2.3 },
{ 6.7, 7.8, 1.2, 2.3, 3.4, 4.5, 5.6 }
};
const int fld_width = 10 ;
const auto fw = std::setw(fld_width) ; // http://www.stroustrup.com/C++11FAQ.html#auto
// print the header
std::cout << fw << "monkey" ;
// http://www.stroustrup.com/C++11FAQ.html#for
for( const std::string& dn : day_names ) std::cout << fw << dn ;
std::cout << '\n' ;
for( int i = 0 ; i < (ndays+1) ; ++i ) std::cout << fw << "----" ;
std::cout << '\n' ;
// print the details
std::cout << std::fixed << std::setprecision(2) ;
for( int i = 0 ; i < nmonkeys ; ++i )
{
std::cout << fw << '#' + std::to_string(i+1) ; // monkey number
for( double amt : food[i] ) std::cout << fw << amt ; // food amt for each day
std::cout << '\n' ;
}
}
|