C ++, Program does not solve all Instances!

1

Hello, good evening. I am in the first period of computer science, my university has a website of its own that evaluates your code and gives you a score from 0 to 100, but it happens that in this code I get 93.3, and the website says that the program does not solves everything that is needed. I'm just using everything I've been teaching, I want to know where it's wrong, or what's missing. Many thanks!

Thisismycode;

#include<iostream>usingnamespacestd;intmain(){intcodigo;floatsalarioatual,aumento,novosalario;cin>>codigo>>salarioatual;switch(codigo){case(1):aumento=salarioatual*0.5;novosalario=salarioatual+aumento;cout<<"Escrituario"<<endl<<aumento<<endl<<novosalario;
    break;
    case (2):
    aumento=salarioatual*0.35;
    novosalario=salarioatual+aumento;
    cout<<"Secretario"<<endl<<aumento<<endl<<novosalario;
    break;
    case(3):
    aumento=salarioatual*0.20;
    novosalario=salarioatual+aumento;
    cout<<"Caixa"<<endl<<aumento<<endl<<novosalario;
    break;
    case(4):
    aumento=salarioatual*0.10;
    novosalario=salarioatual+aumento;
    cout<<"Gerente"<<endl<<aumento<<endl<<novosalario;
    break;
    case(5):
    aumento=salarioatual*0;
    novosalario=salarioatual+aumento;
    cout<<"Diretor"<<endl<<aumento<<endl<<novosalario;
    break;
    default:
    cout<<"Codigo nao definido";
 }  
 return 0;
 }
    
asked by anonymous 11.10.2017 / 23:50

2 answers

1

Your code is correct and will work as stated.

However, since this is an academic paper, I believe that the problem is that the first-order equation involving percentage calculation is implemented in the code.

In your implementation, instead of using percentages directly, you pre-calculated the wage increase coefficient, for example: 0.5 instead of 50% , 0.35 instead of 35% and so on ...

I suggest that you implement the "Rule of Three" that is at the heart of solving the problem, so maybe you get the maximum grade, for example:

#include <iostream>

int main(void) {
    int cod = 0;
    const char * cargo = NULL;
    float salario = 0.0;
    float percentual = 0.0;
    float aumento = 0.0;
    float novosalario = 0.0;

    // Entrada
    std::cin >> cod >> salario;

    // Identifica o codigo do cargo
    switch( cod ) {
        case 1: percentual = 50; cargo = "Escriturario"; break;
        case 2: percentual = 35; cargo = "Secretario"; break;
        case 3: percentual = 20; cargo = "Caixa"; break;
        case 4: percentual = 10; cargo = "Gerente"; break;
        case 5: percentual = 0; cargo = "Diretor"; break;
        default: cargo = "Codigo do cargo nao definido."; break;
    }

    /* Calcula novo Salario */
    aumento = salario * (percentual / 100.0);
    novosalario = salario + aumento;

    /* Saida */
    std::cout << cargo << std::endl << aumento << std::endl << novosalario << std::endl;

    return 0;
}
    
12.10.2017 / 00:24
0

As a matter of curiosity, I show you another way to solve the problem, using arrays for percentages and positions, thus avoiding switch . To make it even more interesting I opted to remove almost all the intermediate calculation variables and drastically compacted the writing.

Please note that this will certainly not be the expected way to resolve the exercise and you may get a lower score, but still, follow the example:

#include <iostream>
using namespace std;

int main(void) {
    int cod; float salario;
    const char cargos[5][20] = {{"Escriturario"},{"Secretario"},{"Caixa"},{"Gerente"},{"Diretor"}};
    const float percentuais[] = { 0.5f, 0.35f, 0.2f, 0.1f, 0};

    cin>>cod>>salario;
    if ((--cod)>=0 || cod<=4)
        cout<<cargos[cod]<<endl<<percentuais[cod]*100<<endl<<(1+percentuais[cod])*salario<<endl;
    else
        cout<<"Codigo do cargo nao definido.";

    return 0;
}
    
12.10.2017 / 01:58