| | |
Do/While Loop help
![]() |
•
•
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 |
ace_thread api array based binary bitmap borland c++ c++endcode c/c++ calling char class classes code coding compile connect console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node output parameter pointer problem program programming project python random read recursion reference remote reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock wordfrequency wxwidgets






