Do/While Loop help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Do/While Loop help

 
0
  #1
Oct 3rd, 2004
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;


}
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Do/While Loop help

 
0
  #2
Oct 3rd, 2004
The entire program should be nested in your do..while loop:
  1. int main()
  2. {
  3. int number, dig1, dig2, dig3;
  4. char answer;
  5.  
  6. do {
  7. // Everything else in here
  8. } while ( answer != 'n' );
  9. }
And you should consider allowing 'N' as well as 'n' to be more user friendly.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 32
Reputation: dontcare is an unknown quantity at this point 
Solved Threads: 0
dontcare dontcare is offline Offline
Light Poster

Re: Do/While Loop help

 
0
  #3
Oct 3rd, 2004
Originally Posted by Narue
The entire program should be nested in your do..while loop:
  1. int main()
  2. {
  3. int number, dig1, dig2, dig3;
  4. char answer;
  5.  
  6. do {
  7. // Everything else in here
  8. } while ( answer != 'n' );
  9. }
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Do/While Loop help

 
0
  #4
Oct 3rd, 2004
I don't have any problems with it:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int number, dig1, dig2, dig3;
  7. char answer;
  8.  
  9. do {
  10. cout << "Enter a Positive Integer from 0 to 999." << endl;
  11. cin >> number;
  12. while ((number < 0) || (number > 999))
  13. {
  14. cout << "Error: Negative Number or Number Greater than 999\n" << "Reenter that number and continue: \n";
  15. cin >> number;
  16. }
  17.  
  18. dig1 = number / 100;
  19. dig2 = (number % 100 - number % 10) / 10;
  20. dig3 = number % 10;
  21.  
  22. for ( int i = 0; i <= dig1; i++ ) {
  23. for ( int j = 0; j <= ( ( i == dig1 ) ? dig2 : 9 ); j++ ) {
  24. for ( int k = 0; k <= ( ( j == dig2 ) ? dig3 : 9 ); k++ )
  25. cout<< i << j << k <<endl;
  26. }
  27. }
  28.  
  29. cout << "Do you want to continue (y/n)?\n";
  30. while ( cin.get ( answer ) && answer != '\n' )
  31. ;
  32. cin >> answer;
  33. } while (answer != 'n');
  34.  
  35. cout << "goodbye!\n";
  36.  
  37. return 0;
  38. }
I even added a paranoia check to flush the input stream because that sounds like the problem you're having.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 11
Reputation: h3rtz is an unknown quantity at this point 
Solved Threads: 0
h3rtz h3rtz is offline Offline
Newbie Poster

Re: Do/While Loop help

 
1
  #5
Oct 8th, 2004
do{
//prog. body here
} while (answer != 'n')
cout << "goodbye!\n";
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Do/While Loop help

 
0
  #6
Oct 8th, 2004
Originally Posted by h3rtz
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 1
Reputation: shubh93 is an unknown quantity at this point 
Solved Threads: 0
shubh93 shubh93 is offline Offline
Newbie Poster

To Get the Factorial Of any Number

 
0
  #7
Oct 10th, 2009
#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
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC