Problem with getline (special)

I got some problem with getline command.
I try to search online, and i get to know should add a "cin.ignore()" before next getline if using cin and getline together.

but i still get some error after i improved.
Can someone help me to solve?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include<iostream>
#include<cstring>
#include<string>
#include<fstream>
#include"employee.h"
#include"manager.h"

using namespace std;

int main()
{
    // Declaration of variable
    string name, address, town, date, phone;
    int id, postcode, worker, edit;
    double hour, rate, gross, net;
    int bypass = 1;
    char *post;
    int employeeCount = 0;
    int managerCount = 0;
    int totalCount = 0;
    int another_employee = 0;
    char* strEmployee = "Employee";
    char* strManager = "Manager";


    Employee employee[100]; // Declaration of class array
    Manager manager[100];
        
    employee[0].resetData(); // Reset counter
    
    if (bypass == 1)
    {
    cout << "Welcome to you Employee Payslip Generator:" << endl;
    
    do 
    {
       cout << "Please enter your employee's name     : ";
       getline (cin,name);
       cout << "Employee's staff ID no.               : ";
       cin  >> id;
       cout << "Employee's Position                   : ";
       cin >> post;
       cout << "Employee's Worker                     : ";
       cin >> worker;
       cin.ignore();
       cout << "Employee's address                    : ";
       getline (cin,address);
       cout << "Town                                  : ";
       getline (cin,town);
       cout << "Postcode                              : ";
       cin  >> postcode;
       cin.ignore();
       cout << "Employee's contact no.                : ";
       getline (cin,phone);
       cout << "Payslip Date(month year)              : ";
       getline (cin,date);
       cout << "Number of hours worked this week(hour): ";
       cin  >> hour;
       cout << "Employee's Payrate(per hour)          : RM ";
       cin  >> rate;
       cout << endl;
       
       
       if (strcmp(strEmployee,post) == 0)
       {
        employee[employeeCount-1].employeeDetail(name, id, post, address, town, postcode, phone, date, hour, rate);
        employee[employeeCount-1].printEmployeeData();
        employee[0].setEmployeeTotalGrossPay(employee[employeeCount-1].getEmployeeGrossPay());
        employee[0].setEmployeeTotalNetPay(employee[employeeCount-1].getEmployeeNetPay());
        ++employeeCount;
       }
       
       else if (strcmp(strManager,post) == 0)
       {
        manager[employeeCount-1].managerDetail(name, id, post, address, town, postcode, phone, date, hour, rate, worker);
        manager[employeeCount-1].printEmployeeData();
        employee[0].setEmployeeTotalGrossPay(manager[employeeCount-1].getEmployeeGrossPay());
        employee[0].setEmployeeTotalNetPay(manager[employeeCount-1].getEmployeeNetPay());
        ++managerCount;
        }
        
       
       cout << "Do you want to process another employee?" << endl;
       cout << "Enter 1 for yes and 0 for no: ";
       cin >> another_employee;
       cin.ignore();
       cout << endl;
       cout << "_______________________________________________" << endl;
       
    }while(another_employee==1);
    
    totalCount = employeeCount + managerCount;
    employee[0].setTotalCount(totalCount);
    }
    
    else
    cout << "Wrong Access." << endl;
    
    do{
        cout << "Do you want to edit data?" << endl;
        cout << "Enter 1 for yes and 0 for no: ";
        cin >> edit;
        
        if (edit == 1)
        {
           int category;
           
           cout << "Please choose the category to access." << endl;
           cout << "1. Manager" << endl;
           cout << "2. Employee" << endl;
           cin >> category;
           
           if (category == 1)
              manager[0].editData(manager);
           
           else if (category == 2)   
              employee[0].editData(employee);
           
           else if (category > 2 || category < 1)
              cout << "Invalid category." << endl;
              
        }
        
        else
            employee[0].printCompanyData();
            
    }while(edit == 1);
   
    manager[0].exportData(manager,managerCount);
    employee[0].exportData(employee,employeeCount);
    
    
    system("pause");
    return 0;
}


Error appear after i input my "worker" value, it skipped all input command next to it.
Did you enter the Employee's Worker value as an integer?
Try replacing cin.ignore() with cin.sync().
You mean i should change cin.ignore() to cin.sync()?
I make some change but the result still the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
do 
    {
       cout << "Please enter your employee's name     : ";
       getline (cin,name);
       cout << "Employee's staff ID no.               : ";
       cin  >> id;
       cout << "Employee's Position                   : ";
       cin >> post;
       cout << "Employee's Worker                     : ";
       cin >> worker;
       cin.sync();
       cout << "Employee's address                    : ";
       getline (cin,address);
       cout << "Town                                  : ";
       getline (cin,town);
       cout << "Postcode                              : ";
       cin  >> postcode;
       cin.sync();
       cout << "Employee's contact no.                : ";
       getline (cin,phone);
       cout << "Payslip Date(month year)              : ";
       getline (cin,date);
       cout << "Number of hours worked this week(hour): ";
       cin  >> hour;
       cout << "Employee's Payrate(per hour)          : RM ";
       cin  >> rate;
       cout << endl;


[IMG]http://i39.tinypic.com/34grqs2.jpg[/IMG]
Yes. I do enter the value as an integer.
1
2
3
4
5
6
     char *post;
     
     // ...

     cout << "Employee's Position                   : ";
      cin >> post;


See anything wrong with this?

Don't use cin.sync(). It has different behaviour in different compilers.
Last edited on
Sorry, i really not understand what wrong with that.
Can you explain further, thanks.
cin.sync() isn't guaranteed to do anything useful.

The underlying principle is that the user will always press ENTER after every input.

Hence, you should locate the end of inputs and extract the ENTER (newline) from it. The end of an input will typically correspond to everything that is associated with a specific prompt.

The std::getline() function extracts and discards that newline for you.

Otherwise, you must do it yourself, with one of:

    - std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
      
To get rid of everything left over on the line, including the newline

    - std::cin.ignore();
      
To get rid of a single character, which you hope is a newline

You can also use my endl extractor, which I can't seem to find right now. Give me a bit and I'll dig it up for you. (I've got to go out to the store ATM.)

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <limits>
#include <string>
using namespace std;

struct point { double x, y; }

int main()
{
  point p;
  string s;

  cout << "Enter a point (as a pair of numbers): ";  // prompt
  cin >> p.x >> p.y;  // input
  cin.ignore( numeric_limits <streamsize> ::max(), '\n' );  // end of input

  cout << "What is your favorite color? ";  // prompt
  getline( cin, s );  // input, end of input

  return 0;
}

Hope this helps.
The problem is actually with the declaration of "post".
It happen when the code is:

1
2
3
4
5
6
     char *post;
     
     // ...

     cout << "Employee's Position                   : ";
      cin >> post;


After i change the declaration, it work well.
1
2
3
4
    string post;

    cout << "Employee's Position                   : ";
    getline (cin,post);


Anyway, thanks for Duos, Cire, Peter and others trying to help me.
Appreciate it.
Ah, I missed that. If you want to know more about c-strings and why that didn't work, see the FAQ here:
http://cplusplus.com/faq/sequences/strings/c-strings-and-pointers/
Topic archived. No new replies allowed.