You are here:

C++/my code has a problem

Advertisement


Question
hey..
i wrote a simple code in C++ but i don't know what is the problem
every thing seems alright but when i enter how many employees it goes directly to enter the second name without reading the first name :/ > here's the code





#include "iostream"
#include "string"
using namespace std;

struct EmployeeName{
  string FirstName;
  string SecondName;
  string LastName;
};
struct EmployeeBirthday{
  int year;
  int month;
  int day;
};
struct EmployeeAdress{
  string Adress1;
  string Adress2;
  string EmailAdress;
  int PhoneNumber;
  int MobileNumber;
};
class Employee{

  EmployeeName name;
  EmployeeBirthday age;
  EmployeeAdress Ad;
  //float Salary;
public:
  void Name(char* FN, string SN, string LN){
     name.FirstName = FN;
     name.SecondName = SN;
     name.LastName = LN;
  }
  string GetName(int i){
     switch (i){
     case 1: return name.FirstName; break;
     case 2: return name.SecondName; break;
     case 3: return name.LastName; break;
     default: return 0;
     }
     return 0;
  }
  void Age(int YE, int MO, int DA){
     age.year = YE;
     age.month = MO;
     age.day = DA;
  }
  int GetAge(int i){
     switch (i){
     case 1: return age.year; break;
     case 2: return age.month; break;
     case 3: return age.day; break;
     default: return 0;
     }
     return 0;
  }
  void Adress(string AD1, string AD2, string EM, int PH, int MB){
     Ad.Adress1 = AD1;
     Ad.Adress2 = AD2;
     Ad.EmailAdress = EM;
     Ad.PhoneNumber = PH;
     Ad.MobileNumber = MB;
  }
  string GetAdress(int i){
     switch (i){
     case 1: return Ad.Adress1; break;
     case 2: return Ad.Adress2; break;
     case 3: return Ad.EmailAdress; break;
     default: return 0;
     }
     return 0;
  }
  int CantactNum(int i){
     switch (i){
     case 1: return Ad.PhoneNumber; break;
     case 2: return Ad.MobileNumber; break;
     default: return 0;
     }
     return 0;
  }
  //void Salary();
};

Employee **Data;

void name(int EmployeesNumber){
  for (int i = 0; i < EmployeesNumber; i++){
     cout << "Enter the full name for the employee number" << i + 1 << endl;
     cout << "\nEnter the first name for the employee\n";
     char FN[20];
     cin.getline(FN,20);
     cout << "\nEnter the second name for the employee\n";
     string SN;
     getline(cin, SN);
     cout << "\nEnter the last name for the employee\n";
     string LN;
     getline(cin, LN);
     Data[i]->Name(FN, SN, LN);
  }

}
void Age(int EmployeesNumber){

  for (int i = 0; i < EmployeesNumber; i++){          //AGE
     cout << "Enter the date of birth for Employee number" << i + 1 << endl;
     cout << "\nEnter the birth-year of the employee\n";
     int YE;
     cin >> YE;
     cout << "\nEnter the birth-month of the employee\n";
     int MO;
     cin >> MO;
     cout << "\nEnter the bith-day of the employee\n";
     int DA;
     cin >> DA;
     Data[i]->Age(YE, MO, DA);
  }
}

