Thanks for the help earlier, I fixed the problem i had earlier in nested loops, but now i need help creating a Do/While Loop that is a loop above all the steps above that ask the user to continue y or n and quit unless they enter y. i know how to terminate the program.


#include <iostream>
using namespace std;

int main()
{
int number, dig1, dig2, dig3;
char answer;
cout << "Enter a Positive Integer from 0 to 999." << endl;
cin >> number;
while ((number < 0) || (number > 999))
{
cout << "Error: Negative Number or Number Greater than 999\n" << "Reenter that number and continue: \n";
cin >> number; }

dig1 = number / 100;
dig2 = (number % 100 - number % 10) / 10;
dig3 = number % 10;

for ( int i = 0; i <= dig1; i++ ) {
for ( int j = 0; j <= ( ( i == dig1 ) ? dig2 : 9 ); j++ ) {
for ( int k = 0; k <= ( ( j == dig2 ) ? dig3 : 9 ); k++ )
cout<< i << j << k <<endl;
}
}

do{
cout << "Do you want to continue (y/n)?\n";
cin >> answer;
} while (answer != 'n&#039;
cout << "goodbye!\n";

return 0;


}

Recommended Answers

All 6 Replies

The entire program should be nested in your do..while loop:

int main()
{
  int number, dig1, dig2, dig3;
  char answer;

  do {
    // Everything else in here
  } while ( answer != 'n' );
}

And you should consider allowing 'N' as well as 'n' to be more user friendly.

The entire program should be nested in your do..while loop:

int main()
{
  int number, dig1, dig2, dig3;
  char answer;

  do {
    // Everything else in here
  } while ( answer != 'n' );
}

And you should consider allowing 'N' as well as 'n' to be more user friendly.

I tried your solution already but when the user types 'n' the program runs once more and outputs goodbye before terminating.

I don't have any problems with it:

#include <iostream>
using namespace std;

int main()
{
  int number, dig1, dig2, dig3;
  char answer;

  do {
    cout << "Enter a Positive Integer from 0 to 999." << endl;
    cin >> number;
    while ((number < 0) || (number > 999))
    {
      cout << "Error: Negative Number or Number Greater than 999\n" << "Reenter that number and continue: \n";
      cin >> number;
    }

    dig1 = number / 100;
    dig2 = (number % 100 - number % 10) / 10;
    dig3 = number % 10;

    for ( int i = 0; i <= dig1; i++ ) {
      for ( int j = 0; j <= ( ( i == dig1 ) ? dig2 : 9 ); j++ ) {
        for ( int k = 0; k <= ( ( j == dig2 ) ? dig3 : 9 ); k++ )
          cout<< i << j << k <<endl;
      }
    }

    cout << "Do you want to continue (y/n)?\n";
    while ( cin.get ( answer ) && answer != '\n' )
      ;
    cin >> answer;
  } while (answer != 'n');

  cout << "goodbye!\n";

  return 0;
}

I even added a paranoia check to flush the input stream because that sounds like the problem you're having.

do{
//prog. body here
} while (answer != 'n')
cout << "goodbye!\n";

do{
//prog. body here
} while (answer != 'n')
cout << "goodbye!\n";

You're about six days too late, slick. If you're going to take that long to post a reply to a question that was already answered then at least add something entertaining to the thread.

#include<iostream.h>
#include<conio.h>
void main()
   {
         clrscr();
         int n, i, fac;
         fac=1;
         i=1;
         do 
               {
                            fac*=n;
                            i++;
               }
         while(i<=n);
         cout<<"\n\n Answer --> "<<fac;
         getch();
   }


//OUTPUT --> This will get the factorial of any number
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.