| | |
Do/While Loop help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2004
Posts: 32
Reputation:
Solved Threads: 0
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;
}
#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;
}
The entire program should be nested in your do..while loop:
And you should consider allowing 'N' as well as 'n' to be more user friendly.
C++ Syntax (Toggle Plain Text)
int main() { int number, dig1, dig2, dig3; char answer; do { // Everything else in here } while ( answer != 'n' ); }
I'm here to prove you wrong.
•
•
Join Date: Oct 2004
Posts: 32
Reputation:
Solved Threads: 0
•
•
•
•
Originally Posted by Narue
The entire program should be nested in your do..while loop:
And you should consider allowing 'N' as well as 'n' to be more user friendly.C++ Syntax (Toggle Plain Text)
int main() { int number, dig1, dig2, dig3; char answer; do { // Everything else in here } while ( answer != 'n' ); }
I don't have any problems with it:
I even added a paranoia check to flush the input stream because that sounds like the problem you're having.
C++ Syntax (Toggle Plain Text)
#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'm here to prove you wrong.
•
•
•
•
Originally Posted by h3rtz
do{
//prog. body here
} while (answer != 'n')
cout << "goodbye!\n";
I'm here to prove you wrong.
![]() |
Similar Threads
- Excel 2007 - infinite code loop (Windows Software)
- Help with gui loop. (C)
- Loop...without the loop (Java)
Other Threads in the C++ Forum
- Previous Thread: Urgent help #3
- Next Thread: template in c++
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






