how do i write a name as a variable?
like (Program)Whats your name?
(User)John
(Program)John, what you want do do?
plz help me ^^ thank you guys :p
Printable View
how do i write a name as a variable?
like (Program)Whats your name?
(User)John
(Program)John, what you want do do?
plz help me ^^ thank you guys :p
You probably want the name variable to be of type string. Then it's just a matter of I/O using cin/cout.
Example if you get baffled. (Like I did. ;))
Code:...
char* name;
cout << "What's you'r name?" << endl;
cin >> name;
cout << name << " what do you want to do? << endl;
...
o man i love u thanks ^^
damn I tried it, but it is not working :confused: why? maybe I have to put another lybrary besides <iostream>?
Why the change from string to char*? That's a very dangerous move and going in the wrong direction. Stick to the string type.Quote:
Originally Posted by Cool-August
They are header files, not libraries and the one for the string container class happens to be <string>. You may need to put a using namespace std; statement after all your #include statements to get this working or use std:: all over the place. You may have also forgotten a doublequote if you copied the code posted by Cool-August as is.Quote:
Originally Posted by Scarvenger
Code:#include <string>
#include <iostream>
int main()
{
std::string name;
std::cout << "What's your name?" << std::endl;
std::cin >> name;
std::cout << name << ", what do you want to do?" << std::endl;
return 0;
}
char* name;
cout << "What's you'r name?" << endl;
cin >> name;
Did you ever hear of memory allocation?
How about learning about nice safe std::strings for strings rather than massively unsafe C-style char arrays or the outright dangerous uninitialised char* pointers.
also, if you're going to use std::cin or std::cin.get() for input, don't forget about the possibility of leaving garbage in the input stream. for example, if the user enters "john doe" and your next question is about their eye color, you're screwed.
a simple std::cin.ignore(32000,'\n'); right after cin or cin.get() should do the trick.