Hi guys,
i have one question, when i type
using namespace std;
at top of my program my compiler gives some error
especially syntax error!
my compiler is Borland C++ v.5
for example simple code:

#include <stdio.h>
#include <conio.h>
#include <iostream.h>

using namespace std;
void main()
{
clrscr();
getch();
}

Regards

Dani AI

Generated

@Ancient Dragon is on the right track: you are mixing two worlds. The pre‑standard headers (like <iostream.h>) put everything in the global namespace, while the standardized headers (like <iostream>) put library names in std. So either use the classic headers and no std, or use the standard headers and qualify with std:: (or a local using), but do not mix them. Bjarne Stroustrup summarizes this change: standard library facilities live in namespace std and come from headers without the .h suffix. See his note here for background: Stroustrup, C++ Style and Technique FAQ.

About cin.ignore() line: ignore discards characters from the input buffer; it is often used to skip a leftover newline before you ask the user to press Enter. Older, pre‑standard libraries sometimes required explicit arguments to ignore, which might explain your error. A portable "pause" that works with standard headers looks like this:

#include <iostream>
#include <limits>

int main() {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // flush any pending newline
    std::cout << "Press Enter to exit..." << std::flush;
    std::cin.get(); // wait for Enter
}

ignore’s signature (and defaults) are documented here: cppreference: istream::ignore.

Two quick portability notes that will save you time later:

  • The C++ entry point must return int (not void). Reference: Stroustrup, "Can I write void main()?".
  • <conio.h> functions such as clrscr() and getch() are vendor‑specific and not part of ISO C/C++. If you rely on them, your code is tied to those compilers. Background: conio.h overview.

Recommended Answers

All 4 Replies

Shouldn't use void main. getch() isn't a great way of stopping the program. iostream.h is non-standard. use iostream:

#include <iostream>

using namespace std;

int main( void ) {

  std::cin.ignore();
  return 0;
}

Hi guys,
i have one question, when i type
using namespace std;
at top of my program my compiler gives some error
especially syntax error!
my compiler is Borland C++ v.5
for example simple code:

When using the old headers with .h extension you don't use "using namespace std". Check to see if your compiler supports the new headers without an extension and use those instead. The old files are out-of-date and will cause lots of other problems if you attempt to use current coding practices.

Shouldn't use void main. getch() isn't a great way of stopping the program. iostream.h is non-standard. use iostream:

#include <iostream>

using namespace std;

int main( void ) {

  std::cin.ignore();
  return 0;
}

Thanks, it works, but i dont understand part of your code!
std::cin.ignore()
why you use that?!
my compiler gives error on that line?!, is it for pausing the screen?!
Regards

When using the old headers with .h extension you don't use "using namespace std". Check to see if your compiler supports the new headers without an extension and use those instead. The old files are out-of-date and will cause lots of other problems if you attempt to use current coding practices.

fzafarani here is some more info on the subject {i found it on the internet!}:

The use of namespace std for all identifiers of the C++ standard library was introduced during
the standardization process. This change is not backward compatible to old header files, in which identifiers of the C++ standard library are declared in the global scope.
In addition, some interfaces of classes changed during the standardization process (however, the goal was to stay backward compatible if possible). So, a new style for the names of standard header files was introduced. This allows vendors to stay backward compatible by providing the old header files.

i got the text from here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.