Structure with pointers that "searches" for a preset stored data

1

Good morning. I have a problem with a code that I am doing for a project in c ++, which I need to search in a structure, in which I use pointers, a specific data. In this case it is the name of a metro or train station. I have tried several things, but none of them have happened and the answers I have seen on the internet are structures which do not use pointers, and I need to use pointers in this project.

What I have is the following:

#include <iostream>
using namespace std;
int main (){
    struct datosEstacion{
           char nombre[30];
           int km;
           int precio;

           char nombre2[30];
           int km2;
           int precio2;

           char nombre3[30];
           int km3;
           int precio3;

           char nombre4[30];
           int km4;
           int precio4;

           char nombre5[30];
           int km5;
           int precio5;

           char nombre6[30];
           int km6;
           int precio6;

           };

           struct datosEstacion *linea1;

           linea1=(struct datosEstacion*)malloc(sizeof(struct datosEstacion));
           strcpy (linea1->nombre,"Propatria");
           linea1->km=0;
           linea1->precio=0;

           strcpy (linea1->nombre2,"Agua salud");
           linea1->km2=14;
           linea1->precio2=500;

           strcpy (linea1->nombre3,"La hoyada");
           linea1->km3=21;
           linea1->precio3=1000;

           strcpy (linea1->nombre4,"Bellas Artes");
           linea1->km4=29;
           linea1->precio4=1500;

           strcpy (linea1->nombre5,"Colegio de ingenieros");
           linea1->km5=35;
           linea1->precio5=2000;

           strcpy (linea1->nombre6,"Plaza Venezuela");
           linea1->km6=44;
           linea1->precio6=2500;

           cout<<"***Estaciones del metro de la linea 1***"<<endl;
           cout<<"Nombre:"<<linea1->nombre<<endl;
           cout<<"Kms:"<<linea1->km<<endl;
           cout<<"Precio:"<<linea1->precio<<endl;
           cout<<endl;
           cout<<"Nombre:"<<linea1->nombre2<<endl;
           cout<<"Kms:"<<linea1->km2<<endl;
           cout<<"Precio:"<<linea1->precio2<<endl;
           cout<<endl;
           cout<<"Nombre:"<<linea1->nombre3<<endl;
           cout<<"Kms:"<<linea1->km3<<endl;
           cout<<"Precio:"<<linea1->precio3<<endl;
           cout<<endl;
           cout<<"Nombre:"<<linea1->nombre4<<endl;
           cout<<"Kms:"<<linea1->km4<<endl;
           cout<<"Precio:"<<linea1->precio4<<endl;
           cout<<endl;
           cout<<"Nombre:"<<linea1->nombre5<<endl;
           cout<<"Kms:"<<linea1->km5<<endl;
           cout<<"Precio:"<<linea1->precio5<<endl;
           cout<<endl;
           cout<<"Nombre:"<<linea1->nombre6<<endl;
           cout<<"Kms:"<<linea1->km6<<endl;
           cout<<"Precio:"<<linea1->precio6<<endl;

           cout<<endl;
           cout<<endl;

           string estacion1;
           string estacion2;

           cout<<"Ingrese la estacion uno (punto de partida)"<<endl;
           getline(cin,estacion1);
           cout<<"Ingrese la estacion dos (punto de llegada)"<<endl;  
           getline(cin,estacion2);  

           //aqui se supone es donde va la sentencia que busca "el nombre de la estacion, para luego "juntarlo" con sus otros datos, que son los kms y el precio

           free(linea1);

           system("pause");
           }

I had tried if (station1) == (line1) but it does not work for me or work for me. The thing is that I do not want to store any data (I already have them pre-established), but I look for it in the structure of pointers or structure with pointers. What I'm looking for is that, after the user enters the name of the station, this is linked with the kms and the price, then print the total that will pay and certain other conditions. I would appreciate your answers.

    
asked by Grecia V. P. Valero 19.02.2017 в 21:02
source

2 answers

1

P. Valero, it seems to me that you are not really using pointers, and in my opinion the way you are writing your code would be very difficult for you to achieve.

First of all, as a suggestion, I reiterate that the use of char nombre[30] is a little C style, even to make the comparisons as strcpy (linea1->nombre2,"Agua salud"); you must include the library <cstring> which is like saying strings to the C style. I suggest you use the <string> library that has a lot of spectacular functions to handle strings.

You also mix pears with apples, because you save the data of the station names as char[] and what you ask the user to save it as string , so it will not work

Having said that, I suggest you use this code:

#include <iostream>
#include <string>
using namespace std;

