Solved

Needs help in loop

Posted on 2011-02-23
10
414 Views
Last Modified: 2012-08-13
Hi guys: Can any one please tell me what i make mistake as it prints the table only first time also it dont terminate the loop when i press the minus sign. Thanks.

#include <iostream>
using namespace std;
int main()
{
      int multiplicand = 0;
      int product = 0;
      int multiplier = 0;
      cout << "Multiplicand (negative number to end):";
      cin >> multiplicand;
      do
      {
            do
            {
                  multiplier +=1;
                  product = multiplicand * multiplier;
                  cout << multiplicand << " * " << multiplier << " = " << product << endl;
            }      while(multiplier < 10);
            cout << endl ;
            cout << "Multiplicand (negative number to end) :";
            cin >> multiplicand;
      } while(multiplicand >=0);
      cout << endl;
      cout << "Multiplicand (negtative number to end):";
      cin >> multiplicand;
      system("Pause");
    return 0;
}  
0
Comment
Question by:mustish1
  • 4
  • 3
  • 3
10 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34966486
You need to reinitialize multiplier in the inner loop rather than the outer loop.

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 300 total points
ID: 34966491
Like this

#include <iostream>
using namespace std;
int main()
{
      int multiplicand = 0;
      int product = 0;
      cout << "Multiplicand (negative number to end):";
      cin >> multiplicand;
      do
      {
            int multiplier = 0;
            do
            {
                  multiplier +=1;
                  product = multiplicand * multiplier;
                  cout << multiplicand << " * " << multiplier << " = " << product << endl;
            }      while(multiplier < 10);
            cout << endl ;
            cout << "Multiplicand (negative number to end) :";
            cin >> multiplicand;
      } while(multiplicand >=0);
      cout << endl;
      cout << "Multiplicand (negtative number to end):";
      cin >> multiplicand;
      system("Pause");
    return 0;
}   

Open in new window

0
 

Author Comment

by:mustish1
ID: 34966497
Thanks yes i figure it out but still a problem when i press the minus sign the program wont terminate
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 34966518
The loop terminates for me when I enter a negative number.
(then it asks me for one more number before the program ends)
If I just press a minus sign without entering a number it will keep looking for a number and never find one.
0
 

Author Comment

by:mustish1
ID: 34966544
when i press the sign it just keep running.


keeprunn.jpg
0
 

Author Comment

by:mustish1
ID: 34966547
it must be terminate when i press the sign
0
 
LVL 84

Expert Comment

by:ozo
ID: 34966599
"negtative number to end"
Did you enter a negative number
0
 
LVL 84

Expert Comment

by:ozo
ID: 34966624
If you want it to end if you press something other than a number,
one way to do that could be to set multiplicand to something that is not >=0
before doing  cin >> multiplicand
so that if it fails to find a number to >> into multiplicand, it will still have a value that ends the loop.
0
 
LVL 16

Assisted Solution

by:sjklein42
sjklein42 earned 300 total points
ID: 34966649
Is there a reason you aren't using "for" loops instead of "do/while"?

Do/While will cause you much grief.  The problem with do/while is that the loop body is executed BEFORE the test.  The "for" loop does the test at the top of the loop, which is what you want.

You should redo  your code to use "for" instead of "do/while".  You cannot get it working right with do/while.
0
 

Author Closing Comment

by:mustish1
ID: 34966806
Thanks. Yes its easy with for and while.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to open text file 11 136
Message not shown 5 87
Capture logon name 13 106
Problem to Office 1 45
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question