Failed to return 0

1

The program consists only of entering a name, and if it is in the file, display a menu. Otherwise the menu is not displayed and the program is terminated by the function fin () that returns in 0.

However, even if the name is not found in the file, the menu is displayed.

#include <iostream>
#include <string>
#include<fstream>

using namespace std;

bool localizacionJugador(char apodo);
void menu();
int fin();


int main()
{
    ofstream ficheroSalida;
    ficheroSalida.open("db2.txt");
    ficheroSalida << "a";
    ficheroSalida.close();

    char apodo;
    cout << "Introduzca su nombre:";
    cin >> apodo;
    localizacionJugador(apodo);
    menu();
}


bool localizacionJugador(char apodo)
{
    char c;
    bool encontrado=false;
    fstream ficheroEntrada;
    ficheroEntrada.open("db2.txt");
    if(ficheroEntrada.is_open())
    {
        while(!ficheroEntrada.eof() && c!=apodo)
        {
            ficheroEntrada.get(c);
        }
        encontrado=c==apodo;
        if(encontrado==true)
        {
            cout << "Usuario encontrado"<<endl<<endl;
        }else
        {
            cout << "Usuario no encontrado";
            fin();
        }
    }else
    {
        cout << "Fichero no encontrado";
        fin();
    }
    ficheroEntrada.close();
    return encontrado;
}

void menu()
{
    cout << "Este es el menu";
}

int fin()
{
    return 0;
}
    
asked by Vendetta 14.11.2017 в 18:14
source

1 answer

1

Your code:

localizacionJugador( apodo );
menu( );

Where do you check if you have found it or not? As it is, the menu is always . You need to check the value returned by the function :

if( localizacionJugador( apodo ) ) {
  menu( );
}

Apart from that, those things you do, pass apodo as a char ... the char contain individual characters , no strings . I suggest you review the relevant part of the code.

    
answered by 14.11.2017 / 19:44
source