2

I have 4 structs:

typedef struct personaje{
    cadena_personaje nombre_personaje;
    char sexo;
    int vidas, danio, magia, nivel;
}personaje;

typedef struct vector_personajes{
    personaje personajes[5];
    int contador;
}vector_personajes;

typedef struct fecha{ 
    int dia, mes, anio;
}fecha;

typedef struct videojuego{
    vector_personajes vp;
    fecha fecha_juego;
    cadena_juego titulo_juego;
    char genero;
    int id, niveles;
    char multijugador;    
}videojuego;

personajes contains the main features of the video game characters which are: the name cadena_personaje nombre_personaje, the sex char sexo and 4 int data types that represent the health, damage, magic and max level of it int vidas, danio, magia, nivel

vector_personajes contains an array size 5 of personajes personaje personajes[5] and a counterint contador that keeps track of the number of characters that video game has (max 5).

fecha contains 3 integers which represent a date, day, month and year. int dia, mes, anio.

videojuego contains the main characteristics of the video game, which are: the name of the video game cadena_juego titulo_juego, the release date fecha fecha_juego, the characters it has vector_personajes vp, the genre char genre which can be, action, rol, simulation..etc., the number of levels it has int niveles, the product id of the game (from a store perspective) int id and the possibility of playing multiplayer char multijugador which can be si/no (yes or no)

I have a void introducir(&v) method which, given a video game, reads all the elements needed to describe the actual video game.

void introducir(videojuego &v){
    int fecha, cont;
    bool correcto=false;
    //juego
    cout<<"Introduce el titulo del videojuego: ";
    cin.get(v.titulo_juego,60);
    cout<<"Introduce el id: ";
    cin>>v.id;
    cout<<"Introduce el genero ";
    while (!correcto){
        cin>>v.genero;
        switch (v.genero)
        {
        case 'a':
            correcto=true;
            break;
        case 'r':
            correcto=true;
            break;
        case 'e':
            correcto=true;
            break;
        case 's':
            correcto=true;
            break;
        case 'd':
            correcto=true;
            break;
        default:
            cout<<"El genero es incorrecto, por favor vuelva a introducrilo: ";
            break;
        }
    }
    correcto=false;
    cout<<"Introduce el numero de personajes: ";
    cin>>v.vp.contador;
    cout<<"Hola";//intento ver donde está el fallo
    for (int i = 1; i <= v.vp.contador; i++){
        cout<<"Introduce el nombre del personaje "<<i<<" :";
        cin.get(v.vp.personajes[i].nombre_personaje,40);
        cout<<"Introduce las vidas del personaje "<<i<<" :";
        cin>>v.vp.personajes[i].vidas;
        cout<<"Introduce el danio maximo que puede hacer el personaje "<<i<<" :";
        cin>>v.vp.personajes[i].danio;
        cout<<"Introduce el nivel maximo al que puede llegar el personaje "<<i<<" :";
        cin>>v.vp.personajes[i].nivel;
        cout<<"Introduce sexo del personaje "<<i<<" :";
        correcto=false;
        /*while (!correcto){
            cin>>v.vp.personajes[i].sexo;
            if (v.vp.personajes[i].sexo=='H'||v.vp.personajes[i].sexo=='h'||v.vp.personajes[i].sexo=='m'||v.vp.personajes[i].sexo=='M'){
                correcto=true;
            }
            else{
                cout<<"hola: ";
            }
        }*/
        correcto=false;
        cout<<"Introduce la magia del personaje "<<i<<" :";
        cin>>v.vp.personajes[i].magia;
    }
}

The problem is that when I execute the introducir(&v) method, it gets to the for ok, but once inside it, the only instructions that the program reads are the couts.

I just don't know what to do. I would really appreciate if anyone could copy and paste my code, compile and run it, and try to figure out what's happening.

Here's the full code:

#include <iostream>

using namespace std;

typedef char cadena_juego[60];
typedef char cadena_personaje[41];

typedef struct personaje{
    cadena_personaje nombre_personaje;
    char sexo;
    int vidas, danio, magia, nivel;
}personaje;

