🔒 Locked
String ?
Advertisement
Original Post
I am writing a text based game which is helping me to learn c++ by actually tying to make a program. Well I need help. I want to be able to let the player input his name and the game use it throughout the game. I have tried cin>>var but it doesn''t do it right. Also cin>>var doesn''t seem to want to accept different input a keep it different.
Could you be a little more descriptive?
What do you mean it doesn''t work?
What is it doing instead?
What do you mean that it doesn''t accept other kinds?
What type is ''var''?
Wait, wait, wait....who''s Nambla Fett?
What do you mean it doesn''t work?
What is it doing instead?
What do you mean that it doesn''t accept other kinds?
What type is ''var''?
Wait, wait, wait....who''s Nambla Fett?
First, you can''t just use
char j;
This will only hold one character. Honestly I don''t have time to give you a c/c++ string tutorial. Search the boards or the web for how to use strings in c/c++.
Second, you are trying to put two different values into the same variable!!
If you wanted the final outcome to be "jack (last name)", then look up ''concatenation''.
Wait, wait, wait....who''s Nambla Fett?
char j;
This will only hold one character. Honestly I don''t have time to give you a c/c++ string tutorial. Search the boards or the web for how to use strings in c/c++.
Second, you are trying to put two different values into the same variable!!
If you wanted the final outcome to be "jack (last name)", then look up ''concatenation''.
Wait, wait, wait....who''s Nambla Fett?
There's no datatype for a string, only a single character, so if you accept input on a character variable, for example:
char x;
cin >> x;
And the user types in an entire string, it will only capture the first character of that string.
What you need to do is create an array of characters, like this:
char buffer[50];
And then accept input on that, like this:
cin >> buffer;
This will assign the first character to buffer[0], the second character to buffer[1], and so on. The only problem is that if the user types in more than one word, well...the program will interpret the first space character to be the end of the string. In other words, if the user types in:
John Doe
The program will simply interpret it is "John." Also, if the user types in more characters than elements in your array (in this case, 50), the program will write past the end of the array in memory and you'll get a fencepost error.
What you need to do is use another method for accepting input from the user, called cin.get(). cin.get() accepts three parameters: name of the array to fill, maximum amount of characters to accepts, and lastly, the terminating character. The third argument is defaulted to the newline character, which is fine, so I usually leave that blank. The maximum amount of characters to fill, the second parameter, must be one less than the amount of characters in your array, because you must account for the terminating character. Here's what the code would look like for your problem:
char name[50];
cout << "Please enter your name: ";
cin.get(name, 49);
cout << "\n\nHello, " << name << ".";
Edited by - utwo007 on November 28, 2001 1:24:00 PM
char x;
cin >> x;
And the user types in an entire string, it will only capture the first character of that string.
What you need to do is create an array of characters, like this:
char buffer[50];
And then accept input on that, like this:
cin >> buffer;
This will assign the first character to buffer[0], the second character to buffer[1], and so on. The only problem is that if the user types in more than one word, well...the program will interpret the first space character to be the end of the string. In other words, if the user types in:
John Doe
The program will simply interpret it is "John." Also, if the user types in more characters than elements in your array (in this case, 50), the program will write past the end of the array in memory and you'll get a fencepost error.
What you need to do is use another method for accepting input from the user, called cin.get(). cin.get() accepts three parameters: name of the array to fill, maximum amount of characters to accepts, and lastly, the terminating character. The third argument is defaulted to the newline character, which is fine, so I usually leave that blank. The maximum amount of characters to fill, the second parameter, must be one less than the amount of characters in your array, because you must account for the terminating character. Here's what the code would look like for your problem:
char name[50];
cout << "Please enter your name: ";
cin.get(name, 49);
cout << "\n\nHello, " << name << ".";
Edited by - utwo007 on November 28, 2001 1:24:00 PM
What he said.
Wait, wait, wait....who''s Nambla Fett?
Wait, wait, wait....who''s Nambla Fett?
quote:
Original post by utwo007
There''s no datatype for a string, only a single character...
In C. Since you''re using C++, why not using std::string (a specialization of type std::basic_string<char>) and live happily ever after?
Start Here!
quote:
Original post by Gameman17
void main()
Ewww...
Psalm 137:9: "Happy shall he be, that taketh and dasheth thy little ones against the stones."
In Borland C++ just do:
#include < string >
and declare a variable as type string and then do input/output as normal.
Edited by - FieroAddict on November 28, 2001 10:27:38 PM
#include < string >
and declare a variable as type string and then do input/output as normal.
Edited by - FieroAddict on November 28, 2001 10:27:38 PM
quote:
In Borland C++ just do:
#include < string >
and declare a variable as type string and then do input/output as normal.
Remember to add this line after all STL includes:
using namespace std;
or, if u only want the string class:
using std::string;
EDIT - On second thoughts, I use MSVC++, so it might not be the same for Borland. Sorry if that's the case, Fiero.
---------------
I finally got it all together...
...and then forgot where I put it.
Edited by - AdmiralBinary on November 28, 2001 10:40:45 PM
quote:
Original post by Anonymous Poster
Whatsamatta with void main()?
If you don''t need a return or arguments, does it really matter?
See http://www.research.att.com/~bs/bs_faq2.html#void-main or read the standard, article 3.6.1.2:
quote:
An implementation shall not predefine the main function. This function shall not be overloaded. It shall
have a return type of type int, but otherwise its type is implementationdefined.
...
Psalm 137:9: "Happy shall he be, that taketh and dasheth thy little ones against the stones."
if your using Borland, then by all means don't use the STL string implementation. Use the AnsiString class instead. It is the closest thing to standard. All declarations necessary to use it come stock in all generated borland empty projects.
ex.
AnsiString lastName;
cin >> lastName;
If you want to do modifications/concatenations to it, if you type 'AnsiString.'(w/o the ') then you will get a list of available methods that pop up(called code completion). Whenever you can, use the AnsiString. On some compilers though, 'String' and 'AnsiString' are the same, but I haven't used one of those compilers yet.
EDIT: I just wanted to comment on the 'void main()' thing. As the previous guy said, the standard states that main shall not be voided and must return an int(etc). There are three reasons why this is important(from my knowledge, maybe I'm wrong):
1)It promotes lazy/sloppy programming
2)Eventually, compilers(standardized) may not compile old projects with their main voided. In fact, some don't already.
3)It saves alot of time in simpler programs to exit with a 'real' exit code(ie return 1 or 0[etc]) in main() than forceful unload when an error occurs.
-Greg
Hopeful Purveyor of the Open Source Community
Edited by - visionare on November 29, 2001 5:27:58 AM
ex.
AnsiString lastName;
cin >> lastName;
If you want to do modifications/concatenations to it, if you type 'AnsiString.'(w/o the ') then you will get a list of available methods that pop up(called code completion). Whenever you can, use the AnsiString. On some compilers though, 'String' and 'AnsiString' are the same, but I haven't used one of those compilers yet.
EDIT: I just wanted to comment on the 'void main()' thing. As the previous guy said, the standard states that main shall not be voided and must return an int(etc). There are three reasons why this is important(from my knowledge, maybe I'm wrong):
1)It promotes lazy/sloppy programming
2)Eventually, compilers(standardized) may not compile old projects with their main voided. In fact, some don't already.
3)It saves alot of time in simpler programs to exit with a 'real' exit code(ie return 1 or 0[etc]) in main() than forceful unload when an error occurs.
-Greg
Hopeful Purveyor of the Open Source Community
Edited by - visionare on November 29, 2001 5:27:58 AM
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement