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?
Copyright © 2024 Q2A.ES - All rights reserved.
Answers & Comments
Verified answer
Variables declared within an if/else statement can only be used within that if/else statement.
Variables declared inside a conditional statement go out of scope once the end of that conditional statement is reached. In other words, this:
int a=3;
if(a==3)
{
int b=5;
}
int c=b;
is invalid and will be rejected by the compiler, but this:
int a=3;
int b=0;
if(a==3)
{
b=5;
}
int c=b;
is perfectly valid and will compile and execute, exhibiting the intended logic. Note how in the second case, b was declared before the conditional statement began.
Since you are declaring and setting the values of the subject, owned and self variables within the if/else blocks they only exist within those blocks. Since you aren't getting an error I assume you have also declared these variables outside the if/else blocks, so all you have to do is assign the values to them and not re-declare them. For example:
owned = "her";