This topic is now archived and is closed to further replies.


not sure how to go about doing this:

Recommended Posts

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!

using namespace std;

	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

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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;

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 -

Share this post

Link to post
Share on other sites
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:

// untested code

const 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.

Share this post

Link to post
Share on other sites