jeudi 30 juillet 2015

I am working on a program that prints a company information (e.g. company name, name of CEO etc.). I am using struct to group all the related variables. My struct looks like:

 struct Company
 {
      std::string NameOfCompany;     
      std::string NameOfCeo;
      int Revenue;
      int NumOfEmployees;
      std::string BranchesIn;
 };

I defined a void printInfo (Company x) to print information of a company. My main () takes data from user and passes that to printInfo(). Rest of the thing is done by printInfo (). My problem is with main () that is following:

   int main ()
  {
     using namespace std;
     Company x;
     cout << "Name of company: ";
     getline(cin, x.NameOfCompany);
     cout << "Name of CEO: ";
     getline(cin, x.NameOfCeo);
     cout << "Total Revenue: ";
     cin >> x.Revenue;
    cout << "Number of employees: ";
     cin >> x.NumOfEmployees;
     cout << "Branches In: ";
     getline(cin, x.BranchesIn);
     printInfo (x);
     return 0;
  }

The above code compiles and links fine (not including the void printInfo (), nothing wrong with that). On execution, the program starts from taking input for x.NameOfCompany. That's okay. But all the other getline () puzzled me. Program never asks for input for one (or both when i change the order of getlines, e.g. BranchesIn before NameOfCeo) variables which is set to take input through getline method. What's the problem. Does getline has any limitations?

If the code has any syntax or typographical error, please ignore. I swear that was linked and compiled fine.

Aucun commentaire:

Enregistrer un commentaire