delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/02/06:15:32

From: Thomas Demmer <demmer AT LSTM DOT Ruhr-UNI-Bochum DOT De>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Input/Output
Date: Thu, 01 Jan 1998 15:35:46 +0100
Organization: Lehrstuhl fuer Stroemungsmechanik
Lines: 74
Message-ID: <34ABA9C2.A626FD14@LSTM.Ruhr-UNI-Bochum.De>
References: <01bd1616$a61f4280$7afd64c3 AT win95 DOT algonet DOT se>
NNTP-Posting-Host: bvb.lstm.ruhr-uni-bochum.de
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019