Program skipping first line of input when displaying final output?

Hi all!

So I have put together my code for an assignment for a C++ class that I am in when I came into a little of a hurdle. So my code takes the input perfectly first time around (name, address, location, phone number) but when it comes to input it again for the required second and third time, it skips the name input. What is wrong with my code? I don't see any ignore function I randomly placed...

UPDATE: I have fixed my code, but now the final output is now wrong as it doesn't even list the names with the rest of their information.
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  #ifndef PERSONALDATA_H
#define PERSONALDATA_H

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

class PERSONALDATA{
private:
    string name;
    string address;
    int age;
    string PhoneNumber;

public:
    //constructor with default values
    PERSONALDATA(string na = "", string loc = "",
                 int a = 0, string pn = "000-000-0000"){
        name = na;
        address = loc;
        age = a;
        PhoneNumber = pn;
    }

    //getter functions
    string getName() const{
        return name;
    }

    string getAddress() const{
        return address;
    }

    int getAge() const{
        return age;
    }

    string getPhoneNumber() const{
        return PhoneNumber;
    }

    //setter functions
    void setName(string na){
        //check for invalid input
        if(na.size() == 0){
            cout << "Empty names not allowed!\n";
            return;
        }
        else{
            name = na;
        }
    }

    void setAddress(string loc){
        //check for invalid input
        if(loc.size() == 0){
            cout << "Empty addresses not allowed!\n";
            return;
        }
        else{
            address = loc;
        }
    }

    void setAge(int a){
        //check for invalid input
        if(a < 0){
            cout << "Age cannot be negative!\n";
            return;
        }
        else{
            age = a;
        }
    }

    void setPhoneNumber(string p){
        //check for invalid input
        //phone format is
        //XXX-XXX-XXXX
        if(!isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2])
           || p[3] != '-' || !isdigit(p[4]) || !isdigit(p[5])
           || !isdigit(p[6]) || p[7] != '-' || !isdigit(p[8])
           || !isdigit(p[9]) || !isdigit(p[10]) || !isdigit(p[11]))
        {
            cout << "Invalid phone format!\n";
            cout << "It should be XXX-XXX-XXXX\n";
            return;
        }
        else{
            PhoneNumber = p;
        }
    }
    void Ask()
        {
            cout << "Enter name: ";
            getline (cin, name);
            cout << endl;

            cout << "Enter address: ";
            getline (cin, address);
            cout << endl;

            cout << "Enter age: ";
            cin >>age;
            cout << endl;

            cout << "Enter phonenumber: ";
            cin >> PhoneNumber;
            cout << endl;
};
};
//contents of PDQL10.cpp
#endif
    
#include <iostream>
#include <string>
using namespace std;

int main()
{
    PERSONALDATA you;
    PERSONALDATA your_friend;
    PERSONALDATA your_family_member;

            cout << "Enter your information" << endl;
            you.Ask();
            cout << "Enter your friends' information" << endl;
            your_friend.Ask();
            cout << "Enter your family members' information" << endl;
            your_family_member.Ask();
            
    cout << "Creating first object with default constructor...\n";
        PERSONALDATA obj1(you);
        cout << "Done!\n";

        cout << "\nNow displaying its data\n";
        cout << "Name: " << obj1.getName();
        cout << "\nAddress: " << obj1.getAddress();
        cout << "\nAge: " << obj1.getAge();
        cout << "\nPhone Number: " << obj1.getPhoneNumber();



        cout << "\nNow displaying its data again\n";
        cout << "Name: " << obj1.getName();
        cout << "\nAddress: " << obj1.getAddress();
        cout << "\nAge: " << obj1.getAge();
        cout << "\nPhone Number: " << obj1.getPhoneNumber();

        cout << "\n\nNow creating another object, calling the ";
        cout << "constructor with our values...\n";
        PERSONALDATA obj2(your_friend);
        cout << "Done!\n";

        cout << "\nNow displaying its data\n";
        cout << "Name: " << obj2.getName();
        cout << "\nAddress: " << obj2.getAddress();
        cout << "\nAge: " << obj2.getAge();
        cout << "\nPhone Number: " << obj2.getPhoneNumber();

        cout << "\n\nNow creating another object, calling the ";
        cout << "constructor only with a name and address argument...\n";
        PERSONALDATA obj3(your_family_member);
        cout << "Done!\n";

        cout << "\nNow displaying its data\n";
        cout << "Name: " << obj3.getName();
        cout << "\nAddress: " << obj3.getAddress();
        cout << "\nAge: " << obj3.getAge();
        cout << "\nPhone Number: " << obj3.getPhoneNumber();
//DISPLAY ALL THREE ENTERED POINTS TO USER
    // SHOW DATA
        cout << "\nNow displaying all 3 data points again\n";
        cout << "Name: " << obj1.getName();
        cout << "\nAddress: " << obj1.getAddress();
        cout << "\nAge: " << obj1.getAge();
        cout << "\nPhone Number: " << obj1.getPhoneNumber();
        cout << "\n";

        cout << "Name: " << obj2.getName();
        cout << "\nAddress: " << obj2.getAddress();
        cout << "\nAge: " << obj2.getAge();
        cout << "\nPhone Number: " << obj2.getPhoneNumber();
        cout << "\n";
    
        cout << "Name: " << obj3.getName();
        cout << "\nAddress: " << obj3.getAddress();
        cout << "\nAge: " << obj3.getAge();
        cout << "\nPhone Number: " << obj3.getPhoneNumber();
        cout << "\n";
    return 0;
};

Last edited on
This is a very common problem that I have answered many times so I'm just going to give you a link to one of my previous answers.

