-2

this is my code

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

class Hamoud{
      private: 
      char name[50];
      int yearbirthday;

    public: 
        float tall;
        int age;
        void Getnameandyear(string name)
{
    std::array<char, 50> ;
    cout<<"enter your name: ";
    getline(nama);
    int year;
    year = yearbirthday;
    cout<<"enter your name: ";
    getchar(name);
    cout<<"Enter your year of birth";
    cin>>year;
}
    void display()
{
    cout<<"your name is: "<<name;
    cout<<"your year of birth is : "<<year;

}
};
int main ()
{
    Hamoud info;
    cout<<"enter your tall ";
    cin>>info.tall;
    cout<<"your tall is : "<<info.tall;
    cout<<"enter your age: ";
    cin>>info.age;
    cout<<"your age is: "<<info.age;
    info.Getnameandyear();
    info.display();

}

but i got errors in the function getnameandyear also in display function as well... i know that to access the private members of the class we have to create a function in public that will help us on access then indirectly.... However, I'm getting stuck on the last few steps.. Any idea of how to solve this problem ??

1 Answers1

0

You probably want something like this:

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

class Hamoud {
private:
  string name;
  int yearbirthday;

public:
  float tall;
  int age;

  void Getnameandyear()
  {
    // no need for std::array<char, 50> or whatever here anyway
    cout << "enter your name: ";
    cin.ignore();   // this clears the input buffer. The exact reason why
                    // we need this is beyond your scope for the moment
    getline(cin, name);

    cout << "Enter your year of birth: ";
    cin >> yearbirthday;
  }

  void display()
  {
    cout << "your name is: " << name << "\n";
    cout << "your year of birth is: " << yearbirthday << "\n";
  }
};

int main()
{
  Hamoud info;
  cout << "enter your tall ";
  cin >> info.tall;
  cout << "your tall is: " << info.tall << "\n";
  cout << "enter your age: ";
  cin >> info.age;
  cout << "your age is: " << info.age << "\n";
  info.Getnameandyear();
  info.display();
}

But anyway, I think you should start reading good beginner's C++ text book.

About the cin.ignore(); before getline: the exact reason is a bit beyond your scope for the moment, but you can find detailed information about this issue in this SO article.

Jabberwocky
  • 40,411
  • 16
  • 50
  • 92