int main (){
    struct datosEstacion
    {
        string nombre;
        int km;
        int precio;
    };

    struct datosEstacion *linea1;

           linea1= new datosEstacion[6];// esto es mas estilo c++
           //linea1=(struct datosEstacion*)malloc(sizeof(struct datosEstacion)); // este es mas estilo C

           linea1->nombre ="Propatria"; //esta es una de las ventajas de string sobre cstring asignacion sin uso de strcpy
           linea1->km=0;
           linea1->precio=0;
// Y AQUI EMPIEZAS A USAR EL PUNTERO
// EN VEZ DE TENER QUE GENERAR UNA ESTRUCTURA DE 6 ESTACIONES
// GENERAS UNA ESTRUCTURA DE 1 ESTACION
// Y VAS "APUNTANDO" ESTACION POR ESTACION


           (linea1+1)->nombre="Agua salud";
           (linea1+1)->km=14;
           (linea1+1)->precio=500;

           (linea1+2)->nombre = "La hoyada";
           (linea1+2)->km=21;
           (linea1+2)->precio=1000;
/*
           .
           .
           .
*/

           cout<<"***Estaciones del metro de la linea 1***"<<endl;
           //Esto permitirá mejorar todo tu codigo
           int numEstaciones= 3; //esto porque solo llené tres, luego tu completas con 6
           for (int i=0 ; i < numEstaciones; i++ )
           {
               cout<<"Nombre:"<<(linea1+i)->nombre<<endl;
               cout<<"Kms:"<<(linea1+i)->km<<endl;
               cout<<"Precio:"<<(linea1+i)->precio<<endl;
               cout<<endl;
           }
           cout<<endl;
           cout<<endl;

           string estacion1;
           string estacion2;

           cout<<"Ingrese la estacion uno (punto de partida)"<<endl;
           getline(cin,estacion1);
           cout<<"Ingrese la estacion dos (punto de llegada)"<<endl;  
           getline(cin,estacion2);  

           //aqui se supone es donde va la sentencia que busca "el nombre de la estacion, para luego "juntarlo" con sus otros datos, que son los kms y el precio
            // Pues ahora si usando punteros es posible, de la otra manera era por decirlo imposible
            int estacionUno;
            int estacionDos;
            for (int i=0 ; i < numEstaciones; i++ )
            {
                if(estacion1.compare((linea1+i)->nombre)==0) estacionUno = i;
                if(estacion2.compare((linea1+i)->nombre)==0) estacionDos = i;
            }
             cout <<" estacion 1 es " << (linea1+estacionUno)->nombre << endl;
             cout <<" estacion 2 es " << (linea1+estacionDos)->nombre << endl;
           delete[] linea1;

           system("pause");
 }

in the internal comments of the code is the explanation. Greetings

    
answered by 21.02.2017 / 09:08
source
1
  

I have a problem with a code I'm doing for a project in c ++

In that case, you should abandon the idea of storing the station names in static size arrays, I advise you to create a structure that stores the station data in a std::string :

struct Estacion{
    std::string nombre;
    double km;
    double precio;
};

In addition, the kilometers and the price are better expressed in data double instead of integers, since both can contain decimals.

Once you have defined the structure Estacion you can store a collection of stations, such as a std::vector :

std::vector<Estacion> datosEstacion = {
    {"Propatria",             0,  0},
    {"Agua salud",            14, 500},
    {"La hoyada",             21, 1000},
    {"Bellas Artes",          29, 1500},
    {"Colegio de ingenieros", 35, 2000},
    {"Plaza Venezuela",       44, 2500},
};

To find the name of the station, it would be as simple as using a loop:

std::string estacion1;
std::string estacion2;

std::cout<<"Ingrese la estacion uno (punto de partida)"<<std::endl;
std::getline(std::cin,estacion1);
std::cout<<"Ingrese la estacion dos (punto de llegada)"<<std::endl;  
std::getline(std::cin,estacion2);

for (int indice = 0; indice < datosEstacion.size(); ++indice){
    if (datosEstacion[indice].nombre == estacion1){
        // hemos encontrado estacion1
    }

    if (datosEstacion[indice].nombre == estacion2){
        // hemos encontrado estacion2
    }
}

You could also use some modern C ++ with std::find_if and a lambda :

std::string estacion1;
std::string estacion2;

std::cout<<"Ingrese la estacion uno (punto de partida)"<<std::endl;
std::getline(std::cin,estacion1);
std::cout<<"Ingrese la estacion dos (punto de llegada)"<<std::endl;  
std::getline(std::cin,estacion2);

auto e1 = std::find_if(datosEstacion.begin(), datosEstacion.end(),
    [&estacion1](const Estacion &e) {
        return e.nombre == estacion1;
});

auto e2 = std::find_if(datosEstacion.begin(), datosEstacion.end(),
    [&estacion2](const Estacion &e) {
        return e.nombre == estacion2;
});

After executing the above code, e1 and e2 would be iterators pointing to the object Estacion whose name is estacion1 and estacion2 respectively.

[Here] you can see the sample code working.

    
answered by 20.02.2017 в 09:54