Beginner's Tinkering | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Beginner's Tinkering

Can anyone tell me what's wrong with this string of code? I have been fooling around with what I have learned so far (very little) and have gotten stuck using the "if" statement. #include <iostream> using namespace std; int main() { int age; int born; int sex; cout << "how old are you? "; cin >> age; cout << "\nwhen were you born? "; cin >> born; cout << "\nare you a man or a woman? "; cin >> sex; if (sex = "male") { born + 76; } else { born + 81; } cout << "\nYou will probably die in the year AD." << born << "unless you're the next Rick James, bitch" << endl; return 0; } There are probably a ton of "issues" with the above but since I don't know proper coding etiquette yet, so bear with me.

18th Jul 2017, 1:24 AM
Ryan krytenberg
Ryan krytenberg - avatar
4 Answers
+ 10
Your etiquette is fine, don't worry about that. Just two adjustments: First, int is only for whole numbers. If you want to input text for sex, add this to the top of your code: #include <string> and changed sex's type to string: string sex; And second, to check if two things are equal, use == (equality operator). If you only use single = (assignment operator), you'll assign "male" to sex every time, and the condition will always be true.
18th Jul 2017, 1:34 AM
Tamra
Tamra - avatar
+ 5
The one thing that definitely jumps out at me: The equals sign (=) is used for assignment. Meaning that, x = 3 sets the variable x to 3. The equality operator (==) is used for comparison; returning true if two things are equal, and false otherwise. So, when comparing sex to "male", it must be a double-equals sign.
18th Jul 2017, 1:33 AM
Keto Z
Keto Z - avatar
+ 4
#include <iostream> using namespace std; int main() { int age; int born; string sex; // change to string cout << "how old are you? "; cin >> age; cout << "\nwhen were you born? "; cin >> born; cout << "\nare you a man or a woman? "; cin >> sex; if (sex == "male") { // use 2 equals signs == not 1 = born += 76; // change operator so that the value is saved into the variable } else { born += 81; // change operator so that the value is saved into the variable } // add some spaces so that it prints correctly cout << "\nYou will probably die in the year AD. " << born << " unless you're the next Rick James, bitch" << endl; return 0; } Other than this it looks good!
18th Jul 2017, 1:38 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Awesome, thanks guys (and gals)! Really nice to have such an active community, lol I wasn't expecting such a quick response. This is my second day working on this stuff but so far I'm loving it!
18th Jul 2017, 1:58 AM
Ryan krytenberg
Ryan krytenberg - avatar