Sign in to follow this  

Variables w/ letters instead of numbers?

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know this has to be the ultimate newbie question but no matter how many times I try I can't get it right, and no matter how many times I check through my book i can't find it. How do I use a variable to store words, or a phrase, ex. a name or a city. Thanks in advance to anyone who can help me! ~Mist Crawler

Share this post


Link to post
Share on other sites
In C++ you can use a std::string.

#include <string>

...

std::string my_string = "blah!";

More info about C++ strings and C-style strings here.

Share this post


Link to post
Share on other sites
If you #include <iostream> you can use std::cin >> my_string; to accept input from the user. You can avoid using all the std:: prefixes by putting using namespace std; at the top of your files (after the includes).

This covers input and output with cin and cout.

EDIT: Changed to a better link.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mist Crawler
Thank you, and just one more question, is there any way I could do that but with the user inputting the variable? (ex. answering a question)

~Mist Crawler



#include <string>
#include <iostream>

int main()
{
std::cout << "Answer the question!" << std::endl;

std::string answer;
std::getline(std::cin, answer);

std::cout << "You answered: " << answer << std::endl;
}

Share this post


Link to post
Share on other sites
The elementary unit of character storage is a 'char.' std::string manages a 'string' of these, though it isn't abstract enough to cast away char.

i.e.
char A = 'A';
cout << A << endl; // writes A

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
It's hard to help you if you don't mention what programming language you're using.

Clearly one of SiCraneBot's predefined clause messages.[wink]

Share this post


Link to post
Share on other sites

This topic is 4856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this