I want to know how to use Cin and Cout Correctly.
Printable View
I want to know how to use Cin and Cout Correctly.
That's basically how it works. Nothing fancy.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
}
<< 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 :)
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;
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
'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..