-1

C++ INTRO14.CPP example from Turbo C++

#include <iostream.h>

int main ()
{

    int your_number;
    cout << "Enter a whole number: ";
    cin >> your_number;

    if (your_number % 2 == 0)
       cout << "\nYour number is even\n";

    if (your_number % 2 != )
    {
       cout << "Your number is odd.\n";

    }

    cout << "That's all!\n";
}

I only get the message "Enter a whole number". When I enter an odd or even number the program seems to terminate.

I get the message "your number is even" (or odd) only after I return to DOS shell.

Why do I only get partial output when I hit RUN from the IDE?

bolov
  • 72,283
  • 15
  • 145
  • 224
James
  • 1
  • 2
  • 4
    I had no idea people still use Turbo C++. The last time I used that software was about 23 years ago. What is stopping you from using a modern compiler and IDE? – paddy Feb 22 '21 at 03:29
  • 2
    This is a classic problem with Turbo-C++. It does not wait for output when program terminates. You can then see the output on output window (no need to return to shell). Also, a general workaround is to add something like `getch()` or `system("pause")` at the end of `main`. Does this answer your question? [No output when running program](https://stackoverflow.com/questions/44024039/no-output-when-running-program) – brc-dd Feb 22 '21 at 03:46
  • Why Turbo C++? There's *many* other *much* better compilers available for free. – tadman Feb 22 '21 at 03:58
  • `#include ` is not standard c++ and won't compile with any compiler from the last 2 decades. This is just one example why you shouldn't use borland C++. – bolov Feb 22 '21 at 04:34
  • 1
    use `getch()` at the end, it will wait till any button is pressed. You will see the message. – Abhishek Dutt Feb 22 '21 at 04:51
  • Turbo C++ is more than a decade older than the first C++ standard (C++98) so you won't be able to learn any useful C++ knowledge, besides it's also harder to find documents for it – phuclv Feb 22 '21 at 06:05
  • IIRC `[ALT+F5]` or just `[F5]` switch between source code and the result of your app once its finished ... `getch()` adding to end will help but on DOSBOX you might want to add at least 3 of them in sequence as it glitches – Spektre Feb 22 '21 at 09:23

1 Answers1

1

As mentioned in the comment, use getch() at the end of the program.

Now why you have to use it?
Short Answer
Because the output is shown to the console in a matter of milliseconds. The output is shown, but it's too fast to be seen by a user. And Turbo C++ has not to control over it. So you have to use the getch() statement.
As mentioned in comment, sometimes a single getch() doesn't work. So I am using it three times.

It should look like this:-

#include <iostream.h>

int main ()
{

int your_number;
cout << "Enter a whole number: ";
cin >> your_number;

if (your_number % 2 == 0)
   cout << "\nYour number is even\n";

if (your_number % 2 != )
{
   cout << "Your number is odd.\n";

}
 
cout << "That's all!\n"; 
getch();
getch();
getch();
}

There is another alternative:-

getchar()
Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24