I need help
Ill just ask all the questions that are on my mind.
Ok, im trying to learn Strings in C++ and I understand how to set a value to a string
example:
string str = "I like big butts";
and I understand how to cout it to the screen
cout << str;
This is sickenly easy assed stuff, but Im having problems
getting the user to assign the value to it.
cin >> str; doesnt seem to work
It clearly doesnt work just treating it like a normal variable.
I need to expand the capacity that the string can hold and all my book (Practical C++) is doing is confusing me.
This should be a breeze for you guys
Its all fun and games till
someone gets pregnant.
Its all fun and games till someone gets pregnant.
Well, string isn''t a standard type (at least I don''t think it is). You should use a char array or something else. Sorry I''m not much help, but I''m tired and my parents are yelling at me, so.....
Wait!
I understand strings now.....
I feel sheepish for asking..
Its amazing what reading can accomplish
I understand strings now.....
I feel sheepish for asking..
Its amazing what reading can accomplish
Its all fun and games till someone gets pregnant.
The string class is part of the standard. I''m also pretty sure
string x;
cin >> string;
works. If i recall,correctly(its been awhile since console I/O) cin only takes one char and the rest gets cut off, you need to use cin getline() or something like that to read the whole string or else it chops it off at the first char.
ECKILLER
string x;
cin >> string;
works. If i recall,correctly(its been awhile since console I/O) cin only takes one char and the rest gets cut off, you need to use cin getline() or something like that to read the whole string or else it chops it off at the first char.
ECKILLER
ECKILLER
ECKILLER:
cin usually gets the first "word" (i.e. non whitespace group of characters) in an entry. Each word thereafter goes into a stream buffer and is extracted with repeated uses of cin. cin.getline() I believe gets all characters until a \n or \r character is encountered.
*oof*
cin usually gets the first "word" (i.e. non whitespace group of characters) in an entry. Each word thereafter goes into a stream buffer and is extracted with repeated uses of cin. cin.getline() I believe gets all characters until a \n or \r character is encountered.
*oof*
*oof*
To add to what Oofnish said, cin.getline has a third, optional argument. This argument is the delimiter.
cin.getline( szMyString, 255, ''\n'' );
This line gets all input until 255 characters have been entered, or until a newline character was entered. Note that you could change ''\n'' to '' '' or ''/'' or ''a'' or any other character you would like.
The default value for this argument is the newline character ("\n").
Josh
http://www.jh-software.com
cin.getline( szMyString, 255, ''\n'' );
This line gets all input until 255 characters have been entered, or until a newline character was entered. Note that you could change ''\n'' to '' '' or ''/'' or ''a'' or any other character you would like.
The default value for this argument is the newline character ("\n").
Josh
http://www.jh-software.com
Joshhttp://www.jh-software.com
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement