Assign a float in main() to a struct inside another struct

Asked

Viewed 20 times

-1

I’m doing an exercise and I can’t understand what I’m missing in this final part. Basically I need to do a struct to register an employee, and within that struct use another struct with the value of salaries (gross and net) of 3 months, add the total and make the media. In the end I need to print the amount of the net salary of each month (in addition to the gross total, net total and average).

The problem is that I’m not sure how do I get the main() to put a float value on each of the 3 positions of this struct. I’m trying to do this:

typedef struct salario{
        float sal, bonus, salBruto, salLiq;
        float tSal, tBonus, tBruto, tLiq;    
    } Salario;
    
typedef struct cadastro{
      char nome[15];
      char rua [15];
      int numero;
      char bairro[15];
      struct salario sala[2];
      float MediaTrimestre;
    } Cadastro;

And further down the code:

for (i=0; i<3; i++){
if (salario.salBruto >1000){
        salario.tLiq = salario.tLiq + (salario.salBruto * 0.8);
        // salario.salLiq = salario.salBruto *0.8; // tentei utilizar isso pra depois usar salario.salLiq mas ainda não resolveu.
        cadastro.sala[i] = salario.salBruto*0.8; // <=== PROBLEMA AQUI!!!!!!! 
      }
      else{
        salario.tLiq = salario.tLiq + (salario.salBruto * 0.95);
      }
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef struct salario{
    float sal, bonus, salBruto, salLiq;
    float tSal, tBonus, tBruto, tLiq;    
} Salario;

typedef struct cadastro{
  char nome[15];
  char rua [15];
  int numero;
  char bairro[15];
  struct salario sala[2];
  float MediaTrimestre;
} Cadastro;

int main() {
  Cadastro cadastro;
  Salario salario;
  salario.tSal = 0, salario.tBonus = 0, salario.tBruto = 0, salario.tLiq=0;
  int i;      

  // CADASTRO: NOME, RUA, NUMERO, BAIRRO
  printf("Nome: ");
  scanf(" %[^\n]", cadastro.nome);
       

  printf("Rua: ");
  scanf(" %[^\n]", cadastro.rua);
        

  printf("Número: ");
  scanf(" %d", &cadastro.numero);

  printf("Bairro: ");
  scanf(" %[^\n]", cadastro.bairro);
      
  //

  // CADASTRO SALÁRIO 3 MESES
  for (i = 0; i < 3; i++){
    printf("\nSalário %dº Mês: ", i+1);
    scanf(" %f", &salario.sal);
    salario.tSal = salario.tSal + salario.sal;
   
        //printf("\n%f\n", salario.tSal); // print para checar o sal se ta add correto
    printf("Bônus %dº Mês: ", i+1);
    scanf(" %f", &salario.bonus);
    salario.tBonus = salario.tBonus + salario.bonus;
        //printf("\n%f\n", salario.bonus); // print para checar o bonus se ta add correto
    
    salario.salBruto = salario.sal + salario.bonus; // bruto de cada mês para calcular o liquido mensal
    salario.tBruto = salario.tSal + salario.tBonus; //total que recebeu nos 3 meses
    
      if (salario.salBruto >1000){
        salario.tLiq = salario.tLiq + (salario.salBruto * 0.8);
        cadastro.sala[i] = 0; // <=== PROBLEMA AQUI!!!!!!!
      }
      else{
        salario.tLiq = salario.tLiq + (salario.salBruto * 0.95);
      }
    }
  //
  /* resto do código */
}

1 answer

0

Looking at the problem, it looks like you are trying to set a float for each element of the.room array, with each element being a struct (not a float). The call of each cadastro.sala[i] is a struct, not a float, so you can’t throw a number right there.

I think you want to define a field of each of the structs, not the whole one. Let’s say this field is salBruto (I’m guessing based on the logic of your code, but it could be tBruto as well). Then the problematic section would be replaced by:

cadastro.sala[i].salBruto = salario.tLiq + (salario.salBruto * 0.95);

And in the second part:

cadastro.sala[i].salBruto = 0;

Change the field salBruto by another that it finds necessary in its logic and is defined in the salary struct. But in summary, only a float field should receive a float value.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.