Solved

Needs help in loop

Posted on 2011-02-23
10
415 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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

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.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 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