typedef struct vector_personajes{
    personaje personajes[5];
    int contador;
}vector_personajes;

typedef struct fecha{ 
    int dia, mes, anio;
}fecha;

typedef struct videojuego{
    vector_personajes vp;
    fecha fecha_juego;
    cadena_juego titulo_juego;
    char genero;
    int id, niveles;
    char multijugador;    
}videojuego;

void introducir(videojuego &v){
    int fecha, cont;
    bool correcto=false;
    //juego
    cout<<"Introduce el titulo del videojuego: ";
    cin.get(v.titulo_juego,60);
    cout<<"Introduce el id: ";
    cin>>v.id;
    cout<<"Introduce el genero ";
    while (!correcto){
        cin>>v.genero;
        switch (v.genero)
        {
        case 'a':
            correcto=true;
            break;
        case 'r':
            correcto=true;
            break;
        case 'e':
            correcto=true;
            break;
        case 's':
            correcto=true;
            break;
        case 'd':
            correcto=true;
            break;
        default:
            cout<<"El genero es incorrecto, por favor vuelva a introducrilo: ";
            break;
        }
    }
    correcto=false;
    cout<<"Introduce el numero de personajes: ";
    cin>>v.vp.contador;
    cout<<"Hola";//intento ver donde está el fallo
    for (int i = 1; i <= v.vp.contador; i++){
        cout<<"Introduce el nombre del personaje "<<i<<" :";
        cin.get(v.vp.personajes[i].nombre_personaje,40);
        cout<<"Introduce las vidas del personaje "<<i<<" :";
        cin>>v.vp.personajes[i].vidas;
        cout<<"Introduce el danio maximo que puede hacer el personaje "<<i<<" :";
        cin>>v.vp.personajes[i].danio;
        cout<<"Introduce el nivel maximo al que puede llegar el personaje "<<i<<" :";
        cin>>v.vp.personajes[i].nivel;
        cout<<"Introduce sexo del personaje "<<i<<" :";
        correcto=false;
        /*while (!correcto){
            cin>>v.vp.personajes[i].sexo;
            if (v.vp.personajes[i].sexo=='H'||v.vp.personajes[i].sexo=='h'||v.vp.personajes[i].sexo=='m'||v.vp.personajes[i].sexo=='M'){
                correcto=true;
            }
            else{
                cout<<"hola: ";
            }
        }*/
        correcto=false;
        cout<<"Introduce la magia del personaje "<<i<<" :";
        cin>>v.vp.personajes[i].magia;
    }
}

