0

(Pardon my english) So im new at coding. the main problem on my program is that when I multiply a variable with another variable, it just doesn't work. here is the full program.

#include <iostream>
using namespace std;

class employment {
  private:
    // Private attribute
    int salary;
    

  public:
    // Setter
    void setSalary(int s) {
      salary = s;
    }
   
    
    // Getter
    int getSalary() {
      return salary;
    }
    
};

void divider(int y){
    for(int i=1;i<y; i++){
        cout<<"=============================\n";
    }
}

int main() {
    string name,category, status;
    char penalty,i;
    int penaltyAm, categoryAm;
    
    //Regional division
    employment regionalDiv;
    regionalDiv.setSalary(5000000);
  
    //Administration and warehousing
    employment adminWarehouse;
    adminWarehouse.setSalary(5700000);
  
    //Factory manager
    employment factManager;
    factManager.setSalary(15000000);

    //Marketing manager
    employment marketManager;
    marketManager.setSalary(16000000);

    //Personnel manager
    employment persManager;
    persManager.setSalary(18000000);

    // Other manager
    employment otManager;
    otManager.setSalary(25000000);
    
    //Personnel director
    employment persDirector;
    persDirector.setSalary(35000000);
    
    //Other director
    employment otDirector;
    otDirector.setSalary(40000000);
    
    //Director of finance
    employment finDirector;
    finDirector.setSalary(45000000);
    
    //President director
    employment presDirector;
    presDirector.setSalary(50000000);
    
    //Director
    employment director;
    director.setSalary(60000000);
    
    do{
    //asking for employment category
    divider(3);
    cout<< "Name: ";
    getline(cin,name);
    cout<< "employment category: ";
    getline(cin, category);
    categoryAm=0;
    if(category=="Regional division"){
        cout<< "Salary: "<<regionalDiv.getSalary()<<endl;
        categoryAm= 5000000;
        }
        
        else if(category=="Administration and warehousing"){
        cout<< "Salary: "<<adminWarehouse.getSalary()<<endl;
        categoryAm= 5700000;
        }
        
        else if(category=="Factory manager"){
        cout<< "Salary: "<<factManager.getSalary()<<endl;
        categoryAm= 15000000    ;   
        }
        
        else if(category=="Marketing manager"){
        cout<< "Salary: "<<marketManager.getSalary()<<endl;
        categoryAm= 16000000;
        }
        
        else if(category=="Personnel manager"){
        cout<< "Salary: "<<persDirector.getSalary()<<endl;
        categoryAm= 18000000;       
        }
        
        else if(category=="Other manager"){
        cout<< "Salary: "<<otManager.getSalary()<<endl;
        categoryAm= 25000000;
        }
        
        else if(category=="Personnel director"){
        cout<< "Salary: "<<persDirector.getSalary()<<endl;
        categoryAm= 35000000;       
        }
        
        else if(category=="Other director"){
        cout<< "Salary: "<<otDirector.getSalary()<<endl;
        categoryAm= 40000000;
        }
        
        else if(category=="Director of finance"){
        cout<< "Salary: "<<presDirector.getSalary()<<endl;
        categoryAm= 45000000;       
        }
        
        else if(category=="President director"){
        cout<< "Salary: "<<presDirector.getSalary()<<endl;
        categoryAm= 50000000;
        }
        
        else if(category=="Director"){
        cout<< "Salary: "<<director.getSalary()<<endl;      
        categoryAm= 60000000;
        }
        
        
        cout<<"Penalty(T/F): ";
        cin>> penalty;
        if(penalty=='T'){
            cout<<"Amount of penalty(%): ";
            cin>>penaltyAm;
            penaltyAm=100-penaltyAm;
//          cout<<penaltyAm<<endl;
//          cout<<categoryAm<<endl;
        int moSalary=categoryAm*penaltyAm/100;
            cout<<"This month salary: "<<moSalary<<endl;
        }else{
            cout<<"This month salary: "<<categoryAm<<endl;
        }
        cout<<"Status :";
        cin>>status;
        divider(2);
        cout<<"new data(Y/N) :";
        cin >> i;
        cin.ignore (); 
    }while(i=='Y');
    
    
  return 0;
}

