Home > OS >  why my code does not work when I put Variables inside vector with operand
why my code does not work when I put Variables inside vector with operand

Time:10-31

//This Code WORKS.

//Main driver

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    string name, age;

    name = "Muzaib";
    age = "16";

    std::vector<std::string> B = {name   "\n"   age   "lll"   '\n'};
    B.push_back(name   '\n'   age);
    cout << B[0];
     cout << B[1];
    return 0;
}

//This Code DOES NOT WORK.

#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
using namespace std;

//Doctor class

class Doctor : public Hospital
{
    protected:
        std::vector<std::string> doctors;               //contains list of doctors.
        string Docname;
        int Docage;
        string Docgender;
        string specialty;

    public:
        Doctor();                       //Constructor
        void listDoctors();
        void addDoctor();
        void removeDoctor();
    
};

// Allows user to add doctor to the list.

void Doctor::addDoctor()
{
    cin.ignore();
    cout << "Enter Doctors name: ";
    getline(cin, Docname);
    cout << "\nEnter Soctors age: ";
    cin >> Docage;
    cin.ignore();
    cout << "\nEnter Doctors gender";
    getline(cin, Docgender);
    cout << "\nEnter Doctors specialty: ";
    getline(cin, specialty);

//This line of code is giving me the following error: [Error] no match for 'operator ' (operand types are 'std::basic_string' and 'int') I do not get any errors on the other program.

    doctors.push_back(Docname   '\n'   "Age: "   Docage   '\n'   "Gender: "   Docgender   '\n' 
      "Specialty: "   specialty);
}

CodePudding user response:

I think you should have a try with below code:

doctors.push_back(Docname '\n' "Age: " std::to_string(Docage) '\n' "Gender: " Docgender '\n' "Specialty: " specialty);

BR,

  • Related