1

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;
}
3
  • Just keep track of how many of your structs were already inputted and check these numbers againstMAX_P and MAX_J. Commented May 18, 2019 at 7:10
  • 1
    "I'm trying to do a function for the space in the array check and another for the verification of the size" please show those attempts. Commented May 18, 2019 at 7:10
  • 1
    Use std::vector instead of arrays. Commented May 18, 2019 at 7:31

1 Answer 1

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

check_array_is_empty is a very poorly and confusing function name, something like check_array_space_available would be clearly better. Also, increasing the array space occupied counter shouldn't be tracked in a static local variable of that function, but elsewhere (probably in a class encapsulating all that stuff).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.