the problem occurs here

cout<<"This month salary: "<<moSalary<<endl;

cin category variable as "Regional division" (aka first if) it just works well.

but when i cin category variable with other than "Regional division", the moSalary variable turned to minus something and that's illogical. Anyway i don't understand the way company works. this is just my excercise.

i tried to change moSalary declaration places but it still fails.

i hope someone can help me to make the moSalary variable works well.

tried 1:

so this is how the program went please look at the "This month salary:.."

when I inputed "Personnel director", it should set categoryAm= 5000000. and I set penalty to Y, means that I should enter the penalty number in percent. i inputed penaltyAm = 10. then I assigned penaltyAm to 100-penaltyAm that is 90 so I can multiply it later.

it should be good but then the output of int moSalary=categoryAm*penaltyAm/100; became minus and I don't understand why.

tried 2: I tried to change moSalary type data into float. it become more confusing for me as I don't have basic knowledge of it. 1.44e+007 what TvT help :"

tried 3: so I tried adding #include<stdint.h> and changing moSalary data type into int64_t and it produced minus number again. Is there anything else I can try? thank you.

Sharfina
  • 11
  • 2
  • Please show the very operation that generates a negative (and give the values). –  Nov 29 '22 at 17:18
  • 5
    "the moSalary variable turned to minus something and that's illogical" No, it is very logical. You just don't know how computer arithmetic works. If you go outside of the range of int it will wrap around to the smallest negative number. Or at least most of the time (technically this is an undefined behaviour). Read more about [integer overflow](https://en.wikipedia.org/wiki/Integer_overflow). To solve this you need something bigger than integer (which typically weights 4 bytes). – freakish Nov 29 '22 at 17:20
  • @freakish: what you describe is how assembler might work, but in C++ (as the question is tagged) it's undefined behavior, isn't it? So anything can happen, not only negative numbers. So OP can be happy that it's only a negative number and he didn't order 2.1 billion pizzas. – Thomas Weller Nov 29 '22 at 17:41
  • Remember that the range of a 32 bit int is: -2147483648 to 2147483647. You may need a 64 bit int: [https://stackoverflow.com/questions/58324123/what-are-the-int64-t-range-limits-on-x86](https://stackoverflow.com/questions/58324123/what-are-the-int64-t-range-limits-on-x86) – drescherjm Nov 29 '22 at 17:41
  • Does this answer your question? [Is signed integer overflow still undefined behavior in C++?](https://stackoverflow.com/questions/16188263/is-signed-integer-overflow-still-undefined-behavior-in-c) – Thomas Weller Nov 29 '22 at 17:44
  • @ThomasWeller technically yes. However I'm not aware of any modern compiler/cpu that is different. – freakish Nov 29 '22 at 17:45
  • @YvesDaoust its edited, can you please help me? – Sharfina Nov 29 '22 at 18:15
  • @ThomasWeller it doesn't work, the number became negative.. is there perhaps any alternatives I can try? – Sharfina Nov 29 '22 at 18:17
  • @Austin should i try to change their data types? – Sharfina Nov 29 '22 at 18:18
  • Where did you change the data type exactly? Did you change `int moSalary`? The screenshot does not help. We need code. – Thomas Weller Nov 29 '22 at 18:24
  • @ThomasWeller yes, i changed `int moSalary` to `int64_t`. I placed it below `string name,category, status; char penalty,i; int penaltyAm, categoryAm;` should I copy all the new code? – Sharfina Nov 29 '22 at 18:30
  • 2
    Well, you should provide a [mre] of the problem, which, in this case, is something like: https://godbolt.org/z/Thes37YTE and has an easy fix: https://godbolt.org/z/1s8EP31zd – Bob__ Nov 29 '22 at 19:03
  • @Bob__ It really works! I just woke up and try it, thank you so much for helping me. – Sharfina Nov 30 '22 at 03:08

0 Answers0