Dan Cezanne Galavan
Dan Cezanne Galavan

Reputation: 19

Problem with functions and structs on c++

My code has two structs, Players[200 MAX] and Characters[30 MAX]. To register a player I have to check if there is space in the array and if the user name is > than 5 characters. After that I can ask for the other data. All has to be with functions, so I'm trying to do a function for the space in the array check and another for the verification of the size.

To be honest I don't know how to handle this, I lost some classes about functions with structs and lost all my mind, was looking on the Internet but there's not a lot of information about this.

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

const int MAX_P = 200;
const int MAX_J = 30;

struct Personatge
{
int Numero;
string Nom;
char Tier;
float Pes;
};

struct Jugadors
{
string Username;
string Nom;
bool Amateur;
int PJfav;
};

int menu();
void altaJugador(Jugadors* jugs);

void main() {

Personatge vectorPersonajes[MAX_P];
Jugadors vectorJugadores[MAX_J];

int opcion = menu();

switch (opcion)
{
case 1: 
    altaJugador(vectorJugadores);
    break;
case 2:
    break;
case 3:

    break;
case 4: 
    break;
default:
    break;
}

system("pause");
}

int menu() {

int eleccion;

cout << "SSBU CEP Tournament \n";
cout <<
    "1. Alta jugador \n" <<
    "2. Establir personatge favorit \n" <<
    "3. Mostrar jugadors \n" <<
    "4. Baixa jugador \n" <<
    "5. Alta personatge (manual) \n" <<
    "6. Llistat de personatges d'un tier \n" <<
    "0. Sortir \n";
cout << "Opcio: \n";

do
{
    cin >> eleccion;
    if (eleccion < 0 || eleccion > 6)
    {
        cout << "Error, tria un numero del 0 al 6 \n";
    }
} while (eleccion < 0 || eleccion > 6);

return eleccion;
}
void altaJugador(Jugadors *jugs) {

cout << "Introduce el username del jugador: \n";
getline(cin, (jugs->Username));

cout << "Introduce el nombre y el apellido: \n";
getline(cin, (jugs->Nom));

cout << "Eres amateur? S/N: \n";
cin >> jugs->Amateur;

cout << "Introduce el numero de tu personaje favorito: \n";
cin >> jugs->PJfav;
}

Upvotes: 1

Views: 70

Answers (1)

Jv 9211
Jv 9211

Reputation: 21

You can try like this you create a function which is check for space in array and as well as length of string. You have to use a variable which pointing to a current position of array index and increase by 1 until it reach to the 199 that mean's your array is full. for string input you create a temp string variable check it's length with if statement

bool check_array_is_empty()
{
 static int counter = 0;
 if (counter < 200)
 {
     counter ++;
     return true;
 }
 else
 {
   return false;
 }

for length of string function

bool check_length(string temp)
{
  int i = 0;
  while(a[i]!='\0')
{
    i++;
}
if (i<5)
{
  return false;
}
else
{
  return true;
}

Upvotes: 1

Related Questions