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
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
You probably want the name variable to be of type string. Then it's just a matter of I/O using cin/cout.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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; ...
Last edited by Queatrix; 10-19-2005 at 10:16 AM. Reason: changed "string name;" to "char* name;".
o man i love u thanks ^^
damn I tried it, but it is not working 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.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.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; }
Last edited by hk_mp5kpdw; 10-19-2005 at 12:23 PM.
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
Join is in our Unofficial Cprog IRC channel
Server: irc.phoenixradio.org
Channel: #Tech
Team Cprog Folding@Home: Team #43476
Download it Here
Detailed Stats Here
More Detailed Stats
52 Members so far, are YOU a member?
Current team score: 1223226 (ranked 374 of 45152)
The CBoard team is doing better than 99.16% of the other teams
Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)
Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT