3

Problem: while doesn't work to start loop over when user inputs y.

I have spent hours searching all forums including Stack Overflow for a clue and tried to figure this out (learning C++ on my own). Based on what I have read I have tried: a do loop works when placed at beginning but it incorrectly calculates by multiplying prior numb entry by next entry factorial (flushing problem?), I tried break and it stops calculation but it also stops without getting to cout <<“Do another?”. I have also tried adding/moving braces around statements and if statements (in desperation).

//problem #6 page 127 Robert Lafore OOP Prg C++ 4th ed.
#include <iostream>
using namespace std;
int main()
{
    unsigned int numb;
    unsigned long fact = 1;  //long for larger numbers
    char ch;

    cout << "Enter a number  ";
    cin >> numb;

    for (int j = numb; j > 0; j--) //multiply 1 by
    {
        fact *= j;
        cout << "Factorial is "<< fact << endl;
        cout << "Do another? (y/n)\n";
        cin >> ch;
    }

    while(ch !='n');  
    return 0;
}
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46

3 Answers3

4

Your logic isn't quite right - you need to perform the operation once, then ask, and redo the entire operation.

Right now, you do a for loop for your operation, then get the character the user types every loop iteration, then just start a while loop that never terminates.

do/while is a good candidate for this:

do
{
    fact = 1; // Reinitialize for subsequent loops
    cout << "Enter a number  ";
    cin >> numb;

    for(int j = numb; j > 0; j--) //multiply 1 by
    {
        fact *= j;
    }

    cout << "Factorial is " << fact << endl;
    cout << "Do another? (y/n)\n";
    cin >> ch;
 }
 while (ch == 'y');  // Switched to make anything other than 'y' break out
UpAndAdam
  • 4,515
  • 3
  • 28
  • 46
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • I tried this and got the same calculation error, e.g. If you enter 3, it will factor correctly to be 6. If you say y to do another and enter in 2, you get a result of 12 which is not a factor of 2, it is 2 times 6...how do I flush out previous inputs before entering a new input? – Abigail Shearer Aug 15 '13 at 23:08
  • @AbigailShearer Did you leave in the `fact = 1;` inside the do/while loop (as I have above?) It sounds like that's missing – Reed Copsey Aug 15 '13 at 23:10
  • 1
    Arrrg.(redfaced) Nope. Now I did and it works, if I understand correctly the fact=1 is a way of setting it back? Thanks so much,I learned a lot on this one! – Abigail Shearer Aug 15 '13 at 23:25
  • @AbigailShearer Yes, it's being set to 1 at the start of each iteration. – Reed Copsey Aug 15 '13 at 23:26
1

You need to do this:

do
{
    for(int j=numb; j>0; j--) //multiply 1 by
    {
        fact *=j;
    }
    cout <<"Factorial is "<< fact<<endl;
    cout <<"Do another? (y/n)\n";
    cin >> ch;
}
while (ch != 'n');

What's wrong:

Your code goes into a for loop:

for(int j=numb; j>0; j--)

It calculates the factorial...but the code to do another one is in the for loop! Your program flow is like this:

  • Enter the for loop
    • Multiply fact by j and store the value in fact
    • Print the value of fact
    • Ask to do another
      • Store the value in ch
    • Continue the for loop

See the problem? You are asked to do another for each iteration of the for loop. Then, the while loop does this:

  • Loop while ch isn't equal to 'n'.

But...the loop is infinite! After the last iteration of the for loop, ch doesn't change. So, if the last value was 'y', well, the while loop keeps on looping over...and over...and over...since it has no body, it just continues forever.

kirbyfan64sos
  • 10,377
  • 6
  • 54
  • 75
0

You have sort of the right bits of code, but the bits of code are in the wrong places.

  1. Move the output and input for "Do Another" to after the for-loop.
  2. Add do { before cout << "enter a number;`
  3. Add '} beforewhile(ch != 'n');`

Right now, you have a forever loop when ch is not 'n', since nothing changes ch inside the actual loop.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227