Why code skips a step

I have an assignment i have wrote a code bellow but it skips qualification and address lines.why its happening.also i want that code automatic insert "/" after day and month(dd/mm/yyy) when user enter age joining date etc.
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
#include <iostream>
using namespace std;
class staffdata
{
private:
char cnic[16];
char name[20];
char fname[20];
char dob[11];
char study[10];
char desig[15];
int salary;
char joining[11];
char contact[13];
char address[40];
public:
void set_data()
{
cout<<"\nPlease enter National Identity Card Number(CNIC)"<<endl;
gets(cnic);
cout<<"Please enter name"<<endl;
gets(name);
cout<<"Please enter father name"<<endl;
gets(fname);
cout<<"Please enter date of birth (dd/mm/yyyy)"<<endl;
cin>>dob;
cout<<"Please enter qualification"<<endl;
gets(study);
cout<<"Please enter Designation"<<endl;
gets(desig);
cout<<"Please enter salary"<<endl;
cin >> salary;
cout<<"Please enter joining date (dd/mm/yyyy)"<<endl;
cin >> joining;
cout<< "Please enter contact number"<<endl;
cin >> contact;
cout<<"Please enter address information"<<endl;
gets(address);
cout<<"\nRecord added successfully !!!"<<endl;
}
void show_data()
{
cout<<"CNIC: \t\t\t"<<cnic<<endl;
cout<<"Name: \t\t\t"<<name<<endl;
cout<<"Father Name: \t\t"<<fname<<endl;
cout<<"DoB: \t\t\t"<<dob<< endl;
cout<<"Qualification: \t\t"<<study<<endl;
cout<<"Designation: \t\t"<<desig<<endl;
cout<<"Salary: \t\t"<<salary<<endl;
cout<<"Joining Date: \t\t"<<joining<<endl;
cout<<"Contact Number: \t"<<contact<<endl;
cout<<"Address: \t\t"<<address<<endl;
}
};
main()
{
	
system("cls");

staffdata g1;
g1.set_data();
cout<<"---------------------------------------------------------------------------"<<endl;
cout<<"\t\t\t\ DISPALAY STAFF INFORMATION"<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
g1.show_data();
cout<<"---------------------------------------------------------------------------"<<endl;
system("pause");
}
Last edited on
Hi,

Avoid using deprecated c-style functions like gets.

Use std::string rather than char arrays, and use std::getline to get user input.

You can read about these in the tutorial at the top left of this page.

Good Luck!!
Thanks you i used getline function and strings but again it skips skips joining date i figured it out that when salary variable is int type program skip joining date when salary type string then joining date execute.. i want to know why it skips when variable type is int...

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
#include <iostream>
#include <string>
using namespace std;
class staffdata
{
private:
string cnic;
string name;
string fname;
string dob;
string study;
string desig;
string salary;
string joining;
string contact;
string address;
public:
void set_data()
{
	

cout<<"\n\nPlease enter National Identity Card Number(CNIC)"<<endl;
getline(cin,cnic);
cout<<"Please enter name"<<endl;
getline(cin,name);
cout<<"Please enter father name"<<endl;
getline(cin,fname);
cout<<"Please enter date of birth (dd/mm/yyyy)"<<endl;
getline(cin,dob);
cout<<"Please enter qualification"<<endl;
getline(cin,study);
cout<<"Please enter Designation"<<endl;
getline(cin,desig);
cout<<"Please enter salary"<<endl;
getline(cin,salary);
cout<<"Please enter joining date (dd/mm/yyyy)"<<endl;
getline(cin,joining);
cout<< "Please enter contact number"<<endl;
getline(cin,contact);
cout<<"Please enter address information"<<endl;
getline(cin,address);
cout<<"\nRecord added successfully !!!"<<endl;
}
void show_data()
{
cout<<"CNIC: \t\t\t"<<cnic<<endl;
cout<<"Name: \t\t\t"<<name<<endl;
cout<<"Father Name: \t\t"<<fname<<endl;
cout<<"DoB: \t\t\t"<<dob<< endl;
cout<<"Qualification: \t\t"<<study<<endl;
cout<<"Designation: \t\t"<<desig<<endl;
cout<<"Salary: \t\t"<<salary<<endl;
cout<<"Joining Date: \t\t"<<joining<<endl;
cout<<"Contact Number: \t"<<contact<<endl;
cout<<"Address: \t\t"<<address<<endl;
}
};
main()
{
	
system("cls");

staffdata g1;
g1.set_data();
cout<<"---------------------------------------------------------------------------"<<endl;
cout<<"\t\t\t\ DISPALAY STAFF INFORMATION"<<endl;
cout<<"---------------------------------------------------------------------------"<<endl;
g1.show_data();
cout<<"---------------------------------------------------------------------------"<<endl;
system("pause");
}
It skips when you use >> to read into the variable because that leaves the enter you press to input the data in the input stream. Then, on the next line, when you try to read a string, it reads that enter press as you having entered the empty string.
Topic archived. No new replies allowed.