Solved

Needs help in loop

Posted on 2011-02-23
10
413 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

A short article about a problem I had getting the GPS LocationListener working.
A short article about problems I had with the new location API and permissions in Marshmallow
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
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…

856 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