Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: GominaTilted en 9 Enero 2019, 19:24 pm



Título: Me ayudais con esta funcion?
Publicado por: GominaTilted en 9 Enero 2019, 19:24 pm
Buenas, se supone que esta funcion tiene que mostrar los libros de una struct entre dos fechas introducidas por el usuario:

Código
  1. void MostrarLibrosFechas (Libros libros)
  2.    {
  3.        Fecha fm, fM;
  4.        Libro lib;
  5.        int i = 1;
  6.  
  7.        cout << "Escribe la fecha menor: " << endl << endl;
  8.  
  9.        fm = PedirFecha ();
  10.  
  11.        cout << "Escribe la fecha mayor: " << endl << endl;
  12.  
  13.        fM = PedirFecha ();
  14.  
  15.        for (int j = 0; j <= libros.num; j++)
  16.        {
  17.            if (fm.anyo >= libros.vect[j].fecha.anyo && fM.anyo <= libros.vect[j].fecha.anyo)
  18.            {
  19.            cout << "Libro " << i << " : " << endl << endl;
  20.                lib = libros.vect[j];
  21.                MostrarLibro (lib);
  22.  
  23.                i++;
  24.            }
  25.  
  26.            else if (fm.anyo == libros.vect[j].fecha.anyo)
  27.            {
  28.                if (fm.mes >= libros.vect[j].fecha.mes)
  29.                {
  30.                    cout << "Libro " << i << " : " << endl << endl;
  31.                    lib = libros.vect[j];
  32.                    MostrarLibro (lib);
  33.  
  34.                    i++;
  35.                }
  36.  
  37.                else if (fm.mes == libros.vect[j].fecha.mes)
  38.                {
  39.                    if (fm.dia >= libros.vect[j].fecha.dia)
  40.                    {
  41.                    cout << "Libro " << i << " : " << endl << endl;
  42.                        lib = libros.vect[j];
  43.                        MostrarLibro (lib);
  44.  
  45.                        i++;
  46.                    }
  47.                }
  48.            }
  49.  
  50.            else if (fM.anyo == libros.vect[j].fecha.anyo)
  51.            {
  52.                if (fM.mes <= libros.vect[j].fecha.mes)
  53.                {
  54.                        cout << "Libro " << i << " : " << endl << endl;
  55.                        lib = libros.vect[j];
  56.                        MostrarLibro (lib);
  57.  
  58.                        i++;
  59.                }
  60.  
  61.                else if (fM.mes == libros.vect[j].fecha.mes)
  62.                {
  63.                    if (fM.dia <= libros.vect[j].fecha.dia)
  64.                    {
  65.                    cout << "Libro " << i << " : " << endl << endl;
  66.                        lib = libros.vect[j];
  67.                        MostrarLibro (lib);
  68.  
  69.                        i++;
  70.                    }
  71.                }
  72.            }
  73.     }
  74.  
  75.     return;
  76. }
  77.  

Por si alguien lo quiere ver, los struct son:

Código
  1. struct Fecha
  2. {
  3.    int dia,mes,anyo;
  4.    };
  5.  
  6. struct Libro
  7. {
  8. Fecha fecha;
  9.    string titulo, autor;
  10.    float precio;
  11.    };
  12.  
  13.    const string NOM_FICH="PracticaFinal_opcion1.libros.dat";
  14.    const int MAX=2000;
  15.    typedef Libro VecLibros[MAX];
  16.  
  17.    struct Libros
  18. {
  19.    int num;
  20.    VecLibros vect;
  21.    };
  22.  

La funcion PedirFecha()

Código:
Fecha PedirFecha ()
    {
        Fecha fecha;
        
        cout << " Dia: ";
        cin >> fecha.dia;
        cout << endl;
        cout << " Mes: ";
        cin >> fecha.mes;
        cout << endl;
        cout << " Anyo: ";
        cin >> fecha.anyo;
        cout << endl;
        
        return fecha;
    }

Y por ultimo la funcion MostrarLibro:

Código:
void MostrarLibro (Libro libro)
{
     cout << "  Titulo del libro:";
        cout << libro.titulo;
        cout << endl;
        cout << "  Nombre del autor:";
        cout << libro.autor;
        cout << endl;
        cout << "  Precio de adquisicion:";
        cout << libro.precio;
        cout << endl;
        cout << "    Dia de compra:";
        cout << libro.fecha.dia;
        cout << endl;
        cout << "    Mes de compra:";
        cout << libro.fecha.mes;
        cout << endl;
        cout << "    Anyo de compra:";
        cout << libro.fecha.anyo << endl << endl;
        
        return;
    }


Título: Re: Me ayudais con esta funcion?
Publicado por: CalgaryCorpus en 9 Enero 2019, 21:03 pm
El primer if parece tener las condiciones al reves


Título: Re: Me ayudais con esta funcion?
Publicado por: GominaTilted en 9 Enero 2019, 22:32 pm
El primer of parece tener las condiciones al reves
Efectivamente xD. Además tenía que añadir varias condiciones más, un poco raro.