Thursday, July 30, 2009

C++ Programming Question. How do I insert a loop to continually repeat this entire sequence?

#include %26lt;iostream%26gt;





using namespace std;





int main()


{


int pH;


cout%26lt;%26lt;"please tpye the pH to the nearest whole number: ";


cin%26gt;%26gt; pH;


cin.ignore();


if ( pH %26lt; 7 ) {


cout%26lt;%26lt;"This pH level is acidic \n";


}


if ( pH %26gt; 7 ) {


cout%26lt;%26lt;"This pH level is basic \n";


}


if ( pH == 7 ) {


cout%26lt;%26lt;"The pH is neutral \n";


}


if ( pH %26lt; 0 ) {


cout%26lt;%26lt;"Please reenter a pH level between 0 and 14 \n";


}


if ( pH %26gt; 14 ) {


cout%26lt;%26lt;"Please reenter a pH level between 0 and 14 \n";


}


cin.get();


}


--- How do you insert the loop here to continually repeat the above when a user hits enter? Thanks for any help.

C++ Programming Question. How do I insert a loop to continually repeat this entire sequence?
wouldn't you just surround it with a do...while block?





For example,





do {


cout %26lt;%26lt; "Please enter...





%26lt;rest of your code%26gt;





} while (pH !='Q');
Reply:int main()


{


int pH;


while( 1 ) {


cout%26lt;%26lt;"please tpye the pH to the nearest whole number: ";


cin%26gt;%26gt; pH;


cin.ignore();





if ( pH %26lt; 0 ) {


cout%26lt;%26lt;"Please reenter a pH level between 0 and 14 \n";


} else if ( pH %26gt; 14 ) {


cout%26lt;%26lt;"Please reenter a pH level between 0 and 14 \n";


} else if ( pH %26lt; 7 ) {


cout%26lt;%26lt;"This pH level is acidic \n";


} else if ( pH %26gt; 7 ) {


cout%26lt;%26lt;"This pH level is basic \n";


} else if ( pH == 7 ) {


cout%26lt;%26lt;"The pH is neutral \n";


}


}


}
Reply:Why would you want to enter an infinite loop. This is a dangerous memory and cpu hog if not done carefully. You should always have an exit point or at least a sleep point so you don't chew the cpu up.





/*Very bad PsuedoCode


Yourloop


Do Something


Sleep for 20 seconds


Pick an exit situation here...never CONTINUOUSLY loop, you want to exit sometime in the future


Loop
Reply:im not really sure wat u mean.......? could you explain a bit more


wat do u want to repeat?/


No comments:

Post a Comment