For simplicity I will always implicitly include the std namespace
The output can also be chained into a single statement
cout <<"This "<<" is a "<<"single C++ statement";
This is useful when you mix variables and literals together.
int age =20;int zipcode =99999;cout <<"I am "<< age <<" years old and my zipcode is "<< zipcode;
The std::endl or endl manipulator function (there are no arguments) will enter a newline and flush the output stream. The following code appears on two different lines.
cout <<"My name is Nissan"<< endl;cout <<"How are you doing?"<< endl;
Input
The keyword cin or std::cin is for input
The following will enter the input into the variable age when the ENTER key has been pressed. Notice >>
Strings are defined using std::string or string keyword and then variables' name. Don't forget to include string
Putting it all together we get
A little bit more difficult example is the following
valid input
invalid input
Namespaces
Namespaces allow us to group named entities that otherwise would have global scope into narrower scopes, giving them namespace scope. This allows organizing the elements of programs into different logical scopes referred to by names.
To create a namespace use the namespace keyword. To use the namespace created use the :: operator
output
Namespaces can also be used to access functions
output
To access a namespace without explicitly calling each the the ns::variable you may do the use the using keyword
notice using namespace ns; on line 8.
This allows us to use x and value in the namespace without using the :: operator
Note: It's actually best practice not to import the entire namespace into your program because it pollutes your namespace. This can lead to naming collisions. It's best to import only what you are using. So use std::cout instead of cout.
A better solution is to use the namespace only where you need it, for example inside the function.
string name;
cout << "What is your name? ";
cin >> name;
cout << endl << "Hello " << name << "!" << endl;
#include <iostream>
#include <string>
int main(){
std::string str;
int a;
double b;
std::cin >> str >> a >> b;
if(std::cin.fail()){
std::cerr << "input problem\n";
return 1;
}
std::cout << "I got: "<< str << ' '
<< a << ' ' << b << std::endl;
}
hi
4
6.7
output: I got: hi 4 6.7
a
b
output: input problem
#include <iostream>
using namespace std;
// Variable created inside namespace
namespace first {
int val = 500;
}
int main() {
int val = 200; // Local variable
cout << first::val << '\n'; //namespace var
return 0;
}
500
#include <iostream>
using namespace std;
namespace ns {
const double x = 100;
double value() { return 2*x; }
}
int main() {
// Access value function within ns
cout << ns::value() << endl;
// Access variable x directly
cout << ns::x << endl;
return 0;
}
200
100
using namespace std;
namespace ns{
const double x = 100;
double value() { return 2*x; }
}
using namespace ns;
int main(){
cout << value() << endl;
cout << x << endl;
return 0;
}
void foo(){
using namespace std;
using ns::context;
// some implementation
};