Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Do-while statement (http://www.go4expert.com/forums/do-while-statement-t25083/)

Naitryn 27Feb2011 00:29

Do-while statement
 
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;


shabbir 27Feb2011 09:41

Re: Do-while statement
 
Mind sharing the errors because I see you have missing } and it does not look like a C or C++ code.

Naitryn 28Feb2011 00:20

Re: Do-while statement
 
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;


Naitryn 28Feb2011 00:35

Re: Do-while statement
 
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?

xpi0t0s 28Feb2011 04:30

Re: Do-while statement
 
What does this line do?
Code:

multiplicand * 1;
OK, more accurately: I know what it does, but what do you think it will do?

Danaigo schwartz 4Mar2011 07:36

Re: Do-while statement
 
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.


All times are GMT +5.5. The time now is 15:12.