Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

SummerGirl

C - Por que meu programa está dando erro quando eu executo?

Recommended Posts

Rotinas:

1. Introduz o número de registros.

2. Ler e gravar os dados de cada pessoa.

3. Calcule o IMC de cada pessoa.

4. Escolha uma das opções.

5. Classificar de acordo com a opção escolhida.

6. Exibe na tela em forma de tabular os dados ordenados.

 

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include <string.h>
#include<math.h>

/* STRUCTURE DECLARATION */
typedef struct  {
char name[100];
char ---;
int age;
float weight;
float height;
float bmi;
char situation[100];

}person;

void Menu();
void PerformOperation(int option);
void Exchange(person* sort);

/* MAIN */
int main()
{
        person *profile;
        FILE *arch;
        int i,qnt;
        arch=fopen("data.txt","w"); //OPENS AND WRITES TO THE FILE
        if(arch==NULL)
        return (-1);
        printf("\nEnter the maximum record:");   
        scanf("%d",&qnt);
        for (i=0;i<qnt;i++){
               printf("\nName: ");
               fflush(stdin);
               gets(profile[i].name);
               printf("---: ");
               fflush(stdin);
               scanf("%c",&profile[i].---);
               printf("Age: ");
               fflush(stdin);
               scanf("%d",&profile[i].age);
               printf("Weight: ");
               fflush(stdin);
               scanf("%f",&profile[i].weight);
               printf("Height: ");
               fflush(stdin);
               scanf("%f",&profile[i].height);
               fprintf(arch,"%20s",profile[i].name);
               fprintf(arch,"%c",profile[i].---);
               fprintf(arch,"%2d",profile[i].age);
               fprintf(arch,"%3.2f",profile[i].weight);
               fprintf(arch,"%3.2f",profile[i].height);
        printf("\nRecord Ok\n");
}
/* CALCULATION OF BMI AND CLASSIFICATION*/
        for(i=0;i<qnt;i++){
               profile[i].bmi=(profile[i].weight/(profile[i].height*profile[i].height));
              
               if((profile[i].bmi>0.0) & (profile[i].bmi<18.5))
               strcpy(profile[i].situation,"Underweight ideal");
               if((profile[i].bmi>=18.5) & (profile[i].bmi<25.0))
               strcpy(profile[i].situation,"Normal weight");
               if((profile[i].bmi>=25.0) & (profile[i].bmi<29.9))
               strcpy(profile[i].situation,"Overweight");
               if((profile[i].bmi>=30.0) & (profile[i].bmi<34.9))
               strcpy(profile[i].situation,"Obesity grade 1");
               if((profile[i].bmi>=35.0) & (profile[i].bmi<39.9))
               strcpy(profile[i].situation,"Obesity grade 2");
               if(profile[i].bmi>=40.0)
               strcpy(profile[i].situation,"Obesity grade 3");
}
/* CHOOSE OPTIONS */
        int option;

        do
        {
               printf("\n\n");        
               Menu();
               printf("\nEnter an option: ");
               scanf("%d",&option);
               PerformOperation(option);
        }while(option != 5);

        printf("________________________________________________________________");
        printf("\n\nname\tsex\tage\tweight\theight\tbmi\situation");
        printf("\n______________________________________________________________");
        for(i=0;i<qnt;i++)
        printf("\n%s",profile[i].name);
        printf("\n%c",profile[i].---);
        printf("\n%d",profile[i].age);
        printf("\n%f",profile[i].weight);
        printf("\n%f",profile[i].height);
        printf("\n%f",profile[i].bmi);
        printf("\n%s",profile[i].situation);
        printf("\n______________________________________________________________");
       
        system("pause");
        fclose(arch);
        return(0);
}
void Menu()
{
        printf("1 - Alphabetically");
        printf("\n2 - weight (increasing)");
        printf("\n3 - Height (descending)");
        printf("\n4 - BMI (increasing)");
        printf("\n5 - Exit");
}
/* EXECUTE OPTION SELECTED */
void PerformOperation(int option)
{
        person sort[50];
        person *profile;
        int i;
        switch(option)
        {
               case 1:
                        if (profile[i].name>profile[i+1].name){
                        Exchange(sort);
               }break;
               case 2:
                        if (profile[i].weight>profile[i+1].weight){
                        Exchange(sort);
               }break;
                             
               case 3:
                        if (profile[i].height<profile[i].height){
                        Exchange(sort);
               }break;
               case 4:
                        if (profile[i].bmi>profile[i].bmi){
                        Exchange(sort);
               }break;
               case 5:
                        printf("\nLeaving...");
               break;
               
        }
}
/* ORDERS IF CHOSEN */
void Exchange(person* sort)
{
     person aux;
     person *profile;
     int i;
               aux=profile[i];
               profile[i]=profile[i+1];
               profile[i+1]=aux;

}

 

A imagem da tela dando erro:

http://img4.imageshack.us/i/summergirlprogramerror.jpg/

Alguém pode me ajudar para fazer esse programa rodar corretamente?

Muito obrigada.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Resposta irônica: porque alguma coisa está errada.

1 - Não use fflush(stdin). Nâo interessa se no Windows funciona. Isso não é padrão.

2- Evite usar system.

3- Não use gets.

 

Falha de segmentação

 

Reveja os acessos à memória.

 

Enter the maximum record:1

 

Name: ---: 2

Age: 3

Weight: 4

Height: 5

 

Record Ok

 

 

1 - Alphabetically

2 - weight (increasing)

3 - Height (descending)

4 - BMI (increasing)

5 - Exit

Enter an option: 2

 

Breakpoint 1, PerformOperation (option=2) at err.c:122

122 if (profile.weight>profile[i+1].weight){

(gdb) display profile

1: profile = (person *) 0x0

 

Sério, se você está usando Dev C/C++ jogue fora essa tralha e baixe um Netbeans ou Eclipse, porque IDE que fecha o programa na cara e apresenta erros tão informativos quanto ler revista de fofoca não pode ser algo que preste.

Compartilhar este post


Link para o post
Compartilhar em outros sites

Muito obrigada pelas dicas.

Instalei o Eclipse, criei um projeto e colei esse codigo.

Quando eu clico em Run project aparece isso aqui:

http://img205.imageshack.us/i/summergirleclipse.jpg/

 

Estou baixando um software chamado Code::Blocks. Ele é melhor que o Dev-C++ ?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Se o Code::Blocks é melhor que o Dev-C++?

 

Absolutamente SIM!

 

Dá pra sentir a diferença só ao abrir o programa...

 

Pode acreditar, ele é BEM melhor!

 

Que o Dev-C++ claro, pois não dá pra trocar o Eclipse por eles!

Compartilhar este post


Link para o post
Compartilhar em outros sites

você lembrou de compilar o fonte antes de executar? Nem sempre o build é feito automaticamente.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.