1

I am a beginner in C++. The C++ that I code is usually on Borland since that's what the school tells us to code on.There's this code which I've attached below. It works fine in Borland but when I try using it in Visual Studio, it gives errors stating that gets() is undefined and the same problem with clrscr().I've recently installed VS so i don't know much about it. Do I install certain header files separately? I've attached only one function of the program here.

    void setdata()
    {
        cout << "--Enter car details--" << endl;
        cout << "Enter car name: " << endl;
        gets(name);
        cout << "Enter car type: " << endl;
        gets(type);
        cout << "Enter year of manufacture: " << endl;
        cin >> year;
        cout << "Enter kilometres driven: " << endl;
        cin >> kms;
        cout << "Enter rate: " << endl;
        cin >> rate;
    }
Omar
  • 19
  • 1
  • 4
  • 1
    `gets` is deprecated and removed for security reasons in quite a few implementations I'd suggest using `cin` instead. – Mgetz Jul 15 '19 at 17:58
  • [`cin.getline`](https://en.cppreference.com/w/cpp/io/basic_istream/getline) is probably your best replacement for `gets` unless you're allowed to use `std::string`, and in that case use [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline). If you've included `` and still can't find `clrscr`, you'll have to [write your own](https://stackoverflow.com/a/6487534/4581301). – user4581301 Jul 15 '19 at 19:10

2 Answers2

0

In recent version of C programming language there is no function clrscr(). In old Dos version of C clrscr() is defined in conio.h headerfile. The clrscr() function generally clears the screen (i.e removes previous input outputs). Now, this is done automatically in the recent version of C also in visual studio.

so if you remove the clrscr() it will works fine.

  • What about the C++ language? The OP has the C++ language tag. Also, C and C++ are different languages (for example, C++ has `std::string` and `std::cin`). – Thomas Matthews Jul 15 '19 at 19:38
  • C++ use cin and cout objects to input and output. It is far more advance than C. Hence we don't need to worry about clearing screen etc. It does automatically. – Sachin Maharjan Jul 16 '19 at 03:06
-1

You have to add #include<cstdio> for gets() function.

clrscr() is the function from Borland compilers. Use system("cls") from the header "stdlib.h" instead. Moreover add using namespace std at the beginning of your file if necessary.

user4581301
  • 33,082
  • 7
  • 33
  • 54
Piotr Siekański
  • 1,665
  • 8
  • 14
  • [Do not use `gets`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). [Avoid using `system`](https://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and-c). [Avoid `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – user4581301 Jul 15 '19 at 18:38