Mail Archives: djgpp/1998/01/02/06:15:32
Erik wrote:
>
> Hello!
>
> I have a question about Input and Output in C++. I was trying to make a
> program that asks for a string.
>
> This is how far I got:
>
> #include <iostream.h>
>
> int main()
> {
> char txt;
>
> cout << "Print a string: " << endl;
>
> So far I´ve got variable of the type CHAR called "txt". In txt should that
> you write be stored.
>
> Then the program will ask me to write something.
> Here is my problem. How should I store that write in the variable "txt"?
>
> Please someone help me!!
Simple answer:
cin >> txt;
Longer answer: This will under most circumstances not get you
what you want. txt is a char, 1 letter. So you probably want something
like
char txt[256];
cout << "Say something: "<<flush; // Note flush instead of endl, so no
LF
cin >> txt;
This will skip all white space and get you all characters up to and
not including trailing white space. If you want the whole line, use
cin.getline(txt,256); // Might be (256,txt)!
Even longer answer:
say
info iostream
on the command line, read it.
--
Ciao
Tom
*************************************************************
* Thomas Demmer *
* Lehrstuhl fuer Stroemungsmechanik *
* Ruhr-Uni-Bochum *
* Universitaetsstr. 150 *
* D-44780 Bochum *
* Tel: +49 234 700 6434 *
* Fax: +49 234 709 4162 *
* http://www.lstm.ruhr-uni-bochum.de/~demmer *
*************************************************************
- Raw text -