c++construct timetable

i am a newbie in c++ programming.i realli need help!!!tis is the code tat i wrote for constructing timetable.could anybody pls help mi!!!i do nt knw which part of my code is wrong.i do nt knw y my program will go infinite loop.


example output:
* 1 2 3 4 5 6 7
----------------------
1 1 2 3 4 5 6 7
2 2 4 6 8 10 12 14
3 3 6 9 12 15 18 21
4 4 8 12 16 20 24 28
5 5 10 15 20 25 30 35



#include <iostream>
using namespace std;


// Function prototypes
int getInt (int);
void displayTimeTable (int,int);


int main ()
{
int rows;
int column;
int n;
int i;


// Reading data validation
rows = getInt (0);
column = getInt (0);
displayTimeTable (rows,column);
}

int getInt (int i)
{
int n;

do
{

cout << "Enter the rows: ";
cin >> n;
cout << "Enter the column: ";
cin >> n;
cout << endl;
if (n > 10)
cout << "Please input a positive integer N that is smaller than 10"<<endl;
else
cout << "Multiplication TimeTable"<<endl;
cout << endl;
}while (n < i);

return n;
}


void displayTimeTable (int n,int i)
{
cout << "*";
for (int n = 1; n <= i; n++)
cout << "\t" << n;
cout << endl;
cout <<"----------------------------------------------------------\n";
for (int n = 1; n < i; ++n)
{
cout << n;
for (int j = 1; n <= i; j++)
cout << "\t" << n * j;
cout << endl;
}
}

First, please use code tags when posting your code. They can be found as the # symbol on the Format toolbar.

Now on to your problems.
You never change i or n in your final for loop in displayTimeTable, so the loop will never exit. You should probably be comparing j <=i since you seem to be using j as your counter and multiplier.

Once you fix that you'll notice you're one column short (don't blame you for not seeing in that mess) so you'll want to make your second for loop in displayTimeTable go till n <= i instead of just n < i.

edit: btw, your code will currently only print out square tables. You need to stop overwriting your variables in displayTimeTable if you want it to print non-square times tables.
Last edited on
You are also prompting the user for two integers, but since you pull it into n both times you will only get the last value entered (columns). Therefore you will never pull in the rows value and return it to your main.
Topic archived. No new replies allowed.