Sunday, July 12, 2009

Please help me! Something is wrong with my C++ program?

// bla bla


#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;


using namespace std;


int main()





{





int seconds;


double distance;


int choice;





const double CarbonDioxide = 258.0;


const double Air = 331.5;


const double Helium = 972.0;


const double Hydrogen = 1270.0;





cout %26lt;%26lt; "\t\tSelect the disired gas\n\n";


cout %26lt;%26lt; "1. Carbon Dioxide\n";


cout %26lt;%26lt; "2. Air\n";


cout %26lt;%26lt; "3. Helium\n";


cout %26lt;%26lt; "4. Hydrogen\n";


cout %26lt;%26lt; "5. Quit\n";


cin %26gt;%26gt; choice;








if (choice == 1)


{


cout %26lt;%26lt; "enter seconds ";


cin %26gt;%26gt; seconds;


if (seconds %26lt; 0 || seconds %26gt; 30)





{


cout %26lt;%26lt; "You must enter seconds\n";


cout %26lt;%26lt; "that are in the range\n";


cout %26lt;%26lt; "of 0-30." %26lt;%26lt; endl;


}


else if (seconds %26gt; 0 || seconds %26lt; 30)


distance = seconds * CarbonDioxide;


cout %26lt;%26lt; "The total distance was.." %26lt;%26lt; distance %26lt;%26lt; endl;


}


else if (choice == 2)


{


cout %26lt;%26lt; "enter seconds ";


cin %26gt;%26gt; seconds;


if (seconds %26lt; 0 || seconds %26gt; 30)


{


cout %26lt;%26lt; "You must enter secon

Please help me! Something is wrong with my C++ program?
Yes what is happening is that this line:


distance = seconds * CarbonDioxide;


gets executed only if this condition evaluates to true:


if (seconds %26gt; 0 || seconds %26lt; 30)





Now what happens if it doesn't and you try to use the value inside distance?





A quick solution to the problem would be to replace


this:


double distance;


with this:


double distance = 0;
Reply:You misspelled 'desired'.


No comments:

Post a Comment