Skipping other input

I am supposed to write a program that holds holds the following personal data: name, address, age, and phone number. Write appropriate accessor and mutator functions. Then demonstrate the class by writing a program that creates three instances of it. One instance should hold your information,and the other two should hold your friends' or family members' information.

The problem is I am stuck in the int main(). I do not know what to put to be able to ask the user the person's information and then display it. Then ask the user again for the other two information. My apologies if this a very amateur question. I am not a very good programmer. Thank you for the help.


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


//This class will hold personal information and demonstrate instances of it.

#include <iostream>
#include <string>

using namespace std;

class PersonalInfo

{
private:
	string name;
	string address;
	int age;
	int phonenumber;

public:
	PersonalInfo (string, string, int, int);
	void setName(string newName);
	void setAddress(string newAddress);
	void setAge(int newAge);
	void setPhonenumber(int newPhonenumber);

	string getName();
	string getAddress();
	int getAge();
    int getPhonenumber();

};

PersonalInfo::PersonalInfo(string nameofperson, string resident, int years, int number)
{
	name = nameofperson;
	address = resident;
	age = years;
	phonenumber = number;
}

	void PersonalInfo::setName(string newName)
	{
		name = newName;
	}

	void PersonalInfo::setAddress(string newAddress)
	{
		address = newAddress;
	}

	void PersonalInfo::setAge(int newAge)
	{
		age = newAge;
	}

	void PersonalInfo::setPhonenumber(int newPhonenumber)
	{
		phonenumber = newPhonenumber;
	}

	string PersonalInfo::getName()
	{
		return name;
	}

	string PersonalInfo::getAddress()
	{
		return address;
	}

	int PersonalInfo::getAge()
	{
		return age;
	}
	
	int PersonalInfo::getPhonenumber()
	{
		return phonenumber;
	}

	int main()
	{
		string person_name;
		string person_address;
		int person_age;
		int person_phone;

		cout << "Enter name: ";
		cin >> person_name;
		cout << "Enter address: ";
		cin >> person_address;
		cout << "Enter age: ";
		cin >> person_age;
		cout << "Enter phone number: ";
		cin >> person_phone;

		return 0;

	}
		

Last edited on
Make the asking for the info a part of your class:
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
class PersonalInfo

{
private:
...

public:
...
	void Ask();
...
};

...
	void PersonalInfo::Ask()
	{
		cout << "Enter name: ";
		cin >> name;
		cout << "Enter address: ";
		cin >> address;
		cout << "Enter age: ";
		cin >> age;
		cout << "Enter phone number: ";
		cin >> phonenumber;
	}
...


You could simply writ in your main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	int main()
	{
		PersonalInfo you;
		PersonalInfo your_friend;
		PersonalInfo 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();

		return 0;

	}
Thank you for the great help. The program runs properly but when I press space, such as "James Smith", the program skips a question based on how many spaced i entered on the previous question. Also, when I input more than 7 digits for the phone number, it skips the all the other inputs.
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

//This class will hold personal information and demonstrate instances of it.

#include <iostream>
#include <string>

using namespace std;

class PersonalInfo

{
private:
	string name;
	string address;
	int age;
	int phonenumber;

public:
	void setName(string newName);
	void setAddress(string newAddress);
	void setAge(int newAge);
	void setPhonenumber(int newPhonenumber);
	string getName();
	string getAddress();
	int getAge();
    int getPhonenumber();
	void Ask();

};


	void PersonalInfo::setName(string newName)
	{
		name = newName;
	}

	void PersonalInfo::setAddress(string newAddress)
	{
		address = newAddress;
	}

	void PersonalInfo::setAge(int newAge)
	{
		age = newAge;
	}

	void PersonalInfo::setPhonenumber(int newPhonenumber)
	{
		phonenumber = newPhonenumber;
	}

	string PersonalInfo::getName()
	{
		return name;
	}

	string PersonalInfo::getAddress()
	{
		return address;
	}

	int PersonalInfo::getAge()
	{
		return age;
	}
	
	int PersonalInfo::getPhonenumber()
	{
		return phonenumber;
	}

	void PersonalInfo::Ask()
	{
		cout << "Enter name: ";
		cin >> name;
		cout << endl;

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

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

		cout << "Enter phone number: ";
		cin >> phonenumber;
		cout << endl;
	}


	int main()
	{
		PersonalInfo you;
		PersonalInfo your_friend;
		PersonalInfo 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();

		return 0;

	}
		
Last edited on
Ok, I solved the skipping problem by changing cin >> name; and cin >> address; to getline (cin, name); and getline (cin, address); but the phone number is still experiencing some problems as stated above.
This is a common problem in simple programs - how to represent data. Everything in the world is not either a 'string', 'int', 'double', etc. Phone number, social security, even a generic 'ID' are good examples. Is your phone number an integer? If I told you my phone number is 123-456-7890, you could parse out the dashes, and eventually store it in an int. What if it's not a US number tho: 12-345-678-9012? Can still strip out the dashes, but now the number is likely too large to store in an int (which has a fixed size). So you may think you need a 'BigInt' object - then consider how to store a number like 011-1234-56789. Might fit in a BigInt, but if you view this as an integer, you are going to strip off the '0' at the front! The solution is either to have a dedicated 'phone number' class, or more simply just store it as a string. I prefer by far to reserve int for numbers that represent counts (number of miles, speeds, etc)


Last edited on
I have put the phonenumber as a string but the result is still the same. Even if I only enter 1 digit it skips the next input. I even tried using getline (cin, phonenumber) but this just totally skips the phone number input.

so far my program is this:

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

//This class will hold personal information and demonstrate instances of it.

#include <iostream>
#include <string>

using namespace std;

class PersonalInfo

{
private:
	string name;
	string address;
	int age;
	string phonenumber;

public:
	void setName(string newName);
	void setAddress(string newAddress);
	void setAge(int newAge);
	void setPhonenumber(int newPhonenumber);
	string getName();
	string getAddress();
	int getAge();
    string getPhonenumber();
	void Ask();

};


	void PersonalInfo::setName(string newName)
	{
		name = newName;
	}

	void PersonalInfo::setAddress(string newAddress)
	{
		address = newAddress;
	}

	void PersonalInfo::setAge(int newAge)
	{
		age = newAge;
	}

	void PersonalInfo::setPhonenumber(int newPhonenumber)
	{
		phonenumber = newPhonenumber;
	}

	string PersonalInfo::getName()
	{
		return name;
	}

	string PersonalInfo::getAddress()
	{
		return address;
	}

	int PersonalInfo::getAge()
	{
		return age;
	}
	
	string PersonalInfo::getPhonenumber()
	{
		return phonenumber;
	}

	void PersonalInfo::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;
	}


	int main()
	{
		PersonalInfo you;
		PersonalInfo your_friend;
		PersonalInfo 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();

		return 0;

	}
		
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <limits>

        void PersonalInfo::Ask()
	{
		cout << "Enter name: ";
		getline (cin, name);
		cout << endl;

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

		cout << "Enter age: ";
		cin >>age;
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		cout << endl;

		cout << "Enter phonenumber: ";
		cin >> phonenumber;
		cout << endl;
	}


The same as:
http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.6
although the problem illustrated is in a slightly different format.
Thank you very much cire. I would have never known how to solve my problem without your help. My program completely works now. Thank you again.
Topic archived. No new replies allowed.