Monday, May 24, 2010

In dev C++, when I debug my program and run it, why does it close after i put N an answer to 1 of my questions

I'm a beginner.





here's my code. can you correct it for me if it needs it? thanx








#include %26lt;iostream%26gt;


using namespace std;





int main()


{





int age;





cout%26lt;%26lt; "Hello Reader.\n"


%26lt;%26lt; "Welcome to my C++ program.\n";





cout %26lt;%26lt; "How old are you?";


cin %26gt;%26gt; age;





if (age %26lt;13)


cout %26lt;%26lt; "You are not yet a teenager.\n"


%26lt;%26lt; "Come back when you are older.\n";


else


cout %26lt;%26lt;"Welcome Teen!!\n";





int sex;





cout%26lt;%26lt;"Are you a girl or a boy?\n";


cin%26gt;%26gt; sex;





if (sex =boy)


cout %26lt;%26lt;"Your cool.\n";





else


cout %26lt;%26lt;"Your sexy.\n";


return 0;


endl;


}

In dev C++, when I debug my program and run it, why does it close after i put N an answer to 1 of my questions
The variable "sex" is not an integer; it is a string. Please change the variable type to "string" instead.





Second of all, you need quotes around "boy".





And third, you need to put == instead of =.





It should be:





#include %26lt;iostream%26gt;


#include %26lt;string%26gt;


using namespace std;





int main()


{





int age;





cout%26lt;%26lt; "Hello Reader.\n"


%26lt;%26lt; "Welcome to my C++ program.\n";





cout %26lt;%26lt; "How old are you?";


cin %26gt;%26gt; age;





if (age %26lt;13) {


cout %26lt;%26lt; "You are not yet a teenager.\n"


%26lt;%26lt; "Come back when you are older.\n";


}


else {


cout %26lt;%26lt;"Welcome Teen!!\n";


}








cout%26lt;%26lt;"Are you a girl or a boy?\n";


string sex;


getline(cin, sex);





if (sex == "boy") {


cout %26lt;%26lt;"Your cool.\n";


}


else {


cout %26lt;%26lt;"Your sexy.\n";


}


return 0;


}

aster

No comments:

Post a Comment