I am novice and am having trouble running my program properly! Help

Asked

Viewed 77 times

-1

My code has to do the following:

Escreva um programa que receba do usuário 4 nomes (nome1, nome2, nome3, nome4) e 4 salários (sal1, sal2, sal3, sal4) de funcionários de uma empresa. Exiba na tela a média (med) e o desvio padrão (s) dos salários. A equação que descreve o desvio padrão está listada a seguir:

Onde:

x_i is the sample at position i; x is the arithmetic mean of the samples; right the number of samples.

It compiles and opens, but the function of the second scanf is skipped, and there are problems in capturing the data too, follow the code:

//bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//inicio da funcao main
int main ()
{
    //declaracao de variaveis
    char nome1, nome2, nome3, nome4 [50];
    float sal1, sal2, sal3, sal4, media, desvio;

    //captacao, calculo e mostra dos dados
    printf ("\nDigite os nomes dos quatro funcionarios: ");
    scanf (" %c %c %c %c ", &nome1, &nome2, &nome3, &nome4);
    printf ("\nFoneca seus salarios, respectivamente: ");
    scanf (" %f %f %f %f ", &sal1, &sal2, &sal3, &sal4);
    media= (sal1+sal2+sal3+sal4)/4;
    printf ("\nA media aritmetica desses salarios e: %f", media);
    desvio=sqrt ((pow(sal1-media,2)+pow(sal2-media,2)+pow(sal3-media,2)+pow(sal4-media,2))/(4-1));
    printf ("\nO desvio padrao dos salarios e: %f", desvio);
    system ("pause");
}

===============================================================

att code after the first comment:

//bibliotecas
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//inicio da funcao main
int main ()
{
    //declaracao de variaveis
    char nome1 [20], nome2 [20], nome3 [20], nome4 [20];
    float sal1, sal2, sal3, sal4, media, desvio;

    //captacao, calculo e mostra dos dados
    printf ("\n\aDigite os nomes dos quatro funcionarios: ");
    scanf (" %s %s %s %s ", nome1, nome2, nome3, nome4);
    printf ("\n\aFoneca seus salarios, respectivamente: ");
    scanf (" %f %f %f %f ", &sal1, &sal2, &sal3, &sal4);
    media= (sal1+sal2+sal3+sal4)/4;
    printf ("\n\aA media aritmetica desses salarios e: %f", media);
    desvio=sqrt ((pow(sal1-media,2)+pow(sal2-media,2)+pow(sal3-media,2)+pow(sal4-media,2))/(4-1));
    printf ("\n\aO desvio padrao dos salarios e: %f", desvio);
    system ("pause");
}
  • With %c in the scanf you are reading a single character and not a string of characters. To read a string use %s and do not put & before the array name. In your statement you must put the amount of characters in each of the names and not only in the last.

  • Thanks for the help, I fixed all that I said! But the main problem still persists. The second scanf is not executed, it is skipped at once.

1 answer

0

Good afternoon Jackson,

The problem with your program is the use of scanf with this type of data. scanf reads the string until it finds a space, enter or tab, i.e.: ", "n" or " t".

There are some solutions to your problem:

First of all, I couldn’t help but notice that your show does absolutely nothing with the names he reads. then the first solution would be to create a large array to store the names and use the function gets().

Example:

char vetorNomes[100];

printf("Digite os nomes dos funcionarios: ");
gets(vetorNomes);

What happens here is that the function gets() reads ALL characters until you find a "Enter", i.e.: "n".

If you need to store the names in different variables for future operations you can create variables separately and use the gets() in each of them.

Example:

char nomeFuncionario1 [20], nomeFuncionario2 [20], nomeFuncionario3 [20], 
nomeFuncionario4 [20];

printf("Digite os nomes dos funcionarios: ");
gets(nomeFuncionario1);
gets(nomeFuncionario2);
gets(nomeFuncionario3);
gets(nomeFuncionario4);

And last but not least, you can state your type of data on scanf as %[^\n]. %[] is a type of data that reads a string, and the syntax " n" serves to point out that when it comes across a Enter, the reading is over.

Example:

char nomeFuncionario1 [20], nomeFuncionario2 [20], nomeFuncionario3 [20], 
nomeFuncionario4 [20];

printf ("\n\aDigite os nomes dos quatro funcionarios: ");
scanf("%[^\n] %[^\n] %[^\n] %[^\n]", nomeFuncionario1, nomeFuncionario2, 
nomeFuncionario3, nomeFuncionario4);

It will work in the same way as the example of gets() above.

I hope I helped. Hugs!

Browser other questions tagged

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