String usage gives error

Asked

Viewed 79 times

1

At the beginning of the code I declare the variable nome as char and when I try to pick up the variable nome down there in the code gives error, I’ve tried several ways and this way this down goes right, but sometimes when the question appears to the user " Do you want to continue ? " and he answers s (yes), sometimes jumps straight to the field "type the employee’s salary:", ie. skips the part where asks 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');
  • Note that in your specific program, else if(sal > 1000) may be replaced by else.

  • Did any help you more? You need something to be improved?

  • Your code is in C, not C++. Change scanf("%s",&name); to scanf("%s", name);, because name is already the address of the initial position of the array, and also review the use of the getch function. Check that the input buffer is not getting dirty after Resp reading.

1 answer

2

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

There are other functions that can be used but I would use the scanf() until it doesn’t get weird time having to give the ENTER, time not.

Then it would look like this:

#include <stdio.h>

int main() {
    char resp;
    do {
    int cont = 1, tinss;
    char s, nome[50];
    float sal, novosala, val;
    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');
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Version plus C++:

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

int main() {
    string  resp;
    do {
        int cont = 1, tinss;
        string s, nome;
        float sal, novosala, val; 
        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");
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

You can do better, but it’s more organized.

Browser other questions tagged

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