column-wise display

I created a program to display the normal multiplication table. The last term and the table should be given by the user. The problem is that, if any bigger number is given as the last term, the display is in a same column that it is looking odd and also in old compilers like "Turbo C++" the starting part of the table is not visible. To overcome this program, I tried to display the output in a manner that in the table 1 to 10 comes in 1st column , 11 to 20 in the 2nd and so on... But i couldn't make it! Please solve this problem....

Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream.h>
#include<conio.h>
void main()
{
int a,n,i,x;
cout<<"\n\n\nEnter a number:";
cin >>n;
cout<<"\n\n\nEnter the last term:";
cin>>x;
cout<<"\n";
for(i=1;i<=x;i++)
{
a=i*n;
cout<<"\t"<<i<<"*"<<n<<"="<<a<<"\n";
}
getch();
}
O_o

I wouldn't be surprised if this compiles, but there are lots of issues here.

Line 1: It's <iostream>. <iostream.h> works and eliminates the problem at line 2.5, but it has been deprecated.
Line 2: <conio.h> is deprecated, although I suppose you could leave it there for getch()- nah, change it. :)
Line 2.5: using namespace std; is missing... this becomes an error when you switch to <iostream>.
Line 3: Although some compilers let this slide, it must be int main().

Now, on to your actual problem. Might I suggest having a couple of loops to print out 0s in the event that a number is too short in your table-printing loop? If you #include <cmath> and use the log10 function to determine how many zeroes you'll need, then that could fix your problem.

-Albatross
Thank you. But, I'm sorry... I'm not clear! Can you please elaborate your answer?
And about the mistakes in my program! I didn't know about this. In my compilers use of "getch();" is not allowed until i use this "conio" header file. Is there any remedy for it?
works perfect for me.

I just changed a few things:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main()
{
int a,n,i,x,exit;
cout<<"\n\n\nEnter a number:";
cin >>n;
cout<<"\n\n\nEnter the last term:";
cin>>x;
cout<<"\n";
for(i=1;i<=x;i++)
{
a=i*n;
cout<<"\t"<<i<<"*"<<n<<"="<<a<<"\n";
}
cin>>exit;
}


Then gave out expected output (im using dev c++)
Last edited on
Yeah it works..... Thanks.
Topic archived. No new replies allowed.