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.
#include <iostream>
#include <cmath>
usingnamespace 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
#include <iostream>
#include <cmath>
usingnamespace 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
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(unsignedint 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:
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.
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)
#include <iostream>
#include <cmath>
usingnamespace 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
#include <iostream>
#include <cmath>
usingnamespace 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
#include <iostream>
#include <cmath>
usingnamespace 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.
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?