I need to format a multiplication table in C++ to look like the one below (for 4
ID: 3782029 • Letter: I
Question
I need to format a multiplication table in C++ to look like the one below (for 4x4 table)
But I can't get the right formating with the program I wrote. What I have is
#include
#include
using namespace std;
int main()
{
char choice;
int tableSize, row, coll;
do
{
cout << " MENU "
<< "a) Generate Multiplication Table "
<< "q) Quit Program "
<< "Please make a selection ";
cin >> choice;
if (choice == 'a')
{
do
{
cout << "Please enter the size of the multiplication table ";
cin >> tableSize;
if (tableSize < 1 || tableSize > 10)
{
cout << "Enter a number between 1 and 10" << endl;
}
} while(tableSize < 1 || tableSize > 10)
cout << "MULTIPLICATION TABLE: " << tableSize << "'s" << endl;
for(coll = 1; coll <= tableSize; coll++)
{
cout << setw(4) << coll << endl << " ";
for(row = 1; row <= tableSize; row++)
{
cout << setw(4) << row << endl ;
for(coll = 1; coll <= tableSize; coll++)
{
cout << setw(4) << (coll * row) << "|";
}
}
}
}
else if (choice != 'q')
{
cout << " Invalid Selection " << endl;
}
else
{
return 0;
}
} while(choice != 'q');
return 0;
}
Can someone please say what I need to change or add in order to get the right format. And the output I get is
MULTIPLICATION TABLE 4' s 1 I 1 I 2 l 3 I 4 I 2 l 2 l 6 l 8 I 4 I 3 I 3 I 6 l 9 12 I 8 l 12 l 16 4 I 4 IExplanation / Answer
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char choice;
int tableSize, row, coll;
do
{
cout << " MENU "
<< "a) Generate Multiplication Table "
<< "q) Quit Program "
<< "Please make a selection ";
cin >> choice;
if (choice == 'a')
{
do
{
cout << "Please enter the size of the multiplication table ";
cin >> tableSize;
if (tableSize < 1 || tableSize > 10)
{
cout << "Enter a number between 1 and 10" << endl;
}
} while(tableSize < 1 || tableSize > 10) ;
cout << "MULTIPLICATION TABLE: " << tableSize << "'s" << endl;
cout <<setw(4)<<"";
for(int x = 1; x <= tableSize; x++)
{
cout << setw(4)<<x;
}
cout << endl ;
cout << setw(4)<<" ";
cout <<" ";
for(int x = 1; x <= tableSize; x++)
{
cout <<"----|";
}
cout << endl ;
for(coll = 1; coll <= tableSize; coll++)
{
cout << setw(4) << coll <<"|";
for(row = 1; row <= tableSize; row++)
{
cout << setw(4) << (coll * row) << "|";
}
cout << endl;
cout << " ";
cout << setw(4) <<"-|";
for(row = 1; row <= tableSize; row++)
{
cout <<"----|";
}
cout <<endl;
}
}
else if (choice != 'q')
{
cout << " Invalid Selection " << endl;
}
else
{
return 0;
}
} while(choice != 'q');
return 0;
}