Struct with string in C

Asked

Viewed 446 times

-1

I have a tremendous question, I did this algorithm here on C, I need to register patients, name, age, sex, if he’s in the risk group, if he’s with covid 19, then I need to count how many are male and how many are female, how many are in the risk group, how many are with covid.

In this case I’m trying to make this comparison with STRLEN there in "srtlen(citz[x].sex == 'F') but I can’t, as I could make this comparison and count how many have of each sex?

/*  // strcpy; strcat, strncat
	Name: João 
	Copyright: 
	Author: 
	Date: 17/04/20 20:11
	Description: 
*/
//
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <locale.h>
#include <string.h>

int function (void);
int main (void){
	setlocale(LC_ALL, "PORTUGUESE");
	//Declarações
	int idade, sexo, grupoderisco, infectado,qtdm,qtdf, strut;
	//
	printf("\n          Prefeitura de Salvador");
	printf("\n      Primeira capital do brasil\n");
	printf("\n     Secretaria Municipal de Saúde");
	printf("\n   Numeros totais do Corona Virus em Salvador");
	
	strut = function();
	
return 0;
}

int function(void){
int x,z;
int tamanho;
	printf("\n\n\n\n   Registro de dados *(INDIVIDUAIS) \n  ");
	printf("\n\n\n\n   Quantas pessoas infectadas temos na cidade? \n  "); scanf("%d", &z);
	fflush(stdin);

struct Covid
{
	int idade,citz[z];
	char nome[50],sexo[50],grupo[10],infectado[10];
};
	//create
	for (x=0;x<z;x++){
	struct Covid citz[x];
	printf("Instruções gerais: Para o programa funcionar, deve-se escrever o nome completo, sexo com 'M' ou 'F', grupo de risco e se está infectado com 'S' ou 'N'' \n");
	printf("Nome completo = \n");
	fgets(citz[x].nome, 50, stdin);
	printf("Sexo = \n");
	fgets(citz[x].sexo, 50, stdin);
	printf("Encontra-se no grupo de risco = \n");
	fgets(citz[x].grupo, 10, stdin);
	printf("Está infectado pelo (COVID-19)? = \n");
	fgets(citz[x].infectado, 10, stdin);
	printf("Qual sua idade? = \n");
	scanf("%d", &citz[x].idade);
	fflush(stdin);
	
	tamanho = strlen(citz[x].sexo == "F");
	printf("Sexo = ", tamanho);



}// final do looping da struct citz[x]
	
	//system("cls");
	//Área de parametros de controle
	printf("[Estatisticas]\n\n\n\n");
	printf("Um total de pessoas infectadas [%d]", z);

		
		
		
		
		
		return 0;	
	}
	
	

  • Note that you declare your variable z and uses it without having done any attribution to it. In addition you use citzas an integral part of the Covid struct and also as the name of a Covid struct array.

  • I declared it there in the function Function and then initialized there in the scanf right below, that’s what you meant?

  • The scanf was cropped on my screen. Now I saw, it’s OK. Check the use of the citz variable name.

  • I now switched, the struct Covid citz[x] to struct Covid citz[z]; and the for that was up put underneath the struct

1 answer

1

strlen() is for measuring the length of the string. To compare, you should use some function of the strcmp family, for example

strcmp(sexo, "F") == 0

strcmp() returns -1, 0, or +1 as the string is smaller, equal to or larger (lexicographically speaking) than the model. In this case, to test equality, test if it returns zero.

Another option, if you are sure that the string always has only 1 character, is to compare only the character:

*sexo == 'F'

Browser other questions tagged

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