delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/01/10:00:22

From: "Jean-Réginald Louis" <louis DOT jean-reginald AT teccart DOT qc DOT ca>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Input/Output
Date: Thu, 01 Jan 1998 09:54:00 -0500
Organization: VTL
Lines: 67
Message-ID: <34ABAE07.61137681@teccart.qc.ca>
References: <01bd1616$a61f4280$7afd64c3 AT win95 DOT algonet DOT se>
NNTP-Posting-Host: ppp185.100.mmtl.videotron.net
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"?
>

First of all, because you want a string to be insert in txt mean logicaly that
txt should be a pointer (or an array with enough room). And then you use the
built in stream cin, to extract carater from the keyboard, like this.

int main(void)
{
 char txt[100];        // 100 caracters max can be write (including \r\n and
the null terminated string)

 cout << "Print a string: ";
 cin >> txt;            // whil wait the user to write something until return
as been press

 /* Do something else */
}

you can even store integer, float, double, ...
ex.

int main(void)
{
 int i;
 float f;
 double d;

 cout << "Write a integer: ";
 cin >> i;

 cout << "Write a float: ";
 cin >> f;

 cout << "Write a double: ";
 cin >> d;

 /* Now you can use  i, f and d as real variable, not string */

double total = i + f + d;
cout << "Total is: " << total;

Thing that can help you!

- Raw text -


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