While won't start loop after y/n input C++ -


problem: while doesn't work start loop on when user inputs y.

i have spent hours searching forums including stack overflow clue , tried figure out (learning c++ on own). based on have read have tried: do loop works when placed @ beginning incorrectly calculates multiplying prior numb entry next entry factorial (flushing problem?), tried break , stops calculation stops without getting cout <<“do another?”. have tried adding/moving braces around statements , if statements (in desperation).

//problem #6 page 127 robert lafore oop prg c++ 4th ed. #include <iostream> using namespace std; int main() {     unsigned int numb;     unsigned long fact = 1;  //long larger numbers     char ch;      cout << "enter number  ";     cin >> numb;      (int j = numb; j > 0; j--) //multiply 1     {         fact *= j;         cout << "factorial "<< fact << endl;         cout << "do another? (y/n)\n";         cin >> ch;     }      while(ch !='n');       return 0; } 

your logic isn't quite right - need perform operation once, ask, , redo entire operation.

right now, for loop operation, character user types every loop iteration, start while loop never terminates.

do/while candidate this:

do {     fact = 1; // reinitialize subsequent loops     cout << "enter number  ";     cin >> numb;      for(int j = numb; j > 0; j--) //multiply 1     {         fact *= j;     }      cout << "factorial " << fact << endl;     cout << "do another? (y/n)\n";     cin >> ch;  }  while (ch == 'y');  // switched make other 'y' break out 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -