Multiplication table program is stuck in a infinte loop, can't seem to debug

I'm writing a Multiplication table generator, I'm finished with the code with 2 minor issues I can't seem to find an answer to. I'm sure i've just been staring at the screen too long and im overlooking something simple. My Regular multiplication table outputs correctly, but my even numbers table just produces a 4 that constantly gets outputted to the screen in a infinite loop.

The only other issue i can't figure out is the do while loop asking to run the program over. Even if you type anything other then 1, the program still loops again. I initialized again=1 before the do while loop in the hopes that it would change the value as the user entered it. Any Ideas for these 2 problems? Is my approach to the for loop i used in the even number table wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
 /* prompt user for a row and column space between  1 and 12 that do
   not need to match.  Program will print out a regular multiplication table
   and then a multiplication table by 2's
   
*/

#include <iostream>
#include <iomanip>

using namespace std;

int userInput(int &row, int &col);
int regTable(int &row, int &col);
int evenTable(int &row, int &col);

int main()
{
    int again, row, col;
    
    
    
    do
      {
       userInput(row,col);
       regTable(row,col);
       evenTable(row,col);
       
       cout << "\nWould You Like To Run This Program Again?  (Press 1 For Yes):  ";
       cin >> again;
       
       }while (again=1);
    
        
    system("PAUSE");
    return 0;
}

int userInput(int &row, int &col)
{
    cout << setw(40) << "Multiplication Tables\n";
    cout << setw(40) << "---------------------\n\n" << endl << endl;
    cout << "Please Enter The Number of Rows For Table (1-12 only):  ";
    cin >> row;
    cout << endl;
    while (row < 1 or row > 12)
       {
            cout << "Incorrect Number, Please Enter The Number of Rows Again (1-12 only):  ";
            cin >> row;
            cout << endl;
       }
    
    cout << "Please Enter The Number of Columns For Table (1-12 only):  ";
    cin >> col;
    cout << endl;
    while (col < 1 or col > 12)
       {
            cout << "Incorrect Number, Please Enter The Number of Columns Again (1-12 only):  ";
            cin >> col;
            cout << endl;
       }
    
    system("cls");
    
    return(row,col);
}

int regTable(int &row,int &col)
{
    int i, j;
    
    i=0;
    j=0;
    
    
    cout << setw(36) << "Regular Multiplication Table" << endl;
    cout << setw(36) << "----------------------------" << endl << endl;
    
    for (i = 1; i <= row; i++)


        {
        for (j = 1; j <= col; j++)
        cout << setw(6) << i*j;
        cout << endl;
    }

}

int evenTable(int &row, int &col)
{
     int i, j;
    
    i=0;
    j=0;
    
    
    cout << setw(41) << "Even Numbers Multiplication Table" << endl;
    cout << setw(41) << "---------------------------------" << endl << endl;
    
    for (i = 2; i <= row; i+2)


        {
        for (j = 2; j <= col; j+2)
        cout << setw(6) << i*j;
        cout << endl;
    }
}
Hello,

The last function's for loop is wrong. The increment section be i+=2.

I don't like using do-while loops... try setting again to 1 before the loop and then just using a regular while loop. That's what I'd do. :)

Pre-Post-Script: The return for your first function doesn't seem right...

-Albatross

EDIT: I forgot the P.S, which is that everyone on this forum would recommend that you don't use system(). It's a security hole and causes a ridiculous amount of overhead.
Last edited on
Line 31:
 
       }while (again=1);


= is the assignment operator. Try ==.
Last edited on
Thank you for your quick reply Albatross. I changed the formula as you indicated and it worked.. thank you so much. But one question. How come it starts at 4? instead of 2 x 2 being the first column? Also, its not putting out the correct number of columns for the even table due to the fact of the increments of +2 subtracted from the total number of rows. Do i have to change something to get the correct number of columns to display there? its basically doing 6 columns and rows if i enter 12 and 12

as for the do-while loop, im going to have to debug it further. I was instructed to do the program with do-while loop for the repeat of the program.

as for the return of the first function.. am i returning the values of row,col wrong? it seems to be working correctly but i know thats not always a indicator of whether im doing it the correct way or not.

and after reading a ton of posts regarding the system() command, i will refrain from using it in any program that has meaning, but these little programs im messing around with shouldn't hurt anything. But i do agree that it shouldn't be used so freely...
Last edited on
No, thank you for being so patient while I take twenty years to respond! Ugh...

This is a problem that just so you know I'm taking VERY seriously (I pulled out my compiler to test it, that's how serious). The do-while loop doesn't work because it's a wrong equals sign. You had "=", not "==". I make that mistake too, and when I do it's hilarious because the whole program always crashes around my ears. :)

The return of the first function I stand corrected about. It works fine.

-Albatross, Now Dead of Old Age
Topic archived. No new replies allowed.