void Adress(int EmployeesNumber){          // Adress of employees
  for (int i = 0; i < EmployeesNumber; i++){
     cout << "Enter the Adresses and how to contact employee number" << i + 1 << endl;
     cout << "\nEnter the adress1 for the employee\n";
     string AD1;
     getline(cin, AD1);
     cout << "\nEnter the adress2 for the employee\n";
     string AD2;
     getline(cin, AD2);
     cout << "\nEnter the e-mail adress for the employee\n";
     string EM;
     getline(cin, EM);
     cout << "\nEnter the Phone-number for the employee\n";
     int PH;
     cin >> PH;
     cout << "\nEnter the Mobile-number the employee\n";
     int MB;
     cin >> MB;
     Data[i]->Adress(AD1, AD2, EM, PH, MB);
  }
}
int NameSearching(int EmployeesNumber){
  cout << "Search by :\n 1. First name\n2. Second name\n3. Last name \n   Or use 1+2, 2+3, 1+3 or 1+2+3 to search by two or three names";
  string  c;
  cin >> c;
  if (!c.compare("1")){
     cout << "\nEnter the first name of the employee: ";
     string name;
     getline(cin, name);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(1).compare(name)){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("2")){
     cout << "Enter the second name of the employee: ";
     string name;
     getline(cin, name);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(2).compare(name)){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("3")) {
     cout << "Enter the last name of the employee: ";
     string name;
     getline(cin, name);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(3).compare(name)){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("1+2")){
     cout << "Enter the first name of the employee: ";
     string name1;
     getline(cin, name1);
     cout << "\nEnter the second name of the employee: ";
     string name2;
     getline(cin, name2);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(1).compare(name1) && !Data[i]->GetName(2).compare(name2)) {
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("1+3")) {
     cout << "Enter the first name of the employee: ";
     string name1;
     getline(cin, name1);
     cout << "\nEnter the last name of the employee: ";
     string name2;
     getline(cin, name2);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(1).compare(name1) && !Data[i]->GetName(3).compare(name2)) {
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("2+3")) {
     cout << "Enter the second name of the employee: ";
     string name1;
     getline(cin, name1);
     cout << "\nEnter the last name of the employee: ";
     string name2;
     getline(cin, name2);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(2).compare(name1) && !Data[i]->GetName(3).compare(name2)) {
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("1+2+3")) {
     cout << "Enter the first name of the employee: ";
     string name1;
     getline(cin, name1);
     cout << "\nEnter the second name of the employee: ";
     string name2;
     getline(cin, name2);
     cout << "\nEnter the last name of the employee: ";
     string name3;
     getline(cin, name3);
     for (int i = 0; i < EmployeesNumber; i++){
        if (!Data[i]->GetName(1).compare(name1) && !Data[i]->GetName(2).compare(name2) && !Data[i]->GetName(3).compare(name3)) {
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  return 0;
}

int AgeSearch(int EmployeesNumber){
  cout << "Search by :\n1. Birth-year\n2.Birth month\n3. Birth-day\n   Or you can use 1+2, 1+3, 2+3 or 1+2+3";
  string  c;
  getline(cin, c);
  if (!c.compare("1")) {
     cout << "\nEnter the Birth-year of the employee: ";
     int Y;
     cin >> Y;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(1) == Y){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("2")) {
     cout << "Enter the Birth-month of the employee: ";
     int Y;
     cin >> Y;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(2) == Y){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("3")) {
     cout << "Enter the Birth-day of the employee: ";
     int Y;
     cin >> Y;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(3) == Y){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("1+2")) {
     cout << "Enter the Birth-year of the employee: ";
     int Y;
     cin >> Y;
     cout << "\nEnter the Birth-month of the employee: ";
     int Y2;
     cin >> Y2;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(1) == Y && Data[i]->GetAge(2) == Y2){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("1+3")) {
     cout << "Enter the Birth-year of the employee: ";
     int Y1;
     cin >> Y1;
     cout << "\nEnter the Birth-day the employee: ";
     int Y2;
     cin >> Y2;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(1) == Y1 && Data[i]->GetAge(3) == Y2){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("2+3")) {
     cout << "Enter the Birth-month of the employee: ";
     int Y1;
     cin >> Y1;
     cout << "\nEnter the Birth-day the employee: ";
     int Y2;
     cin >> Y2;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(2) == Y1 && Data[i]->GetAge(3) == Y2){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  else if (!c.compare("1+2+3")) {
     cout << "Enter the Birth-year of the employee: ";
     int Y1;
     cin >> Y1;
     cout << "\nEnter the Birth-month of the employee: ";
     int Y2;
     cin >> Y2;
     cout << "\nEnter the Birth-day of the employee: ";
     int Y3;
     cin >> Y3;
     for (int i = 0; i < EmployeesNumber; i++){
        if (Data[i]->GetAge(1) == Y1 &&  Data[i]->GetAge(2) == Y2 &&  Data[i]->GetAge(3) == Y3){
         cout << "\nEmployee's Full name: " << Data[i]->GetName(1) << " " << Data[i]->GetName(2) << " " << Data[i]->GetName(3) << endl;
         cout << "Employee's Date of birth: " << Data[i]->GetAge(3) << "/" << Data[i]->GetAge(2) << "/" << Data[i]->GetAge(1) << endl;
         cout << "Employee's Adress 1: " << Data[i]->GetAdress(1) << endl << "Employee's Adress 2: " << Data[i]->GetAdress(2) << endl << "Employee's E-mail adress" << Data[i]->GetAdress(3) << endl;
         cout << "Employee's phone number: " << Data[i]->CantactNum(1) << endl << "Employee's Mobile number: " << Data[i]->CantactNum(2) << endl;
        }
     }
  }
  return 0;
}
void Sorting(int EmployeesNumber){
  while (1){
     cout << "Choose something you want to do: \n" << "1. Search by name\n2. Search by age\n3. Search by adress\n4. Exit";
     int k;
     cin >> k;
     switch (k){

     case 1:
        NameSearching(EmployeesNumber); break;
     case 2:
        AgeSearch(EmployeesNumber); break;
     case 4: return;
     }
  }
}
int main(){

  int EmployeesNumber;
  cout << "How many employees do you have ?\n";
  cin >> EmployeesNumber;

  Data = new Employee*[EmployeesNumber];
  for (int i = 0; i < EmployeesNumber; i++){
     Data[i] = new Employee;
  }

  name(EmployeesNumber);
  Age(EmployeesNumber);
  Adress(EmployeesNumber);
  Sorting(EmployeesNumber);
  system("pause");
  return 0;
}

Answer
You do not have to code it like that.  You can use cin like you correctly used cout.  What you used compiles, but it is used in special circumstances.  It does not seem to be working for you, so I recommend just using cin as follows :

//string FN;
char FN[20];
cin >> FN ;
//cin.getline(FN,20);
cout << "\nEnter the second name for the employee\n";
string SN;
//getline(cin, SN);
cin >> SN ;
cout << "\nEnter the last name for the employee\n";
string LN;
//getline(cin, LN);
cin >> LN ;

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Titus B. Ledbetter, Jr.

Expertise

C , MFC, Object Oriented, Artificial Intelligence

Experience

I have over ten years experience in the field of Computer Science, five years experience developing C/C++. I recently wrote a chess program using Object Oriented, C++, MFC.

Education/Credentials
Master's Degree Computer Science from Johns Hopkins

©2016 About.com. All rights reserved.