|
-
Jan 10th, 2001, 08:14 PM
#1
Thread Starter
Member
I want to know how to use Cin and Cout Correctly.
-
Jan 10th, 2001, 08:29 PM
#2
Frenzied Member
Code:
#include <iostream>
using namespace std;
int main()
{ cout << "Some output text" //send a string to the console
<< endl; // send a newline character
int a;
cout << "Enter an integer: "; //prompt user for input
cin >> a; //get user input and put it in variable a
cout << endl << endl //go down a couple of lines
<< "You entered the integer " //output a string
<< a //output an integer
<< endl; //another newline
return 0; //end function
}
That's basically how it works. Nothing fancy.
<< is the stream insertion operator
>> is the stream extraction operator
cin and cout are streams. cin is the input stream, and cout is the output stream. You insert things into the output stream and extract them from the input stream. Makes sense, really
Harry.
"From one thing, know ten thousand things."
-
Jan 11th, 2001, 09:34 AM
#3
Frenzied Member
You may encounter problems using cin with multi-word input. In that case you use cin.getline
Code:
char text[256];
cout<<"Enter Sentence"<<endl;
cin.getline(text,256);
cout<<text<<endl;
-
Jan 14th, 2001, 09:46 PM
#4
Hyperactive Member
you could also use
printf to print text out(like cout)
scanf - to take input from the keyboard.
AS far as i see they are the same as cout and cin but i found these easier to use
Matt 
-
Jan 15th, 2001, 06:08 AM
#5
Frenzied Member
'cout' and 'cin' are easier to use than 'printf' and 'scanf'. 'printf' and 'scanf' have more complex sintax, if you want to display a value have to use %d or %s or..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|