C++ do while loop Generating multiplication tables with Nested Loops

Description

C++ do while loop Generating multiplication tables with Nested Loops

#include <iostream>
#include <iomanip>
#include <locale>

int main()// w w w. ja  v a 2s. co  m
{
  int table {};              // Table size
  const int table_min {2};   // Minimum table size - at least up to the 2-times
  const int table_max {12};  // Maximum table size
  char reply {};                // Response to prompt

  do
  {
    std::cout << "What size table would you like ("
              << table_min << " to " << table_max << ")? ";
    std::cin  >> table;         // Get the table size
    std::cout << std::endl;

    // Make sure table size is within the limits
    if(table < table_min || table > table_max)
    {
      std::cout << "Invalid table size entered. Program terminated." << std::endl;
      return 1;
    }

    // Create the top line of the table
    std::cout << std::setw(6) << " |";
    for(int i {1} ; i <= table ; ++i)
    {
      std::cout << " " << std::setw(3) << i << " |";
    }
    std::cout << std::endl;

    // Create the separator row
    for(int i {} ; i <= table ; ++i)
    {
      std::cout << "------";
    }
    std::cout << std::endl;

    for(int i {1} ; i <= table ; ++i)
    {    // Iterate over rows
      std::cout << " " << std::setw(3) << i << " |"; // Start the row

      // Output the values in a row
      for(int j {1} ; j <= table ; ++j)
      {
        std::cout << " " << std::setw(3) << i*j << " |";  // For each col.
      }
      std::cout << std::endl;                             // End the row
    }

    // Check if another table is required
    std::cout << "\nDo you want another table (y or n)? ";
    std::cin >> reply;
  } while(tolower(reply) == 'y');
}



PreviousNext

Related