Simple C++ Variables

Posts 16–30 of 32 · Page 2 of 3
Quote Originally Posted by theelement12 View Post
No problem. Have fun
Hey, I know you're learning just like me but quick question. Instead of using a number guessing thing, in which you would use, like you did int ThisIsANumber; what is the prefer to store characters? Wouldn't it be like string SuperSecretPassword; for example?

OK WELL EDIT! I figured out that string is correct, but now I have this dillema.
Code:
#include <iostream>

using namespace std;

int main()
{
    string Answer;
    string BluePill;
    
    BluePill = 10;
    
    cout << "Welcome to the matrix, should you choose the BluePill " << endl;
    cout << "You will return to what you refer to as normal, and live out " << endl;
    cout << "the rest of your meaningless life. OR, you could choose the " << endl;
    cout << "Bluepill, in which case your life will change forever. You will " << endl;
    cout << "be immersed into a world like nothing you've seen before. " << endl;
    cout << "So the choice is your's, the RedPill, or The BluePill? " << endl;
    cout << "Please enter your answer, either RedPill, or BluePill :" << endl;
    cin.get();
    cin.get();
    cin >> Answer;
    cin.ignore();
    
    if (Answer == BluePill)
    {
               cout << "Welcome to the Matrix";
    }
    else
    {
        cout << "Good luckk with the rest of your pathetic life moron.";
    }

    return 0;
}
What happens is the window opens, it shows all my writing and such, but regardless of my input answer I receive: Good Luckk with the rest of your pathetic life moron.

Also, regardless of my placement of cin.get(); / cin.ignore(); the window doesn't stay open after my input.

Help?

EDIT AGAIN!

I believe I may have run into an issue when I tried to make BluePill a variable, because I can't just declare it with out defining it, and because I can't really think of a definition of blue pill, I just gave it the value of ten, but since the possible answer isn't ten, then regardless RedPill Or BluePill will draw me the answer "Good luckk with the rest of your Pathetic Life Moron"

So then I believe my bigger question is.....How do I declare a string variable without having to give it an integral definition?

EDIT AGAIN! So many edits!

I had to fix a few things in this also before I compiled, I originally wanted to make it Red Pill or Blue Pill, but my compiler had a problem dealing with the space inbetween so I had to switch to RedPill or BluePill.

So another question would be: is there a way to declare a string variable with a space or do I just have to make it work? Or instead of using a string variable is there a more efficient, be it more advanced, way to do this so that I don't just keep trying to find out there's no possible way.

EDIT YET AGAIN! HAHA!

I figured out my window closing problem, and then I thought about WHEN it was closing. It was closing after the ELSE portion of the IF, Then, Else Loop. So I added cin.get(); after the Else statement, and now my window remains open. I believe my only remaining problem in the code is I'm not properly declaring my string variable. As I even changed the value of BluePill from BluePill == 10 to BluePill = BluePill still to no avail.
Quote Originally Posted by ilovecookies View Post
Hey, I know you're learning just like me but quick question. Instead of using a number guessing thing, in which you would use, like you did int ThisIsANumber; what is the prefer to store characters? Wouldn't it be like string SuperSecretPassword; for example?

OK WELL EDIT! I figured out that string is correct, but now I have this dillema.
Code:
#include <iostream>

using namespace std;

int main()
{
    string Answer;
    string BluePill;
    
    BluePill = 10;
    
    cout << "Welcome to the matrix, should you choose the BluePill " << endl;
    cout << "You will return to what you refer to as normal, and live out " << endl;
    cout << "the rest of your meaningless life. OR, you could choose the " << endl;
    cout << "Bluepill, in which case your life will change forever. You will " << endl;
    cout << "be immersed into a world like nothing you've seen before. " << endl;
    cout << "So the choice is your's, the RedPill, or The BluePill? " << endl;
    cout << "Please enter your answer, either RedPill, or BluePill :" << endl;
    cin.get();
    cin.get();
    cin >> Answer;
    cin.ignore();
    
    if (Answer == BluePill)
    {
               cout << "Welcome to the Matrix";
    }
    else
    {
        cout << "Good luckk with the rest of your pathetic life moron.";
    }

    return 0;
}
What happens is the window opens, it shows all my writing and such, but regardless of my input answer I receive: Good Luckk with the rest of your pathetic life moron.

Also, regardless of my placement of cin.get(); / cin.ignore(); the window doesn't stay open after my input.

Help?

EDIT AGAIN!

