0

I have this problem with this code, when I compile show me a error in the function gets, this say me that, I can change gets for getw, but when I do it again the machine show me a error. I use C++14

#include<iostream>

using namespace std;

class Maleta{
    
    private:
    
    char *color;                 //declaracion de variables ---documentando
    char *material;
    char *marca;
    int precio;
    
    public:
        
    void entrada (void);     //Declaracion de metodos
    void proceso (void);
    void salida (void);
    
    
};

void Maleta::entrada()
{
    
    cout<<"de color "<<endl;
    gets(color);
    cout<<"de material"<<endl;
    gets(material);
    cout<<"de marca"<<endl;
    gets(marca);
    cout<<"de precio"<<endl;
    cin>>precio;
    
}

void Maleta::salida(void)
{
    cout<<"color :"<<color<<endl;
    cout<<"material :"<<material<<endl;
    cout<<"marca :" <<marca<<endl;
    cout<<"precio :"<<precio<<endl;
    
}

int main()
{
    Maleta morral;
    morral.entrada();
    morral.salida();
    
    
}`

I want fix the error with the function gets.

hirof
  • 11
  • 3
  • 5
    ***How fix the error: cannot convert 'char*' to 'FILE*'?*** In c++ you probably should not be using either `char*` or `FILE*` instead you should use the `std::string` and `ifstream` – drescherjm Aug 15 '23 at 00:58
  • 1
    This program doesn't compile as-is. I needed to add `#include ` and explicitly set the compiler to use C++11 (since std::gets was deprecated in c++14). Even after fixing those problems, I can't reproduce your error. Please post a version that compiles along with which C++ version you're using. https://godbolt.org/z/abvsW7YTo – JohnFilleau Aug 15 '23 at 00:59
  • 5
    or `gets`. The [documentation page for `gets`](https://man7.org/linux/man-pages/man3/gets.3.html) has literally said "Never use `gets`" literally for decades. They literally mean it. – user4581301 Aug 15 '23 at 01:00
  • your code is far from working... `gets` was deprecated decades ago. In `C`. `char*` is just a pointer. Inyour code points to... nothing at all'. Use `C++`. – arfneto Aug 15 '23 at 01:01
  • 1
    The documentation I'm reading says to swap `gets` for `fgets`. However, you can't just do a find-replace. `fgets` uses a different signature (in order to be safe). You need to send in extra parameters. If this is for a class, talk to your teaching staff. If this is for a personal project, then do what drescherjm says. – JohnFilleau Aug 15 '23 at 01:03
  • *compile show me a error in the function gets, this say me that, I can change gets for getw* John picked that off. You need `#include ` for `gets`. Unfortunately the advice to use `getw` is bad advice in this case. Probably it assumes most people running into the no `gets` problem have simply had a finger slip down one key and typed an s in place of a w. `getw` simply reads an `int` from a file. The error message in the title is what you get when you follow this bad advice and blindly replace `gets` with `getw`. – user4581301 Aug 15 '23 at 01:14

1 Answers1

2

The gets() function was deprecated in C++ 11 and removed in C++ 14. It should not be used. It can cause buffer overflow, among other things. Do not use it. You can make your life a lot easier by using C++ strings and also using getline. Here is a fixed version of your code:

#include <iostream>
#include <string>

using namespace std;

class Maleta {
    private:
    string color;   // I changed these to C++ strings
    string material;
    string marca;
    int precio;

    public:
    void entrada(void);
    void proceso(void);
    void salida(void);
};

void Maleta::entrada() {
    cout << "de color " << endl;
    getline(cin, color); //Note that instead of gets, we are using getline for these strings
    cout << "de material" << endl;
    getline(cin, material);
    cout << "de marca" << endl;
    getline(cin, marca);
    cout << "de precio" << endl;
    cin >> precio; //We can keep your use of cin for precio since it is an int
    cin.ignore(); // Clear newline from the buffer
}

void Maleta::salida(void) {
    cout << "color :" << color << endl;
    cout << "material :" << material << endl;
    cout << "marca :" << marca << endl;
    cout << "precio :" << precio << endl;
}

int main() {
    Maleta morral;
    morral.entrada();
    morral.salida();
}
Ben A.
  • 874
  • 7
  • 23
  • After it was deprecated, `gets` was removed from the language. – Keith Thompson Aug 15 '23 at 02:02
  • Thanks for the clarification. I just edited my answer. It was removed in 14, but it is still in 11. I still use C++ 11 because... so to me I think of it as deprecated. – Ben A. Aug 15 '23 at 02:09