Variables w/ letters instead of numbers?
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
In C++ you can use a std::string.
More info about C++ strings and C-style strings here.
#include <string>...std::string my_string = "blah!";
More info about C++ strings and C-style strings here.
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
~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.
This covers input and output with cin and cout.
EDIT: Changed to a better link.
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;}
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
i.e.
char A = 'A';
cout << A << endl; // writes A
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
Popular Topics
Advertisement