Advertisement

Characters

Started by February 14, 2000 03:41 PM
4 comments, last by Fredric 24 years, 7 months ago
In one of my programs, I need the user to type in a string of characters, which is to be stored in a variable like so: char userName; This compiles fine, but when I compiled and ran my program, used it up to the point where I need to type in a string of characters, the program only shows the first character I type when I later use the variable userName. Can anyone show me how to make my program show every single character that the user types? I''m sure it''s very simple... I just don''t know how to do so! Thanks
3D Math- The type of mathematics that'll put hair on your chest!
Not to be rude, but this is the kind of information easily found in a beginners book on C/C++. However, I''ll try and help anyway.

You''re declaring a single character, which happens to be one byte in length. All string operations require an ARRAY of characters, of a certain length.

Try using
char userName[50];

that declares an array of characters 50 bytes long. When you use a string input function such as scanf() all the input the user typed is entered into the array (called a buffer) and a "\0" or NULL character appended to the end. Note that when you declare a variable as an array, the variable name by itself is actually only a pointer to the buffer... as if you had declared it as char* userName;
You must be careful that you don''t try to access the above array past the 49th character (since arrays in C/C++ are 0 based) or you will be running wild through random spots in your system memory.

In fact, i''m surprised that your program wasn''t crashing already.

hope that helps a bit.


*oof*
*oof*
Advertisement
I''m not sure what language you''re programming in, but in most languages a "char" variable is limited to 1 character. What you should do is declare userName as a string variable.
Talon87talon87@looksmart.comECHoA:www.angelfire.com/nc2/echoaVisit the ECHoA website!
Just do what Oofnish says, he''s right. However, you can make it a 64 character array:

UserName[64];


Good luck!
D:
Sorry, I messed up, it should be:

char UserName[64];
D:
Thanks for help... I''m kicking myself because that was so easy, but I didn''t know how to do it!
I''m reading a C++ book, but we haven''t gotten to the chapter dedicated to Arrays, so don''t laugh too hard. I''m learning references right now, and after that, advanced functions. Anyways... I just wanted to thankyou.
3D Math- The type of mathematics that'll put hair on your chest!

This topic is closed to new replies.

Advertisement