May 2021 3 35 Report
If/else statement variables?

Ok, first of all, thanks for actually entering the question!

Ok, I've been wondering if inside the if statement (meaning in the statement perse) I can write variables. I don't know if I made myself clear, I mean like this:

(Bare in mind I am really just starting and doing this as a hobby, so it is pretty simple for most of you most likely).

cout << "Please, indicate your gender : ";

string gender;

cin >> gender;

if (gender=="woman")

{

string subject = "she";

string owned = "her";

string self = "herself";

}

else if (gender=="man")

{

string subject = "he";

string owned = "his";

string self = "himself";

}

What I want to do, is that every time that the "gender" string variable is equal to each case, that the string variables of "subject", "owned" and "self" to be equal to what are presented in each If/else if statement.

I believe it is not wrong to do it this way, but when I want to use the variables further on, they do not appear. No error message, no nothing. Just a blank space.

" cout << " Then, something amazing happened. " << name << " was on the computer when suddenly, out of the "

<< "computer's, screen swirly " << paint << " things started comming out." << endl

<< "At first, " << name << ", thought that it wasn't really happenning. That it was all just a dream."

<< " It was evident that this couldn't be true... Maybe " << **subject** << " had gone mad?"

<< endl << endl

<< name << " kept looking at the " << paint << " things that were coming out of the screen. Suddenly, " << funny

<< " came out of the screen." << endl

<< "- No time to explain! get in the computer!- shelled " << funny << " while grabbing "<< owned

<<" sleeves really tight."

<< endl << name << " was astonished when " << subject << " could finally see what was inside the screen.";"

That is what I wrote, and this is what the outcome is:

"Then, something amazing happened. Amps was on the computer when suddenly, out of the computer's, screen swirly blue things started comming out.

At first, Amps, thought that it wasn't really happenning. That it was all just a dream. It was evident that this couldn't be true... Maybe had gone mad?

Amps kept looking at the blue things that were coming out of the screen. Suddenly, Cry came out of the screen.

- No time to explain! get in the computer!- shelled Cry while grabbing sleeves really tight.

Amps was astonished when could finally see what was inside the screen."

Thank you before hand! Please try to be simple, I am only a beginner!

Update:

And in the case I want to use "alternative" variables, as in... I want to have a set of variables that are called the same (subject, owned, self) but want them to have different values depending on the string variable gender that the user inputs. How is it then?

Please enter comments
Please enter your name.
Please enter the correct email address.
You must agree before submitting.

Answers & Comments


Helpful Social

Copyright © 2024 Q2A.ES - All rights reserved.