Solved

Needs help in loop

Posted on 2011-02-23
10
406 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks yes i figure it out but still a problem when i press the minus sign the program wont terminate
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
Comment Utility
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
Comment Utility
when i press the sign it just keep running.


keeprunn.jpg
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:mustish1
Comment Utility
it must be terminate when i press the sign
0
 
LVL 84

Expert Comment

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

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks. Yes its easy with for and while.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now