Sunday, August 2, 2009

Need help creating Loops for C++?

I have this code for temperature check im just a begginer on this don't blame me.


Our proffesor wont give us the code for loops.


ahhmm mainly after the prossess is done i would like to input whether i pres Y to try agian or N to shut down the program ahhm pls help! im a die hard on this thing here the code i've started with.








#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


int temperature, pressure;


cout%26lt;%26lt; "Input temperature: \n";


cin%26gt;%26gt;temperature;





cout%26lt;%26lt; "Input pressure: \n";


cin%26gt;%26gt;pressure;





if (temperature%26gt;100 || pressure%26gt;200) {


cout%26lt;%26lt;"Warning" ; }





else {


cout%26lt;%26lt;"Okey" ; }





system ("PAUSE");


return 0;





}

Need help creating Loops for C++?
sorry dont know c++ but here's a shot..





#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


string Continue;


Continue="y";


while(Continue=="y") {


int temperature, pressure;


cout%26lt;%26lt; "Input temperature: \n";


cin%26gt;%26gt;temperature;





cout%26lt;%26lt; "Input pressure: \n";


cin%26gt;%26gt;pressure;





if (temperature%26gt;100 || pressure%26gt;200) {


cout%26lt;%26lt;"Warning" ; }





else {


cout%26lt;%26lt;"Okey" ; }





system ("PAUSE");





return 0;





}


cout%26lt;%26lt; "Would you like to continue: \n";


cin%26gt;%26gt;Continue;


}
Reply:#include %26lt;iostream%26gt;


using namespace std;


int main ()


{


int temperature, pressure;


char try;


hell:


{


cout%26lt;%26lt; "Input temperature: \n";


cin%26gt;%26gt;temperature;





cout%26lt;%26lt; "Input pressure: \n";


cin%26gt;%26gt;pressure;





if (temperature%26gt;100 || pressure%26gt;200) {


cout%26lt;%26lt;"Warning" ; }





else {


cout%26lt;%26lt;"Okey" ; }


}


system ("PAUSE");


cout%26lt;%26lt;"Want to try again? Y/N";


cin%26gt;%26gt;try;


if(try=='Y')


{goto hell();}


return 0;





}











--i'm not sure bout the syntax but that's the flow...
Reply:char answer;





cout%26lt;%26lt; "Would you like to test the temp: \n";


cin%26gt;%26gt;answer;


While (answer=='y' || answer==Y')


{


cout%26lt;%26lt; "Input temperature: \n";


cin%26gt;%26gt;temperature;


cout%26lt;%26lt; "Input pressure: \n";


cin%26gt;%26gt;pressure;


if (temperature%26gt;100 || pressure%26gt;200)


{ cout%26lt;%26lt;"Warning" ;


}


else


{


cout%26lt;%26lt;"Okey" ;


}


cout%26lt;%26lt; "Would you like to test the temp:again? \n";


cin%26gt;%26gt;answer;


}//ends while loop





Remember, the key to loops, whatever condition got you into it, has to get you out of it.


No comments:

Post a Comment