1

This is a simple program and my question is to know why is it compulsory to use using namespace std ? why program is not complied without using this ?

#include <iostream>

using namespace std;

int main(){

    int a , b , c , d;

    cout << "Enter First Value" <<endl;
    cin >> a;

    cout << "Enter Second Value" <<endl;
    cin >> b;

    cout << "Enter 1 to add values" << endl  << "Enter 2 to subtract values" <<endl  <<"Enter 3 to multiply values" <<endl ;
    cin >> c;

    if (c == 1){    
        d = a + b;
        cout << "After Adding your answer is " << d << endl;
    }

    else if (c == 2){
        d = a - b;
        cout << "After Subtracting your answer is " << d << endl;
    }

    else if (c == 3){
        d = a * b;
        cout << "After Multiplication your answer is " << d << endl;
    }
    else{
        cout << "You Enter Invalid operation to perform";   
    }

    system("pause");  //buitin function to stop the comand screen.

    return 0;
} 
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
Haroon
  • 9
  • 1
  • 3
  • 6
    It's actually the opposite of compulsory. It's mainly used in C++ books for the sake of brevity. But in the real world, pulling the whole `namespace std` can cause serious headaches. – 101010 Nov 02 '16 at 13:06
  • *why is it compulsory to use using namespace std* It isnt. – Borgleader Nov 02 '16 at 13:08
  • Bad practice to use it: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Saurav Sahu Nov 02 '16 at 13:08
  • PLEASE READ THIS: http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – NathanOliver Nov 02 '16 at 13:08
  • 3
    *why program is not complied without using this ?* Did you try to remove it and look what is your compiler problem without it? – Aracthor Nov 02 '16 at 13:16

4 Answers4

11

why is it compulsory to use using namespace std ?

It isn't.

In fact, I would recommend against it.

However, if you do not write using namespace std, then you need to fully qualify the names you use from the standard library. That means std::string instead of string, std::cout instead of cout, and so forth.

If your book does not tell you this already, then you need a better book.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 7
    Note that you can put `using namespace std;` inside of a function, which may be more acceptable in some circumstances. – asu Nov 02 '16 at 13:35
8

You should not (but can) use

using namespace std;

You can use std:: before each object from that namespace instead, e.g.:

std::cout << "Enter First Value" << std::endl;
std::cin >> a;
VolAnd
  • 6,367
  • 3
  • 25
  • 43
1

Generally spoken, using namespace is not recommended since it uncontrolled imports everything from the name space into yours.

This may be okay, for very small tools or in a local scope but should not be done under no circumstances in header files you provide as an interface.

One convenient thing that might be ok, is to import some often used identifier into your (cpp file global - not header) namespace via.

using std::cout;
using std::cin;
using std::endl;

Also note "Lightness Races in Orbit"'s link

vlad_tepesch
  • 6,681
  • 1
  • 38
  • 80
0

Without namespaces, you should care if your custom name conflicts with the included ones. The using namespace part of your code is a syntactical sweetener: you do not have to put the std:: scopes for all occurences. Your code could be more readable, but set of your scopeless names are narrowed. Consider following example:

#include <cstdio>
#include <iostream>

int main()
{
    char endl[] = "xy";
    std::cout << "[" << std::endl << "]";
    std::cout << "[" << endl << "]";
    return 0;
}

If you want to use the name endl for custom purposes, you cannot have the using namespace directive, otherwise it would become ambiguous.

renonsz
  • 571
  • 1
  • 4
  • 17