3
I would like a help in pointer call concepts, and dynamic allocation with struct
.
I’m developing a code to address an issue of college work, and I’m coming across some errors that I believe are conceptual, but I’m not getting around to solving them. Someone could give me a light?
I didn’t know that there was a restriction of aid in a programming community, either due to language, so maybe this request for help could be duplicated.
My problem is basically in the function that I create the "report" of the registered actions, the compiler does not return all the registered information besides returning an error message from . dll (sic!)
Follows the code:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <string.h>
#define MAX 100
#define QACOES 5
struct Data
{
int dia;
int mes;
int ano;
};
struct bvalores
{
char codigoAcao[5];
char areaAtuacao[MAX];
struct Data data;
float valorAbertura = 0;
float valorFechamento = 0;
double variAcao;
};
struct bvalores* acoes[QACOES];
int contador;
//recebe os dados digitados pelo usuário
void registros(struct bvalores* acoes)
{
setlocale(LC_ALL, "Portuguese");
printf_s("Digite o código da ação: ");
scanf_s("%s", &acoes->codigoAcao, 5);
printf_s("Digite a área de atuação da empresa: ");
scanf_s("%s", &acoes->areaAtuacao, MAX);
printf_s("Qual o valor de aberta: ");
scanf_s("%f", &acoes->valorAbertura);
printf_s("Qual o valor de fechamento: ");
scanf_s("%f", &acoes->valorFechamento);
acoes->variAcao = (acoes->valorAbertura - acoes->valorFechamento) / acoes->valorAbertura;
acoes->variAcao *= 100;
printf("Data de lançamento: ");
scanf_s("%d / %d / %d", &acoes->data.dia, &acoes->data.mes, &acoes->data.ano);
}
//registra cada ação com seus respectivos dados em uma posição da memória
void novoregistro()
{
setlocale(LC_ALL, "Portuguese");
if (contador < QACOES)
{
*(acoes + contador) = (struct bvalores*)malloc(1 * sizeof(struct bvalores));
registros(acoes[contador]);
contador++;
}
else
{
printf_s("Só é possível 5 registros. Limite excedido");
}
}
char menu()
{
setlocale(LC_ALL, "Portuguese");
printf_s("\n");
printf_s("Digite [I] para incluir um novo registro: \n");
printf_s("Digite [R] para visualizar a variação das ações registradas: \n");
printf_s("Digite [S] para sair do programa: \n");
printf_s("\nQual a opção desejada: ");
int opcao = getchar();
int c;
while ((c = getchar()) != '\n' && c != EOF)
{
opcao = c;
}
return opcao;
}
//função que monta um relatório dos registro das açoes
void relatorio()
{
setlocale(LC_ALL, "Portuguese");
struct bvalores* acao;
for (int i = 0; i <= contador; i++)
{
acao = *(acoes + i);
printf_s("Ação da bolsa: %s - Area de atuação: %s\n", acao->codigoAcao, acao->areaAtuacao);
printf_s("Data da operação: %d/%d/%d\n", acao->data.dia, acao->data.mes, acao->data.ano);
printf_s("\n");
printf_s("Valor de abertura: R$ %.2f - Valor de fechamento: R$ %.2f\n", acao->valorAbertura, acao->valorFechamento);
printf_s("Variãção do dia: %.2f%%", acao->variAcao);
printf_s("\n");
}
}
int main()
{
char sopcao;
do
{
sopcao = menu();
switch (sopcao)
{
case 'i':
case 'I':
novoregistro();
break;
case 'R':
case 'r':
relatorio();
break;
default:
printf_s("Opção invalida");
break;
}
} while (sopcao != 'S' && sopcao != 's');
system("pause");
return 0;
}
You did not assign an initial value to the variable
contador
before using it.– anonimo
Welcome to the Sopt. Our goal is to form a repository of questions and answers useful to several users so that one can find an answer through the search engine, either the one embedded in the site or a search engine like Google. There is no restriction of the language as you spoke, but the question being about any doubt regarding languages or programming involving them, or conceptual. In case of your doubt for example it is interesting to reduce the code to the minimum necessary to reproduce the problem and cite in text the error message or log that is being presented.
– Piovezan
It is also interesting to break your other doubts into individual questions concerning each subject. P.S.: Very good your code. Take the opportunity to use the tool and ask questions here.
– Piovezan
Also ask questions about the operation of the site in the area Meta (meta.pt.stackoverflow.com), which is a Q&A apart. And there is also worth the search.
– Piovezan
Finally pay attention to the importance of adapting the title of the question to the question being asked, in your case as many were very generic and less useful to locate.
– Piovezan
Piovezan, I appreciate the tips... It is my first post and I promise to follow your guidelines in the next...
– Andre Coelho