Code not working

I want to know that why these codes are not executing using DEV C++.
Code .1:

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n=10;
char ch='*';
cout<<"Testing output... ";
cout<<n;
cout<<ch;
getch();
}
Code .2:

#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int n=10;
char ch='*';
cout<<"Testing output... ";
cout<<n;
cout<<ch;
getch();
}

Firstly always use code tags when you post - select the code then press the <> button on the right.


Looking at this and your other post, where you have Turbo C++ 3.0:

TC3 is really really old (like 20 years old) and so it doesn't have many of the features of modern C++.

Modern C++ has a thing callled std namespace. Namespaces didn't exist when TC3 was written.

So to get the code to work on a modern compiler, you need to do one 3 things:

1. Put std:: before each std namespace thing - there are hundreds of std things, but to start with you could have std::cout, std::cin, std::endl this is the most recommended way.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream> // no .h - that is deprecated
#include <string>

int main () {    //main always return an int - always forever

std::string MyString = "Greetings";

std::cout << MyString << "\n"; //   \n newline character escape 
std::cout << "Hello World" << std::endl;  // endl same as newline but flushes the buffer

return 0;   //if everything is OK - something else otherwise
}


2. After the include statements, put using statements - like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream> // no .h - that is deprecated
#include <string>

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

int main () {    //main always return an int - always forever

std::string MyString = "Greetings";

cout << MyString << "\n"; //   \n newline character escape 

cout << "Hello World" << endl;

return 0;   //if everything is OK - something else otherwise
}


This is a mixed approach - make use of the using statement for common things, do std:: for not so common things.

3. Put a using namespace std after the includes. Not recommended because it drags untold stuff into the global namespace and can cause naming conflicts for functions or variables.

The conio.h is not a portable header.

After all that I have heard that Dev C is not that great as a compiler. Get something better like Code::Blocks or MinGW. I use the KDevelop or QtCreator IDE's (Integfrated Development Environment) on Linux, Which has a whole heap of advantages over Windows IMO.

Hope all goes well.
Last edited on
Function clrscr() is non-standard and may not exist in all compilers.
Similarly, getch() or _getch() is found in the <conio.h> header, which is not standardised and may not be available with all compilers.
Topic archived. No new replies allowed.