-1

I am trying to get better at C++ by challenging myself by creating a program that manages employees. Given a certain salary vs. sales you will either be hired, fired or keep your job. I have been working with OOP a lot recently and thought that structuring it by class would make the exercise easier. I've gone over this again and again and I can't seem to find a solution. That being said I would very much appreciate some insight on what I am doing wrong. Keep in mind I am new to C++ and OOP but the more I learn the better I will be in the future.

Originally it was all one file then I separated the classes into header files. It seems to run a little smoother now but I still get the error that name, salary and scope (my variables) aren't defined in the scope of my program.


main.cpp

#include "Boss.h"
#include "Employee.h"
#include "PotentialEmployee.h"
#include <iostream>


using namespace std;


int main ()
{


  Boss Boss1;
  Boss Boss2;
  Boss Boss3;

  Employee Employee1;
  Employee Employee2;
  Employee Employee3;

  PotentialEmployee PotentialEmployee1;
  PotentialEmployee PotentialEmployee2;

  Employee1.name = "Michael";
  Employee2.name = "John";
  Employee3.name = "Lisa";

  Boss1.name = "Luke";
  Boss2.name = "Ben";
  Boss3.name = "Jack";

  PotentialEmployee1.name = "Bill";
  PotentialEmployee2.name = "Fred";

  Employee1.salary = 55000;
  Employee2.salary = 65000;
  Employee3.salary = 75000;

  Boss1.salary = 88000;
  Boss2.salary = 95000;
  Boss3.salary = 88000;

  PotentialEmployee1.salary = 55000;
  PotentialEmployee2.salary = 65000;

  Employee1.sales = 12000;
  Employee2.sales = 40000;
  Employee3.sales = 80000;

  Boss1.sales = 200000;
  Boss2.sales = 250000;
  Boss3.sales = 280000;

  PotentialEmployee1.sales = 55000;
  PotentialEmployee2.sales = 65000;


  if (sales <= salary) 
    std::cout << "I'm sorry " << name << "you have been fired";

  else if (sales >= salary) 

      std::cout << "Good job " << name << "you get a bonus";

   else 
      std::cout << "Try and do better next quarter";


      return 0;
    }

main.cpp: In function ‘int main()’: main.cpp:66:7: error: ‘sales’ was not declared in this scope if (sales <= salary) { ^ main.cpp:66:16: error: ‘salary’ was not declared in this scope if (sales <= salary) { ^ main.cpp:67:34: error: ‘name’ was not declared in this scope std::cout << "I'm sorry " << name << "you have been fired"; ^ main.cpp:71:35: error: ‘name’ was not declared in this scope std::cout << "Good job " << name << "you get a bonus";

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Can you please include the exact error you get? – Avery Mar 29 '19 at 14:41
  • 3
    1) "_I still get the error that name, salary and scope (my variables) aren't defined in the scope of my program._" Please copy-paste the error-message, instead of paraphrasing it. 2) Please provide [mcve]. – Algirdas Preidžius Mar 29 '19 at 14:41
  • 1
    Where *are* `name`, `salary`, and `scope` declared? They are not declared in this code. (You're not using anything called "scope". Did you mean "sales"?) – molbdnilo Mar 29 '19 at 14:48
  • I apologize, i'm new to StackOverflow and it only let me include the main file. Let me add the rest. – Jordan Brandes Mar 29 '19 at 14:52
  • Here are my exact errors: main.cpp: In function ‘int main()’: main.cpp:66:7: error: ‘sales’ was not declared in this scope if (sales <= salary) { ^ main.cpp:66:16: error: ‘salary’ was not declared in this scope if (sales <= salary) { ^ main.cpp:67:34: error: ‘name’ was not declared in this scope std::cout << "I'm sorry " << name << "you have been fired"; ^ main.cpp:71:35: error: ‘name’ was not declared in this scope std::cout << "Good job " << name << "you get a bonus"; ^ – Jordan Brandes Mar 29 '19 at 14:55
  • Name, salary and scope are declared in the individual header files. They all follow this format. #include #pragma once using namespace std; struct Boss { public: string name; int salary; int sales; }; – Jordan Brandes Mar 29 '19 at 14:56
  • you can [edit](https://stackoverflow.com/posts/55419729/edit) your question. I added the error message for you, but the formatting is horrible, maybe you can fix that. – 463035818_is_not_an_ai Mar 29 '19 at 14:57
  • 1
    @JordanBrandes Those are member variables. You've been assigning values to them, and examining their values looks exactly the same: `Boss1.sales <= Boss1.salary`. Perhaps it's time for a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – molbdnilo Mar 29 '19 at 14:59
  • I see no `sales` defined anywhere in the given code. Presumably you have `Boss::sales` and `Employee::sales`, but no `sales`. – user4581301 Mar 29 '19 at 15:00
  • Got it! Thank you guys for your help! I was hoping to do it in a more general way where I wouldn't have to write out Boss1, Boss2, Boss3 each time but it looks like that's the best way to do it. – Jordan Brandes Mar 29 '19 at 15:02
  • Sidenote: When you have numbered variables, it usually means you want an array. `Boss Bosses[3];` and later `Boss[0].name = "Luke";`. Also read up on [aggregate initialization](https://stackoverflow.com/questions/17712872/what-is-aggregate-initialization). It's very handy stuff. – user4581301 Mar 29 '19 at 16:39

1 Answers1

2

As the error tries to tell you there is no sales and no salary declared in main. You probably want to refer to the Employees sales and salary, that would be

if (Employee1.sales <= Employee1.salary) 
    std::cout << "I'm sorry " << name << "you have been fired";

Some more tips:

  • better use small starting letters for object names and capitals for types. In your code it is not easy to see what is a variable and what is a type.
  • whenever you find yourself naming variables foo1, foo2, foo3 etc you should use a std::vector or an array instead. This will help you to loop over all employes and evaluate their sales/salary.
  • the {} for if and else are optional, but it is often better to write them any way.
  • your two cases are not exclusive. If sales == salary then both conditions would be true, but only the first one will get evaluated. Hence there is a tiny confusion: Looking at the else condition one might expect that a employee with sales == salary did a good job, but they will get fired :(
  • last and least, a company that keeps their employess even if they generate zero income is a very nice one :)

PS:

I was hoping to do it in a more general way where I wouldn't have to write out Boss1, Boss2, Boss3 each time but it looks like that's the best way to do it

When you find yourself writing a block of code that has lots of Boss1.some_member then that block of code is a good candidate to be actually implemented in a member function (where you dont need to prefix the object). Eg the employee could have a bool Employee::evaluate_salary_vs_sales(); that contains the logic you now have in main and returns whether they get fired or not.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Thank you so much for your help. As I said i'm new and the more guidance I have the better I'll be in the future. I'll edit my code in the future. – Jordan Brandes Mar 29 '19 at 15:11
  • @JordanBrandes glad to help, though you should take what I wrote with a grain of salt, SO is not the best place for tutoring and my answer is lots of handwaving. For proper learning there is no way around getting a [book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 463035818_is_not_an_ai Mar 29 '19 at 15:37