Do-while statement

Naitryn's Avatar, Join Date: Feb 2011
Newbie Member
I'm supposed to write a code in do-while statement...I've done the majority of it, but I know there's errors with it....how do I fix it?


Code:
#include <iostream>
using namespace std;

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

    //input values
    cout << "Multiplicand (negaqtive number to end): ";
    cin >> multiplicand;

    //calculations
    do 
    {
         (multiplicand >= 0);
            (multiplier = 1)
                (multiplier < 10);
            (multiplier += 1);
            {
                while 
                    product=multiplicand * multiplier;
                cout << multiplicand << "*"
                    << multiplier << "="
                    << product << endl;
            }
    //display 
            cout << endl;
            cout << "Multiplicand (negative number to end): ";
            cin >> multiplicand;
    
    return 0;

Last edited by shabbir; 27Feb2011 at 09:41.. Reason: Code blocks
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Mind sharing the errors because I see you have missing } and it does not look like a C or C++ code.
0
Naitryn's Avatar, Join Date: Feb 2011
Newbie Member
Okay, I fixed it a little bit and it is supposed to be a C++ code but it's still not running properly and I'm not entirely sure where the error is. I need to make sure the code has post test loops in it and the particular problem is to allow one or more multiplication tables and the multiplier allows 1-9. What do I need to do?

Code:
//Directives
#include <iostream>
using namespace std;

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

    //input values
    cout << "Multiplicand (negaqtive number to end): ";
    cin >> multiplicand;

    //calculations
    do {
         multiplicand * 1;
             cout << multiplicand << endl;
    }
                while (multiplier < 10);
                
    //display 
            cout << endl;
            cout << "Multiplicand (negative number to end): ";
            cin >> multiplicand;
    
    return 0;

Last edited by shabbir; 28Feb2011 at 06:44.. Reason: Code blocks
0
Naitryn's Avatar, Join Date: Feb 2011
Newbie Member
Crap. I got the thing to run, but when I enter the multiplicand....all it does is repeat the number. Constantly. Oops. I guess I messed up somewhere. Any ideas, please?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
What does this line do?
Code:
multiplicand * 1;
OK, more accurately: I know what it does, but what do you think it will do?
0
Danaigo schwartz's Avatar, Join Date: Mar 2011
Newbie Member
Hello I am a student at UAT
I think I see your problem at lteast part of it.
If it repeats the number entered continually it sounds like a looping error. so lets look at loops.

Code:
 do {
         multiplicand * 1;
             cout << multiplicand << endl;
    }
                while (multiplier < 10);
Now a do / while loop will do something repeatedly until the conditions in the while branch are met.
so if it constantly repeats a number over and over in the cout, then the problem must be here somewhere. My advice to stop the first problem of repeating numbers is fix the loop.

As for the logic error ( that would be the calculations going wrong) you should start with some psuedo code. For instance, what are you trying to do.
are you trying to multiply the numbers up until the "multiplicand" using the multiplier?

If so then your logic should read like this:
multiply my multiplicand by my multiplier
out put my result
add one to my multiplier
check if my multiplier is equal to my multiplicand
while it is not repeat the process.

It will help if you get into a habit of writing this down, then use these comments as commentary for the code. That will help those of us helping you understand your problem better and it will better define what you are trying to accomplish.