int main(){
    videojuego v;
    introducir(v);
    return 0;
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    This is likely a variation of [this issue](https://stackoverflow.com/questions/21567291/), only with `cin.get()` instead of `getline()`. Why are you using `char[]` arrays for strings, instead of using `std::string`? You should use `std::getline()` to read a line of text, it will discard any trailing `'\n'` for you. If you insist on using `char[]`, then consider `cin.getline()`. Otherwise, you have to discard any trailing `'\n'` manually with `cin.ignore()`. – Remy Lebeau Dec 03 '21 at 19:31

2 Answers2

0

cin.get() only reads one char, which in this case is the \n left behind by the previous operator>>. Try using cin.ignore() before the cin.get(), like this:

for (int i = 1; i <= v.vp.contador; i++){
        cout<<"Introduce el nombre del personaje "<<i<<" :";
        cin.ignore();
        cin.get(v.vp.personajes[i].nombre_personaje,40);
        cout<<"Introduce las vidas del personaje "<<i<<" :";
        cin>>v.vp.personajes[i].vidas;
        cout<<"Introduce el danio maximo que puede hacer el personaje "<<i<<" :";
        cin>>v.vp.personajes[i].danio;
        cout<<"Introduce el nivel maximo al que puede llegar el personaje "<<i<<" :";
        cin>>v.vp.personajes[i].nivel;
        cout<<"Introduce sexo del personaje "<<i<<" :";
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    Calling `cin.ignore();` right before `cin.get()` is a code smell. You should be calling `ignore()` after the read operation that actually left the unwanted char(s) behind, not call it right before the next read operation. – Remy Lebeau Dec 03 '21 at 19:27
  • just tried and doesn't work. I used `cin.get()` because I want to add spaces in the character name for example: ezio auditore because `cin` just reads till the first blank space – PABLO SETRAKIAN Dec 03 '21 at 19:29
  • You tried to put cin.ignore() as I said at the first example before cin.get()? Because I tried it and compiled it, and it worked for me. – Bilal Aabkari Dec 03 '21 at 19:32
0

My programming teacher answered my e-mail with the solution. What's happening is that the cin.get()is not working right. The input buffer keeps data and takes it into other fields. What I gotta do is reading cin() (not being able to read plan spaces anymore but ‍♀️) so I don't have any problem with the buffer.

"The input buffer keeps data and takes it into other fields" is my own English translation to "el buffer de entrada se queda con datos y los toma en los otros campos" if anyone doesn't understand what im saying given that I don't really know what a buffer and what it does, so I can't really express myself with that. Ima keep investigating about that "buffer".

Thank you all for your answers!! Appreciate it, really!

Final code is the following:

#include <iostream>

using namespace std;

#define maxjue 60
#define maxcad 41
#define maxper 5

typedef char cadena_juego[maxjue];
typedef char cadena_personaje[maxcad];

typedef struct
{
    cadena_personaje nombre_personaje;
    char sexo;
    int vidas, danio, magia, nivel;
}personaje;

typedef personaje v_per [maxper];

typedef struct
{
    v_per personajes;
    int contador;
}vector_personajes;


typedef struct
{
    int dia, mes, anio;
}fecha;

typedef struct
{
    vector_personajes vp;
    fecha fecha_juego;
    cadena_juego titulo_juego;
    char genero;
    int id, niveles;
    char multijugador;
}videojuego;

void introducir(videojuego &v);

int main()
{
    videojuego v;
    introducir(v);
    return 0;
}

void introducir(videojuego &v)
{
    int fecha, cont;
    char aux;

    //juego
    cout<<"Introduce el titulo del videojuego: ";
    cin>>v.titulo_juego;
    cout<<"Introduce el id: ";
    cin>>v.id;
    cout<<"Introduce el genero ";
    cin>>v.genero;
    while (v.genero!='a' && v.genero!='r' && v.genero!='e' && v.genero!='s' && v.genero!='d' )
    {
        cout<<"Genero incorrecto, vuelva a introducir:";
        cin>>v.genero;
    }
    cout<<"Introduce el numero de personajes: ";
    cin>>v.vp.contador;
    cout<<"Hola";//intento ver donde está el fallo
    //en realidad no hay tal fallo, el cin.get no esta funcionando bien
    //el buffer de entrada se queda con datos y los toma en los otros campos
    //lee con cin>> las cadenas para no tener problemas de buffer
    for (int i = 0; i < v.vp.contador; i++)
    {
        cout<<"Introduce el nombre del personaje "<<i+1<<" :";
        cin>>v.vp.personajes[i].nombre_personaje;
        cout<<"Introduce las vidas del personaje "<<i+1<<" :";
        cin>>v.vp.personajes[i].vidas;
        cout<<"Introduce el danio maximo que puede hacer el personaje "<<i+1<<" :";
        cin>>v.vp.personajes[i].danio;
        cout<<"Introduce el nivel maximo al que puede llegar el personaje "<<i+1<<" :";
        cin>>v.vp.personajes[i].nivel;
        cout<<"Introduce sexo del personaje "<<i+1<<" :";
        cin>>v.vp.personajes[i].sexo;
        while (v.vp.personajes[i].sexo!='H'&& v.vp.personajes[i].sexo !='h'&& v.vp.personajes[i].sexo !='m'&& v.vp.personajes[i].sexo !='M')
        {
            cout<<"Error, Introduce de nuevo sexo del personaje "<<i+1<<" :";
            cin>>v.vp.personajes[i].sexo;
        }
        cout<<"Introduce la magia del personaje "<<i+1<<" :";
        cin>>v.vp.personajes[i].magia;
    }
}