Mail Archives: djgpp/1998/01/01/10:00:22
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 -