1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Do-while statement

Discussion in 'C++' started by Naitryn, Feb 26, 2011.

  1. Naitryn

    Naitryn New Member

    Joined:
    Feb 5, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Feb 27, 2011
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Mind sharing the errors because I see you have missing } and it does not look like a C or C++ code.
     
  3. Naitryn

    Naitryn New Member

    Joined:
    Feb 5, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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 a moderator: Feb 28, 2011
  4. Naitryn

    Naitryn New Member

    Joined:
    Feb 5, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    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?
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    What does this line do?
    Code:
    multiplicand * 1;
    
    OK, more accurately: I know what it does, but what do you think it will do?
     
  6. Danaigo schwartz

    Danaigo schwartz New Member

    Joined:
    Mar 4, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page