Variables w/ letters instead of numbers?

Started by
8 comments, last by Chris Hare 19 years, 7 months ago
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
Advertisement
It's hard to help you if you don't mention what programming language you're using.
Ah yes, sorry, that slipped my mind (hectic week), I'm using C++.

~Mist Crawler
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.
Ra
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
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.
Ra
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;}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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
Thanks alot!!

~Mist Crawler
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]

This topic is closed to new replies.

Advertisement