c++ cout variable code example

Example 1: how to print a string to console in c++

// Just some basic format 

#include <iostream>
#include <string>
using namespace std;

int main()
{
 cout << "Print a String" << endl; 
}

Example 2: how to grab all of user input c++

// cin with strings
#include <iostream>
#include <string>
using namespace std;

int main ()
{
  string mystr;
  cout << "What's your name? ";
  getline (cin, mystr);
  cout << "Hello " << mystr << ".\n";
  cout << "What is your favorite team? ";
  getline (cin, mystr);
  cout << "I like " << mystr << " too!\n";
  return 0;
}

Example 3: cout value c++

#include <iostream>

using namespace std;

int main()
{
	int a,b;
	char str[] = "Hello Programmers";
	
	/* Single insertion operator */
	cout << "Enter 2 numbers - ";
	cin >> a >> b;
	cout << str;
	cout << endl;
	
	/* Multiple insertion operator */
	cout << "Value of a is " << a << endl << "Value of b is " << b;
	
	return 0;
}

Example 4: how to cout in c++

std::cout << "Hello World!" << std::endl; //Or you can do
std::cout << "Hello World!" <<; //Only in some scenarios

Tags:

Cpp Example