![]() |
| ||
| Do/While Loop help 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' cout << "goodbye!\n"; return 0; } |
| ||
| Re: Do/While Loop help The entire program should be nested in your do..while loop: int main()And you should consider allowing 'N' as well as 'n' to be more user friendly. |
| ||
| Re: Do/While Loop help Quote:
|
| ||
| Re: Do/While Loop help I don't have any problems with it: #include <iostream>I even added a paranoia check to flush the input stream because that sounds like the problem you're having. |
| ||
| Re: Do/While Loop help do{ //prog. body here } while (answer != 'n') cout << "goodbye!\n"; |
| ||
| Re: Do/While Loop help Quote:
|
| ||
| To Get the Factorial Of any Number #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 |
| All times are GMT -4. The time now is 1:11 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC