Do - While Loop for "For" Multiplication Table

Hello. I need help regarding displaying a multiplication table for an inputted number. In the first input, I can get the program to display the table, but when it asks for the next number, it just keeps on asking without displaying the table.


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
  
#include <iostream>
#include <cmath>

using namespace std;

int main ()

{

    //declare variables
  
    int total              = 0;
    int multiplicand       = 0;
    int multiplier         = 0;
    
    //input
    
    do 
    
    {
        
    cout << "Enter Number for Multiplication Table: ";
    cin >> multiplicand;
    cout << endl;
    
    //processing and output
  
               for ( multiplier >= 1; multiplier <= 10; multiplier += 1)
               
               {
                   total = multiplicand * multiplier;
                   cout << multiplicand << " multiplied by " << multiplier << " is equal to " << total << endl;
                   cout << endl;
                                    
               }   //end for          
    
     }    while ( multiplicand != -1 );
    
    return 0;
    
}   //end of main function

 
for ( multiplier >= 1; multiplier <= 10; multiplier += 1)


change this to

 
for ( multiplier = 1; multiplier <= 10; multiplier += 1)


EDIT: Also, you do not need to include <cmath> for this purpose
Last edited on
You need to reset multiplier.

Edit: abhishekm71 has expressed this more sensibly.

In C++ it would be even better to use

for (int multiplier = 1; multiplier <= 10; multiplier += 1)

and remove the variable declaration from further up, as multiplier is only used within the scope of the for loop.

And a similar thing could (should) be done for total. As it is only used within the scope of the for loop, it should be defined there.

Basically, you define variables in as tight a scope as possible, preferably at point of first use.

Andy

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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    //declare variables

    int multiplicand = 0;

    //input

    do
    {
        cout << "Enter Number for Multiplication Table: ";
        cin >> multiplicand;
        cout << endl;

        //processing and output

        for (int multiplier = 1; multiplier <= 10; multiplier += 1)
        {
            int total = multiplicand * multiplier;
            cout << multiplicand << " multiplied by " << multiplier
                 << " is equal to " << total << endl;
            cout << endl;
        } //end for
    } while ( multiplicand != -1 );

    return 0;

} //end of main function 

Last edited on
Thank you for the answer. I knew there was something wrong in my "For" loop.

Can you please explain why it should be "=" and not ">="? Is it because it is just an initialization?
The proper syntax of a for loop:

for( [variable initialization]; [condition in which not to break]; [any post-actions])

Example:

Psuedo Code:
1
2
3
4
5
string s = "Whatever you want this string to be";
for(unsigned int x = 0; x < s.size(); x++) //from 0, to the size of the string
{
    cout<< s[x]<< endl;
}


We could also add other conditions, like:

This would loop until either a period in the string, or we have gone out of the size of the string.
Psuedo Code:
1
2
3
4
5
6
7
8
bool dotmet = false;
for(unsigned int x = 0; ((x < s.size()) && !dotmet); x++)
{
    if(s[x] == '.')
    {
        dotmet = true;
     }
}


So there are many things you can do with this.
Can you please explain why it should be "=" and not ">="? Is it because it is just an initialization?

Yes.

Andy

PS As spelt out in IWishIKnew's post.

PPS But not "just" -- it's important!
Last edited on
Yes.

Andy

PS As spelt out in IWishIKnew's post.

PPS But not "just" -- it's important!


Oh, that's why I'm having trouble finding the error.

If I were to make it a pretest loop, would I just put the while right below the do, or should I still have to make it a for loop or while loop?
If I were to make it a pretest loop, would I just put the while right below the do, or should I still have to make it a for loop or while loop?

I am unclear about what "it" is!?

If you're talking about the inner loop, then a for-loop is the best fit here.

I don't think it matter here whether the outer loop is while or do-while, but you prob need to add an if-test to you don't output a -1 times table.

Andy
I am unclear about what "it" is!?

If you're talking about the inner loop, then a for-loop is the best fit here.

I don't think it matter here whether the outer loop is while or do-while, but you prob need to add an if-test to you don't output a -1 times table.

Andy


Well, I'm talking about the whole code or program itself. I knew that the for-loop and the while-loop are pretest loops, but I have a for-loop already in the program, so I'm a bit confused on how to make a pretest loop out of the codes I have.
Define "pretest loop".