https://www.cplusplus.com/forum/beginner/266289/#msg1146100
@Peter87 Hey Peter, just followed for your solution and it did fix my problem. However, when the system itself displays all three data lists the name field is blank. Any know how on a fix?
Last edited on
You probably did some mistake. I can't say more without seeing the code.
Last edited on
As 1 file:

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <iostream>
#include <string>
using namespace std;

class PERSONALDATA {
private:
	string name;
	string address;
	int age {};
	string PhoneNumber {};

public:
	//constructor with default values
	PERSONALDATA(const string& na = "", const string& loc = "", int a = 0, const string& pn = "000-000-0000") :name(na), address(loc), age(a), PhoneNumber(pn) {}

	//getter functions
	string getName() const { return name; }
	string getAddress() const { return address; }
	int getAge() const { return age; }
	string getPhoneNumber() const { return PhoneNumber; }

	//setter functions
	void setName(const string& na) {
		//check for invalid input
		if (na.empty())
			cout << "Empty names not allowed!\n";
		else
			name = na;
	}

	void setAddress(const string& loc) {
		//check for invalid input
		if (loc.empty())
			cout << "Empty addresses not allowed!\n";
		else
			address = loc;
	}

	void setAge(int a) {
		//check for invalid input
		if (a < 0)
			cout << "Age cannot be negative!\n";
		else
			age = a;
	}

	void setPhoneNumber(const string& p) {
		//check for invalid input
		//phone format is
		//XXX-XXX-XXXX

		if (p.size() != 12 || !isdigit(p[0]) || !isdigit(p[1]) || !isdigit(p[2])
			|| p[3] != '-' || !isdigit(p[4]) || !isdigit(p[5])
			|| !isdigit(p[6]) || p[7] != '-' || !isdigit(p[8])
			|| !isdigit(p[9]) || !isdigit(p[10]) || !isdigit(p[11]))
		{
			cout << "Invalid phone format!\n";
			cout << "It should be XXX-XXX-XXXX\n";
		} else
			PhoneNumber = p;
	}

	void Ask()
	{
		string str;
		int inpage;

		cout << "Enter name: ";
		getline(cin, str);
		setName(str);
		cout << endl;

		cout << "Enter address: ";
		getline(cin, str);
		setAddress(str);
		cout << endl;

		cout << "Enter age: ";
		cin >> inpage;
		setAge(inpage);
		cout << endl;

		cout << "Enter phonenumber: ";
		cin >> str;
		setPhoneNumber(str);
		cout << endl;

		cin.ignore(1000, '\n');
	}
};

int main()
{
	PERSONALDATA you;
	PERSONALDATA your_friend;
	PERSONALDATA your_family_member;

	cout << "Enter your information" << endl;
	you.Ask();

	cout << "Enter your friends' information" << endl;
	your_friend.Ask();

	cout << "Enter your family members' information" << endl;
	your_family_member.Ask();

	cout << "Creating first object with default constructor...\n";

	PERSONALDATA obj1(you);

	cout << "Done!\n";
	cout << "\nNow displaying its data\n";
	cout << "Name: " << obj1.getName();
	cout << "\nAddress: " << obj1.getAddress();
	cout << "\nAge: " << obj1.getAge();
	cout << "\nPhone Number: " << obj1.getPhoneNumber();

	cout << "\nNow displaying its data again\n";
	cout << "Name: " << obj1.getName();
	cout << "\nAddress: " << obj1.getAddress();
	cout << "\nAge: " << obj1.getAge();
	cout << "\nPhone Number: " << obj1.getPhoneNumber();

	cout << "\n\nNow creating another object, calling the ";
	cout << "constructor with our values...\n";

	PERSONALDATA obj2(your_friend);

	cout << "Done!\n";
	cout << "\nNow displaying its data\n";
	cout << "Name: " << obj2.getName();
	cout << "\nAddress: " << obj2.getAddress();
	cout << "\nAge: " << obj2.getAge();
	cout << "\nPhone Number: " << obj2.getPhoneNumber();

	cout << "\n\nNow creating another object, calling the ";
	cout << "constructor only with a name and address argument...\n";

	PERSONALDATA obj3(your_family_member);

	cout << "Done!\n";
	cout << "\nNow displaying its data\n";
	cout << "Name: " << obj3.getName();
	cout << "\nAddress: " << obj3.getAddress();
	cout << "\nAge: " << obj3.getAge();
	cout << "\nPhone Number: " << obj3.getPhoneNumber();

	//DISPLAY ALL THREE ENTERED POINTS TO USER
	// SHOW DATA

	cout << "\nNow displaying all 3 data points again\n";
	cout << "Name: " << obj1.getName();
	cout << "\nAddress: " << obj1.getAddress();
	cout << "\nAge: " << obj1.getAge();
	cout << "\nPhone Number: " << obj1.getPhoneNumber();
	cout << "\n";

	cout << "Name: " << obj2.getName();
	cout << "\nAddress: " << obj2.getAddress();
	cout << "\nAge: " << obj2.getAge();
	cout << "\nPhone Number: " << obj2.getPhoneNumber();
	cout << "\n";

	cout << "Name: " << obj3.getName();
	cout << "\nAddress: " << obj3.getAddress();
	cout << "\nAge: " << obj3.getAge();
	cout << "\nPhone Number: " << obj3.getPhoneNumber();
	cout << "\n";
	return 0;
}


You need to think about you're going to do if an input is incorrect. At the moment it sinply reports the issue and resumes.

@seeplus It was already one "file" so what's the difference?
For easy compile, I moved the .h contents into the .cpp file.

The main change is in Ask() - but some other minor changes elsewhere (eg constructor, using .empty() rather than .size() == 0, checking size of p in setPhoneNumber(), using const string& rather than string as function parameter...)

For easy compile, I moved the .h contents into the .cpp file

OK, but it already compiled. ;)
Topic archived. No new replies allowed.