I believe I may have run into an issue when I tried to make BluePill a variable, because I can't just declare it with out defining it, and because I can't really think of a definition of blue pill, I just gave it the value of ten, but since the possible answer isn't ten, then regardless RedPill Or BluePill will draw me the answer "Good luckk with the rest of your Pathetic Life Moron"

So then I believe my bigger question is.....How do I declare a string variable without having to give it an integral definition?

EDIT AGAIN! So many edits!

I had to fix a few things in this also before I compiled, I originally wanted to make it Red Pill or Blue Pill, but my compiler had a problem dealing with the space inbetween so I had to switch to RedPill or BluePill.

So another question would be: is there a way to declare a string variable with a space or do I just have to make it work? Or instead of using a string variable is there a more efficient, be it more advanced, way to do this so that I don't just keep trying to find out there's no possible way.

EDIT YET AGAIN! HAHA!

I figured out my window closing problem, and then I thought about WHEN it was closing. It was closing after the ELSE portion of the IF, Then, Else Loop. So I added cin.get(); after the Else statement, and now my window remains open. I believe my only remaining problem in the code is I'm not properly declaring my string variable. As I even changed the value of BluePill from BluePill == 10 to BluePill = BluePill still to no avail.
=).... A string variable, such as your BluePill holds a string, which is text enclosed within double quotes. For example
[php]
string bluepill = "Blue pill";
[/php]

Thus now you can check if the string inside the variable answer, is the same as the string inside the variable BluePill. Of course, currently it is case sensitive. To make it not you would have to do some other things.

But the BluePill variable isn't even that necessary, it would probably be simpler just to do something like this:
[php]
if (answer == "Blue Pill")
{
Do whatever
}
Else
{
Whatever again
}
[/php]

Which is checking if the string inside the variable answer is the same as the string "Blue Pill".

I probably explained that badly though
FINALLY! With the help of TheElement12, Zeco, and Everyone else who posted here, came upon

Code:
#include <iostream>

using namespace std;

int main()
{
    string Answer;
    string BluePill;
    
    BluePill = BluePill;
    
    cout << "Welcome to the matrix, should you choose the BluePill " << endl;
    cout << "You will return to what you refer to as normal, and live out " << endl;
    cout << "the rest of your meaningless life. OR, you could choose the " << endl;
    cout << "Bluepill, in which case your life will change forever. You will " << endl;
    cout << "be immersed into a world like nothing you've seen before. " << endl;
    cout << "So the choice is your's, the RedPill, or The BluePill? " << endl;
    cout << "Please enter your answer, either RedPill, or BluePill :" << endl;
    cin >> Answer;
    cin.ignore();
    
    if (Answer == "BluePill" )
    {
               cout << "Welcome to the Matrix";
               cin.get();
    }
    else
    {
        cout << "Good luck with the rest of your pathetic life moron.";
        cin.get();
    }

    return 0;
}
If you wanna try it out, just compile and run, Type in BluePill and it will say "Welcome to the matrix." and anything else will get the response, "Good luck with the rest of your pathetic life moron."

Now i'mma see If I can't get a guessing game, that if you get the wrong answer, it says "Try again." And starts over.
Quote Originally Posted by Matrix_NEO006 View Post
i quoted your code.
B-but ...there are no namespaces >_< Or includes! It burns, it burns!
Quote Originally Posted by Matrix_NEO006 View Post
i quoted your code.
Awwww, and you made it look so much betterful. I Should've done it like that, I eventually figured out I didn't even need the BluePill string variable, so I just made a single variable string Answer; and made it store the inputted answer into the string Answer, and do an if else, If (answer == "Blue Pill)

So now I feel i'm ready to move on a bit further in my book since i've comprehended a few of the basic things.

Again thanks to all who helped! =D And awesomeness for quoting me!
I don't get it every time i write in any number it tell s me "number you have netred is 547" and than it shut down man!
LOL i never heard of that problem man maybe ur compiler is crazy
somehow I hate topics like these o__O
when i just started programming i ran into the same problems.

solution:

finish the C++ basic book and all your questions will be answered.
I like how this turned into a general discussion. LOL.
Quote Originally Posted by theelement12 View Post
I like how this turned into a general discussion. LOL.
Lol Yeah man sorry I kinda thread jacked you.
Nah you didn't. Your better at C++ than me. I only work on it during the weekends.
My old post I hope I get my account unbanned. Haha.
Quote Originally Posted by hlm2 View Post
My old post I hope I get my account unbanned. Haha.
Ummm... its obviously unbanned or it would have [BANNED] next to it o_O...
Posts 16–30 of 32 · Page 2 of 3
This thread is closed for replies.

Similar Threads

Tags for this Thread

Talk with us