C++, Program does not resolve all Instances!

Asked

Viewed 76 times

1

Hey, good night, man. I’m in the first period of computer science, my university has its own website that evaluates its code and gives you a grade from 0 to 100, but it turns out that in this code below I get 93.3, and the site says that the program does not solve everything that is needed. I’m just using everything you’ve been teaching,I want to know where it’s wrong, or what’s missing. Thank you very much! Esse é o problema

This is my code;

#include <iostream>
using namespace std;
int main (){
int codigo;
float salarioatual,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;
 }
  • What’s your problem? You can also post your code in textual mode, instead of a print that we can’t interact with?

  • I tried to post textual and left by half, what is the form I post textual? the problem is that the site sees an error in it understood, there is some issue that it does not solve, and I can not see this issue!

2 answers

1

Your code is correct and will work as stated.

However, as this is an academic work, I believe the intent of the problem is that the first-degree equation involving the calculation of percentages is implemented in the code.

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

I suggest that you implement the "Rule of Three" that is at the heart of solving the problem, maybe so you get the top 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;
}
  • Funny, that the two do the same functions, and as you said, gave 100, thank you very much! , You can explain to me what is the library string ?

  • @Victoroliveirasilva: It was just an addiction of mine to use the standard type std::string to store texts, hence the need for #include <string>. But in this case, the thing can be simpler and a pointer const char can be used. See my edit.

  • @Victoroliveirasilva: A curious observation is the fact that the program you wrote is theoretically more efficient that the program of my answer. It happens because it does not perform the division by 100, in fact, this division was pre-calculated "inside your head", saving the computer from having to perform this "arduous" task.

  • I got it, it’s very interesting, the site here in the college has a lot of these things that don’t make much sense, and then we have to adapt to it, because he is the one with the note kkkk!! Thanks man!

0

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

Note that this will certainly not be the expected way to solve the exercise and it is possible to get an even lower grade, 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;
}
  • Very interesting, in the case here, I have to use the Switch because we are learning the repetition structures, so all exercises, I have to use, switch or if Else. However, this method seems more complex looking at first, but gives much less work and is very interesting, I believe I’ll still learn this!! Thanks for showing another way!

Browser other questions tagged

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