Why Doesn't a Simple "Hello World"-Style Program Compile With Turbo C++

Why doesn't a simple Hello World-style program compile with Turbo C++?

There's no problem with this program. (Except probably some stylistic issues —
using namespace std is not recommended). The problem is with Turbo C++. It is a very old piece of software. It implements a dialect of C++, so-called pre-ANSI C++, that has completely fallen out of use by the beginning of this millennium. The first ANSI standard for C++ was published in 1998, then there was the 2003 version, the 2011 version, the 2014 version, the 2017 version, and now we expect the 2020 version to be officially published. Each of these standard revisions brought more or less significant changes to the language.

For Turbo C++ you have to modify the program like this:

#include <iostream.h>      // note the .h suffix
// using namespace std; // Turbo C++ doesn't implement namespaces

int main()
{
cout << "Hello, World!";
return 0;
}

If you look at this program, the difference between the modern C++ dialect and the one accepted by Turbo C++ may seem small. However it will grow much larger as your programs will be getting more complex.

While you can learn programming using Turbo C++ I would strongly recommend to avoid that if humanly possible because of the following problems:

  1. You will be learning a language that is somewhat similar to a popular language used in the industry, but is very different nevertheless, for no good reason. If you plan to write C++ for real software development, you will have to re-learn much. It is much easier to learn modern C++ right away.
  2. There's no extant literature about Turbo C++. Nearly 100% of C++ material you will find on the internet or in the books is not directly applicable to Turbo C++ out of the box. Some will need only minor adaptation, while other material is completely unusable. Pretty much the only source of help immediately available to you is the built-in Turbo C++ help.
  3. Few people remember Turbo C++. When asking questions on forums, always specify that you are using a pre-ANSI dialect in order to filter out responses geared towards the modern version of the language. You will probably get a bunch of comments suggesting you to stop immediately and switch to a modern compiler with every question you ask.

There are many modern free (as in beer, as well as in speech) compilers and IDEs you can use in place of Turbo C++. Some of these include:

  1. Visual C++ Community Edition is an IDE and a compiler from Microsoft
  2. Code::Blocks is a lightweight IDE. On Windows it ships with a somewhat outdated compiler, but you can install a more modern compiler yourself
  3. Eclipse CDT is a powerful cross-platform IDE. It doesn't ship with its own compiler so you need to install a separate compiler. On Windows, use e.g. MinGW.
  4. Many more
  5. In addition, there are many online compilers such as http://ideone.com, https://www.onlinegdb.com/ and http://coliru.stacked-crooked.com/, plus many more (these are mostly good for trying out ideas and writing very small programs).
  6. Both Clang/LLVM and GCC are free software compilers supporting recent versions of C++.

Regrettably, some schools/teachers appear to force students to use Turbo C++ even in this day and age. Unfortunately this is not something this community can fix. If you find yourself in this situation, prepare to not being able to get much outside help.

c++ program not giving desired output

The problem is that the data member s has not been initialized and you're using that uninitialized data member which leads to undefined behavior.

Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior.

So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.

So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.

Solution

Since you're using Turbo, you can solve the problem by adding a parameterized constructor that initializes both the data members n and s to 0 as shown below:

//other code here as before
class summer
{
unsigned int n,s;//unsigned int used instead of int
public:
int get();
void calc();
void show();
void define();
//parameterized constructor
summer(): n(0), s(0) //uses constructor initializer list
{

}
};

//other code here as before

The output of the modified program can be seen here.

Some of the changes i made include:

  1. Added a parameterized constructor to initialize data members n and s to 0 using constructor initializer list.
  2. Made the data members n and s to be of type unsigned int.

1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.

A simple C code compile and run successfully in Borland Turbo C++ compiler but show errors in other compiler

Probably you are trying to compile it using C compiler.
Try with C++ compiler like g++ it works!

Why do we use iostream.h in Turbo C++?

why do we use <iostream.h> in turbo c++ IDE

Because turbo c++ IDE uses the turbo c++ compiler, which was written before c++ was standardised. In that ancient dialect of the language, the STL header is named differently from the standard library header that we have in the standard.

That said, very few people use turbo c++ in the real world.

and add always use using namespace std;

No. We never use using namespace std;. And you should neither.

But specifically in turbo C++ dialect, the language didn't have namespaces yet.

How can I get and use the header file graphics.h in my C++ program?

<graphics.h> is very old library. It's better to use something that is new

Here are some 2D libraries (platform independent) for C/C++

SDL

GTK+

Qt

Also there is a free very powerful 3D open source graphics library for C++

OGRE



Related Topics



Leave a reply



Submit