Solved

Needs help in loop

Posted on 2011-02-23
10
411 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sorting efficency of sorting algorithm 30 113
Controlled Assessment GCSE - desperate help needed 4 85
Scripting vs. Programming languages 25 165
Hide vba in gp 7 95
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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.

777 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