Advertisement

Getting String Input

Started by January 19, 2000 09:29 AM
4 comments, last by Ray745 24 years, 11 months ago
in a text-only program in C++, how would i get a whole string of input. i know how to get a single character or integer char a; cin >> a; but what would i do if i wanted to get a whole word or set of numbers without doing like char a, b, c, d, e, f; cin >> a >> b >> c >> d >> e >> f; please help me, any info would be helpful thank you If I only had a nickle for every problem I''ve had, I wouldn''t have anymore problems, but then I guess I wouldn''t have anymore nickles either...
If I only had a nickle for every problem I''ve had, I wouldn''t have anymore problems, but then I guess I wouldn''t have anymore nickles either...
I don''t have a C++ reference around right now. But I know there are several functions that can be used to read lines of input.

Try getline(char*, int, char) from the iostream.h library.
the char * is the string you want to read into, int defines the string length, and the last char is an optional delimiter char.
Advertisement
Have you tried getchar().
I can''t remember the specific syntax but you should be able to find it in any C or C++ reference book.

Best regards,
Sherman
_________________ Best regards, Sherman Chin Director Sherman3D (Malaysia) Sdn Bhd www.Sherman3D.com www.AlphaKimori.com
I believe the C library functions gets and scanf will both read a whole line of input.
This would be possible, too:
char s[SIZE];char *Buffer;cin >> Buffer;sprintf (s, Buffer); 


(The chars that are > SIZE will be erased)
You can either use cin.read or cin.getline. Here is an example with cin.getline:

void main(){    char buffer[ 80 ];        cout << "Enter a sentence:  ";    cin.getline( buffer, 80 );} 


Edited by - Mike on 1/19/00 11:48:08 AM

This topic is closed to new replies.

Advertisement