0

I am currently using 'Digital Mars C++' as I needed Turbo C++ like Syntax and 32-bit exe build so that's the reason.

Now my problem that I am facing is that when I write the code below,

#include <iostream.h>
#include <stdio.h>
int main()
{
    char n[30];
    cout << "Enter Name: ";
    gets(n);
    return 0;
}

Then I save this file then I save this file as test.cpp.

Then I opened my command prompt and typed dmc test.cpp.

Now as I got a compiled file the exe file test.exe, now when I run it it's first asking for input then it displays a message Enter Name:.

Please help me with this problem it is really necessary.

NOTE :

  • when I use printf instead of cout it works fine.

  • and when I replace my statement cout << " Enter Name: "; with cout << " Enter Name: " << endl then also i get my message and then asks input.

Please do not suggest me of using the GCC compiler.

And I would be glad if you direct me where to make changes in my stdio.h file to get rectified gets function to work properly or any such other header file which is related to fixing this error.

Chen Li
  • 4,824
  • 3
  • 28
  • 55
Nikhil.Nixel
  • 555
  • 2
  • 11
  • 25
  • 3
    Have you tried `cout << "Enter Name: " << flush;`? – hlt May 19 '18 at 12:04
  • 8
    Don't use `gets`. Never ***ever*** use `gets`. It's an old and **dangerous** function and has therefore been either obsoleted and now even *removed* from both the C and C++ specifications. – Some programmer dude May 19 '18 at 12:05
  • What does flush do. – Nikhil.Nixel May 19 '18 at 12:05
  • 1
    why do you *need* Turbo C++-like Syntax? 32-bit build is irrelevant here as there are tons of 32-bit compilers for each platform – phuclv May 19 '18 at 12:05
  • 2
    And *please* get a more up to date compiler. You're learning the ancient pre-standard C++. – Some programmer dude May 19 '18 at 12:06
  • [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/995714) @hlt there's no STL in Turbo C++ so no cout either – phuclv May 19 '18 at 12:06
  • @Someprogrammerdude I get that but still if I am trying to make a Turbo C like editor then I need to use get function I know it's dangerous to use it but as I am trying to make an editor just like Turbo C4 so I need the syntax that my teachers use in our school to run efficiently in my compiler some trying to fix the gets function problem please tell me where can I make the changes in the header file to get it right – Nikhil.Nixel May 19 '18 at 12:07
  • Well he is using `cout` (and says it works with `endl`) so I'm not entirely sure about that @LưuVĩnhPhúc – hlt May 19 '18 at 12:07
  • @Nikhil Standard output is buffered, and usually only sent to the console at the end of a line (?), `flush` manually tells it to send everything in the buffer out. – hlt May 19 '18 at 12:08
  • @LưuVĩnhPhúc I know but is that stupid education system in our in my country that enforces the 11th grade students to use Turbo C plus plus compiler colours. – Nikhil.Nixel May 19 '18 at 12:08
  • 2
    Being forced to use Turbo C... my condolences – hlt May 19 '18 at 12:09
  • @hlt believe me bro I have tried it in Turbo C4 the same code that I'm presenting to you now and it works. – Nikhil.Nixel May 19 '18 at 12:10
  • @LưuVĩnhPhúc please elaborate what is STL I am new to it. And bro believe me Turbo C4 supports use of COUT – Nikhil.Nixel May 19 '18 at 12:12
  • 1
    Use fgets or cin or getline instead of gets – stark May 19 '18 at 15:16

1 Answers1

1

You can Add this code,

 cout << flush;

just before using cout << "Some Statement"; or In direct approach you can do this cout << " Some Statement" << flush; then use gets() with ease.

Nikhil.Nixel
  • 555
  • 2
  • 11
  • 25