String usage gives error

0

At the beginning of the code I declare the variable nome as char and when I try to get the nome variable down there in the code it gives error, I already tried in several ways and that way down there until it works, but sometimes when the user asks "Do you want to continue?" and it responds s (yes), sometimes it jumps directly to the field "enter the salary of the employee:", that is. skip the part where you ask the employee's name

int cont, tinss;

char s, resp, nome[50];

float sal, novosala, val; 

cont = 1;

do
{
    printf("\n digite o nome do funcionario; ");
    scanf("%s",&nome);
    printf("\n digite o salario do funcionario: ");
    scanf("%f",&sal);

    if(sal <= 500)
        {
            novosala = sal - ((sal / 100) * 8);
            val = (sal / 100) * 8;
            tinss = 8;              
        }
    else
    if(sal > 500 &&  sal <= 1000)
        {
            novosala = sal - ((sal / 100) * 10);
            val = (sal / 100) * 10;
            tinss = 10; 
        }
    else
    if(sal > 1000)
        {
            novosala = sal - ((sal / 100) * 12);
            val = (sal / 100) * 12;
            tinss = 12;
        }


    printf("\n Nome: %s \n",nome);
    printf("\n Salario bruto: %f \n", sal);
    printf("\n Taxa de INSS: %d% \n", tinss);
    printf("\n Valor de INSS %f \n", val);
    printf("\n Salario liquido: %f \n", novosala);

    printf("\n deseja continuar ? \n");
    resp = getch();
    cont = cont + 1;
}
while(resp == 's');
    
asked by anonymous 18.09.2015 / 21:00

1 answer

1

First, you may even be using a C ++ compiler but you are programming in C. And you are using a function that should not be used any more. Delete the getch() and the problem will be solved.

It has other functions that can be used but I would use scanf() so as not to be queer time to have ENTER , time not.

Then it would look like this:

#include <stdio.h>

int main() {
    int cont, tinss;

    char s, resp, nome[50];

    float sal, novosala, val; 

    cont = 1;

    do
    {
        printf("\n digite o nome do funcionario; ");
        scanf("%s",&nome);
        printf("\n digite o salario do funcionario: ");
        scanf("%f",&sal);

        if(sal <= 500)
            {
                novosala = sal - ((sal / 100) * 8);
                val = (sal / 100) * 8;
                tinss = 8;              
            }
        else
        if(sal > 500 &&  sal <= 1000)
            {
                novosala = sal - ((sal / 100) * 10);
                val = (sal / 100) * 10;
                tinss = 10; 
            }
        else
        if(sal > 1000)
            {
                novosala = sal - ((sal / 100) * 12);
                val = (sal / 100) * 12;
                tinss = 12;
            }


        printf("\n Nome: %s \n",nome);
        printf("\n Salario bruto: %f \n", sal);
        printf("\n Taxa de INSS: %d% \n", tinss);
        printf("\n Valor de INSS %f \n", val);
        printf("\n Salario liquido: %f \n", novosala);

        printf("\n deseja continuar ? \n");
        scanf("%s",&resp);
        cont = cont + 1;
    }
    while(resp == 's');
    return 0;
}

See running on ideone .

More C ++ Version:

#include <iostream>
#include <string>
using namespace std;

int main() {
    int cont = 1, tinss;
    string s, resp, nome;
    float sal, novosala, val; 
    do {
        cout << endl << "digite o nome do funcionario: ";
        cin >> nome;
        cout << endl << "digite o salario do funcionario: ";
        cin >> sal;

        if(sal <= 500) {
            novosala = sal - ((sal / 100) * 8);
            val = (sal / 100) * 8;
            tinss = 8;              
        } else if (sal > 500 &&  sal <= 1000) {
            novosala = sal - ((sal / 100) * 10);
            val = (sal / 100) * 10;
            tinss = 10; 
        } else if(sal > 1000) {
            novosala = sal - ((sal / 100) * 12);
            val = (sal / 100) * 12;
            tinss = 12;
        }
        cout << endl << "Nome: " << nome << endl;
        cout << "Salario bruto: " << sal << endl;
        cout << "Taxa de INSS: " << tinss << endl;
        cout << "Valor de INSS " << val << endl;
        cout << "Salario liquido: " << novosala << endl;

        cout << endl << "deseja continuar ? " << endl;
        cin >> resp;
        cont++;
    }
    while(resp == "s");
    return 0;
}

See running on ideone .

You can do better, but you're more organized.

    
18.09.2015 / 21:15