Friday, February 12, 2010

Need help with a C++ program, I need to know how to make the program end when the statement is false?

#include %26lt;iostream%26gt; //Required for all C++ programs


#include %26lt;iomanip%26gt; //Part of the standard library for input/output, required for setw function


using namespace std;





int main() // Beginning of program


{


int choice;


double seconds,answer;


double speed;


bool validInput = true;





cout %26lt;%26lt; ';\t\tMenu\n';;


cout %26lt;%26lt; ';---------------------------------------鈥?br>

cout %26lt;%26lt; ';1. Carbon Dioxide'; %26lt;%26lt; endl;


cout %26lt;%26lt; ';2. Air'; %26lt;%26lt; endl;


cout %26lt;%26lt; ';3. Helium'; %26lt;%26lt; endl;


cout %26lt;%26lt; ';4. Hydrogen'; %26lt;%26lt; endl;


cout %26lt;%26lt; endl;


cout %26lt;%26lt; ';Please select one of the gases from the list.'; %26lt;%26lt; endl;


cout %26lt;%26lt; ';Enter your choice, 1-4: ';;


cin %26gt;%26gt; choice;


cout %26lt;%26lt; endl;





if (choice %26lt;=0 || choice %26gt;=5)





cout %26lt;%26lt; ';You did not choose a correct number please try again.'; %26lt;%26lt; endl;


else


(choice %26gt;=1 || choice %26lt;=4);





cout %26lt;%26lt; ';Enter the time in seconds that the soundwave traveled between 1-30: ';;


cin %26gt;%26gt; seconds;


cout %26lt;%26lt; endl;





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





cout %26lt;%26lt; ';SorNeed help with a C++ program, I need to know how to make the program end when the statement is false?
You have more than one area in which the user enters a choice, If you are asking how do you end your program at a point in which the user has entered invalid information,





switch your





if (choice %26lt;=0 || choice %26gt;=5)


cout %26lt;%26lt; ';You did not choose a correct number please try again.'; %26lt;%26lt; endl;


To:


if(choice %26lt;=0 || choice %26gt;= 5)


{


cout %26lt;%26lt; ';You entered invalid information'; %26lt;%26lt; endl;


return 0;


}





The bottom switch statement you would add:





default:


cout %26lt;%26lt; ';You entered invaid number'; %26lt;%26lt; endl;


return 0;








Anytime you return from main, the program is over.Need help with a C++ program, I need to know how to make the program end when the statement is false?
first of all you need a default case in your switch down at the bottom, but what statement are you talking about?

No comments:

Post a Comment