0

Is there a way for me to add a variable to a local variables name? i.e

cout << "YOU HAVE SELECTED EMPLOYEE" << "employee & WhatEmployeeIsEdit".getname();

I have tried

"employee & WhatEmployeeIsEdit"

"employee + WhatEmployeeIsEdit"

and with my extremely limited coding vocabulary searching W3 has been very difficult. I have no coding experience whatsoever so I apologize if this is an improper question or has been asked elsewhere. The complete code is as follows :

#include<iostream>
#include<cmath>

#include "Learning.h"

using namespace std; // this makes it so you dont have to type STD:: every time you want to c out or in


class Employee { //rem class has attributes and behaviors
private: // rem unless its specifically public or protected, its private
    //classes like this do not have any data in them, instead they serve as a bluprint for all "employee"s that are made

    string Name; /// this is a variable declraration
    string Company;
    int Age;
public:// implied priavte if not stated, but reccomended to make it clear
    void setName(string name) { // name getter
        Name = name;
    }
    void setCompany(string company) { // company getter
        Company = company;
    }
    void setAge(int age) { // age getter
        Age = age;
    }
    string getName() {      ///name setter
        return Name;
    }
    string getCompany() {       ///Company setter
        return Company;
    }
    int getAge() {      ///age setter
        return Age;
    }
    void SayHI() { /// this is a function of the class employee
        cout<< "Name - " << Name << "\n Company - " << Company << "\n Age - " << Age << "\n\n -------------------------------------------------\n";
    }

    Employee(string name, string company, int age) { // this is the constructor, note that it is inside the class, and public but outside of all functions
        Name = name;
            Company = company;
            Age = age;
    }
};



int main() {

    //Employee employee1;               //these definitions are only avalible if attributes are set to public
    //employee1.Name = "joe";
    //employee1.Company = "WGU";
    //employee1.Age = 29;
    //employee1.SayHI();

    //Employee employee2;
     //employee2.Name = "Nate";
    //employee2.Company = "St.T";
    //employee2.Age = 22; 

    Employee employee1 = Employee("Ray", "Enviormentalist", 23);
    employee1.SayHI();

    Employee employee4 = Employee("Anna", "Stay at home", 28);
    employee4.SayHI();


    employee1.setAge(33);
    //std::cout << employee1.getName() << " is " << employee1.getAge() << " years old" <<"\n";

    int WhatUserWantsToDo;
    int WhatEmployeeIsEdit;

    cout << employee1.getName() << " is " << employee1.getAge() << " years old " << endl; 

    cout << "WHAT DO YOU WANT TO DO? \n  -  Age =1\n  -  Company =2";

        cin >> WhatUserWantsToDo;

        if (WhatUserWantsToDo == 1) {
            cout << "WHAT EMPLOYEE DO YOU WANT TO EDIT? \n 1 \n 2";
            cin >> WhatEmployeeIsEdit;
            cout << "YOU HAVE SELECTED EMPLOYEE" << "employee & WhatEmployeeIsEdit".getname();
            if (WhatEmployeeIsEdit == 1) // possible to make if whatEmployeeIsEdit == search for matching employee number? 
                // then ask for new employee age
                //then print employee name is X years old





        } if {
            //if they chose 2, goto select employee, then prompt for new complany name, and print employee name's  new company is x
        }
        else {
            cout >> "SORRY, PLEASE CHOOSE ONE OF THE SUGGESTED VALUES"
        } // maybe add back button? so you dont have to re-run the program each time?
            // how dose program hold data between runs? 
            //save function?
     




return 0
} // end main
  • 1
    What are you hoping your strings _do_? (You’ll surely need some different syntax, but until we know what the goal is…) – Davis Herring Sep 03 '23 at 00:37
  • The idea is that my line will say "YOU HAVE SELECTED EMPLOYEE [employees name]" but without having to build a whole bunch of if statements to check to see if the number they pressed is associated with a known employee in the system. I thought that since local variable employee# had a number I could put the console input into the local variable I am calling. Sorry if I'm using the wrong words or this makes no sense. – Macewindow54 Sep 03 '23 at 00:45
  • 1
    Sounds like you want to use an array, vector, or map – Kevin Sep 03 '23 at 00:46
  • Okay I will look into those things, thank you – Macewindow54 Sep 03 '23 at 00:47
  • 1
    Does this answer your question? [Convert string to variable name or variable type](https://stackoverflow.com/questions/7143120/convert-string-to-variable-name-or-variable-type) – Raymond Chen Sep 03 '23 at 01:36
  • Whenever you find yourself creating variable names with numbers on them (and you have more than two or so of them), you probably wanted to use a data structure that suports numeric indexing like an array or vector instead of separate variables. Instead of `employee1` and `employee2` you probably want `employee[0]` and `employee[1]` of either a plain array or possibly a `std::vector`. (Also, remember that array indexing starts from 0, not 1 in C++). – Wyck Sep 03 '23 at 02:12
  • It would be better to focus on what you want to accomplish than on how you think you could accomplish it. See [What is the XY problem?](https://meta.stackexchange.com/q/66377) – JaMiT Sep 03 '23 at 04:50

0 Answers0