I also don't see any purpose for the do-while loop. I think you should get rid of it.
Last edited on
I think he probably means 'loop in which condition is checked prior to 1st loop execution'.

I personally prefer the for loop for most instances because I feel it offers much flexibility, but I don't know whether there is any performance implication in using one loop over another.
I also don't see any purpose for the do-while loop.

It asks you for a series of numbers, so you can get it to display a number of times tables. When you get bored, you enter -1 to exit the loop.

I'm a bit confused on how to make a pretest loop out of the codes I have.

It makes little odds here; see below.

And as all loops end up as some set of jumps (conditional and unconditional) when converted to their machine code equivalent, I don't see why any sensible use of while, do-while, or for loop would be worse than any of the others.

With do-while (now with if-test to stop output of -1 times table)

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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    //declare variables
    int multiplicand = 0;

    do
    {
        //input
        cout << "Enter Number for Multiplication Table (or -1 to exit): ";
        cin >> multiplicand;
        cout << endl;

        if (-1 != multiplicand)
        {
            //processing and output
            for (int multiplier = 1; multiplier <= 10; multiplier += 1)
            {
                int total = multiplicand * multiplier;
                cout << multiplicand << " multiplied by " << multiplier
                     << " is equal to " << total << endl;
                cout << endl;
            } //end for
        } //end if
    } while ( multiplicand != -1 );

    return 0;

} //end of main function  


with while

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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    //declare variables
    int multiplicand = 0;

    while ( multiplicand != -1 )
    {
        //input
        cout << "Enter Number for Multiplication Table (or -1 to exit): ";
        cin >> multiplicand;
        cout << endl;

        if (-1 != multiplicand)
        {
            //processing and output
            for (int multiplier = 1; multiplier <= 10; multiplier += 1)
            {
                int total = multiplicand * multiplier;
                cout << multiplicand << " multiplied by " << multiplier
                     << " is equal to " << total << endl;
                cout << endl;
            } //end for
        } //end if
    } //end while

    return 0;

} //end of main function  


and for

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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
    for ( int multiplicand = 0; multiplicand != -1; /*updated using cin*/  )
    {
        //input
        cout << "Enter Number for Multiplication Table (or -1 to exit): ";
        cin >> multiplicand;
        cout << endl;

        if (-1 != multiplicand)
        {
            //processing and output
            for (int multiplier = 1; multiplier <= 10; multiplier += 1)
            {
                int total = multiplicand * multiplier;
                cout << multiplicand << " multiplied by " << multiplier
                     << " is equal to " << total << endl;
                cout << endl;
            } //end for (int multiplier ...
        } //end if
    } //end for ( int multiplicand ...

    return 0;

} //end of main function  


Andy

PS You can get rid of the if-test if you move the input into a helper funcion, e.g.

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
#include <iostream>
#include <cmath>

using namespace std;

//forward declarations
int getMultiplicand();

int main ()
{
    //declare variables
    int multiplicand = 0;
    while (-1 != (multiplicand = getMultiplicand()))
    {
        //processing and output
        for (int multiplier = 1; multiplier <= 10; multiplier += 1)
        {
            int total = multiplicand * multiplier;
            cout << multiplicand << " multiplied by " << multiplier
                    << " is equal to " << total << endl;
            cout << endl;
        } //end for (int multiplier ...
    } //end for ( int multiplicand ...

    return 0;

} //end of main function 

int getMultiplicand()
{
    //declare variables
    int multiplicand = 0;
    //input
    cout << "Enter Number for Multiplication Table (or -1 to exit): ";
    cin >> multiplicand;
    cout << endl;
    return multiplicand;
}
Last edited on
@Andy +1
And as all loops end up as some set of jumps (conditional and unconditional) when converted to their machine code equivalent, I don't see why any sensible use of while, do-while, or for loop would be worse than any of the others.


Good thing I understood now the concept of the different kinds of loops. I really appreciate the help I got here. I'm sure this would help me a lot in my subject.

Am I right that the pretest and posttest loops work the same?
Zenfire:
Am I right that the pretest and posttest loops work the same?


If you're refering to the while() and do/while() loops: no, you are wrong.

Do while loops will always execute at least once before executing the break condition.
1
2
3
4
5
do
{
    /* ... */
}
while( ... );


while loops will execute the condition to break before executing the code.

for loops are while loops, but with limitations that are numeric, or otherwise.
Topic archived. No new replies allowed.