#include<iostream>
#include<iomanip>
using namespace std;
main()
{
int size;
int x, int y;
int a = size - 12;
cout << endl<< "How many rows/columns would you like in your multiplication table? ";
cin >> size;
cout << endl;
cout << size << " by " << size << "multiplication table"
<< endl << endl
<< " |";
for (x = 1; x <= size; x++) //column headers
cout << setw(3) << x << " ";
cout << endl;
for (x = 1; x <= size; x++) //row headers
cout << "______";
cout << endl;
for (x = 1; x <= size; x++)
{
cout << endl;
cout << setw(2) << x << setw(2) << "|";
for (y = 1; y <= size; y++)
cout << setw(3) << x*y << " ";
}
cout << endl;
return 0;
}
The Light shine on you
not sure how to go about doing this:
OK, I have it worked out to look nice if ''size'' equals 12 or less, but higher than that and it comes onto the next line. So I want to make it so it only shows the last 12 numbers in the multiplicaiton tabke, say I put in 45 for ''size'' than I would want to show 34-45. I thought it might work if i subtraced ''a'' from ''size'' in the FOR staements, but I just eneded up with an infinite loop. any help? thanks!
You could try something like this:
int start;
AFTER your cin >> size;
if (size>12) //Only do the subtraction if it''s going to result in > 0
start = size-12;
else
start=1;
then in all your loops change it from x=1; to x=start; (and y=1; to y=start. That should solve your problem.
Billy - BillyB@mrsnj.com
int start;
AFTER your cin >> size;
if (size>12) //Only do the subtraction if it''s going to result in > 0
start = size-12;
else
start=1;
then in all your loops change it from x=1; to x=start; (and y=1; to y=start. That should solve your problem.
Billy - BillyB@mrsnj.com
You can''t say "int a = size - 12", and expect a to be automagically calculated every time the value of size changes. C++ is a static language! You must put this calculation after getting size from the user. You will also want to check if a is less than a particular size and, if so, modify it to become a particular minimum size. For example:
Also, bear in mind that when you receive user input into a numeric field, the user might enter a non-numeric. You need to check the cin failbit for that, else your program will not do anything sensible in the face of invalid input.
--
1st law of programming: Any given program, when running, is obsolete.
// untested codeconst int max_size = 12;const int min_size = 6;cin >> size;int a = size - max_size;if( a < min_size ) a = min_size;
Also, bear in mind that when you receive user input into a numeric field, the user might enter a non-numeric. You need to check the cin failbit for that, else your program will not do anything sensible in the face of invalid input.
--
1st law of programming: Any given program, when running, is obsolete.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement