You are here:

C++/c++timetable

Advertisement


Question
hi,tis is the code i wrote for constructing the timetable could u pls help mi!!!i do nt knw why my program will go infinite loop.


example program 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 (n,i);
}

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;
       }
}

i would be veri delightful if u could help mi with my problem.thankz!!!!
thankz!!!

Answer
the approach is right.
but the basic problem with your code is:

a. the use of uninitialized variables. for example, in main, n and i which are passed to 'displayTimeTable' are not initialized.
tip: postpone the definition a variable until you know how to initialize it.

b. the same variable being used for more than one piece of data. for example the variable 'n' in displayTimeTable.
tip: use different loop control variables in nested loops.

either of which could result in an infinite loop.

another minor issue was in getting duplicate user input for rows, cols.

with a few corrections, the code should work as specified:

#include <iostream>
using namespace std;


// Function prototypes
int getInt( const char* data_desc );
void displayTimeTable (int,int);


int main ()
{
     // Reading data validation
     int rows = getInt( "rows" );
     int columns = getInt( "columns" );

     displayTimeTable( columns, rows );
}

int getInt( const char* data_desc )
{
     int n;
     const int MAX_VAL = 9 ;
     const int MIN_VAL = 1 ;
     do
     {
         cout << "Enter the number of " << data_desc << ": ";
         cin >> n;
         if( ( n > MAX_VAL ) || ( n < MIN_VAL ) )
         cout << "Please input a positive integer N that is "
         "between " << MIN_VAL << " and " <<  MAX_VAL << endl;
     }while( ( n > MAX_VAL ) || ( n < 1 ) );

     return n;
}


void displayTimeTable (int cols, int rows )
{
     cout << "* ";
     for( int i = 1; i <= cols ;   i )
         cout << "t" << i ;
     cout << "n---------------------------------------------------------------n";

     for( int i = 1; i <= rows ;   i )
     {
         cout << i << '.' ;
         for( int j = 1 ; j <= cols ;   j )
         cout << "t" << i * j;
         cout << endl;
     }
}  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.