//program 5.8 using a nested loop to generate multplication tables
include<iostream>
include<iomanip>
include<cctype>
using namespace std;
int main() {
int table = 0;
const int table_min = 2;
const int table_max = 12;
char ch = 0;
do
{
cout << endl << "what size would you like (" << table_min << "to" << table_max << " )?";
cin >> table;
cout << endl;
//make sure table size is within the limits
if (table<table_min || table>table_max) {
cout << "invalid table size entered. program terminated." << endl;
exit(1);//返回0表示正常结束;返回1表示非正常结束
}
//create the top line of the table
cout << " |";
for (int i = 1; i <= table; i++) cout << "" << setw(3) << i << " |";
cout << endl;
//create the separatorrow
for (int i = 1; i <= table; i++)cout << "-----";
cout << endl;
for (int i = 0; i <= table; i++) {
cout << "" << setw(3) << i << " |";
//output the values in a row
for (int j = 1; j <= table; j++)cout << "" << setw(3) << i*j << " |";
cout << endl;
}
//check if another table is required
cout << endl << "do you want another table(y or n)?";
cin >> ch;
cout << endl;
} while (tolower(ch) == 'y');
return 0;
}
网友评论