Failing to get input

Good morning,
Yes the following question is regarding my homework but, I have written the code, I am not asking for someone to write it for me. I am supposed to implement the member functions of class Person.
1
2
3
4
5
6
7
8
9
10
11
class Person
{
public:
   Person();
   Person(string pname, int page);
   void get_name() const;
   void get_age() const;
private:
   string name;
   int age; // 0 if unknown
};


The code I wrote is below. Where I am struggling is the program does not allow me to input age. Therefore, I cannot test if my temp for age works. It automatically defaults to 0 because it hasn't taken input. Thanks for your help. Here is my code;
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
//    Author:                                                                                  

//    Assignment:  Lab 10 - P5.1                                                                                

//    Program Title:  Person function

//    Program Description: The program prompts the user for first and last name and age.

//    It then prints the output that was provided by the user.

//    Compiler Used: Microsoft Visual C++

//    Completion Date:  August 10, 2013

    #include<iostream>
    #include<string>
    using namespace std;
	class Person
		{
		private:
			string Name; // String of name and last name;
			int Age; // 0 if unknown
		public:
		Person::Person(): Name(" "), Age (0) // Constructor to initialize default values.
		{
		}
		string getName()
		{
			return Name; // return pointer to the name;
		}
		void setName(string n)
		{
			Name=n; //set name to the name passed in parameter.
		}

		int getAge()
		{
			return Age; // return age from object.
		}
		void setAge(int a)
		{
			if (a>=1) // check if age is 0 or not.
			Age=a;
			else
			Age=0;
		}
    };
    Person p; // create an object for class Person
    string gets(); // Function DEFINED to return a new string entered by the user
    void print(); // Function to print the details of the Person.

	int main() // Main starts here
    {
		int temp; // temp variable to hold age of Person
		// Persons details entered by user
		cout <<"\n Enter your first and last name:: ";
		p.setName(gets());
		cout <<"\n Enter your age:: ";
		cin >>temp;
		p.setAge(temp);
		
		// OUTPUT STARTS
		cout <<"\n\n\n \t\t\t:: Your Name and Age ::";
		print(); // call function to print the details of the Person
		return 0; // finish main()
    }

		string gets(){
		string n;
		cin>>n;
		return n;
    }

    void print(){
    cout <<"\n Name: "<< p.getName();
    cout <<"\n Age: "<< p.getAge() << "\n\n";
    }

	
Last edited on
cout <<"\n Enter your first and last name:: ";

This expects the user to enter two words, ex. John Doe.

Then you have
p.setName(gets());

gets() uses
cin >> n

which will get what the user inputs up to whitespace. Space, tab, newline, etc.
You need to change it to something like:
1
2
cout << "\nEnter your first name:: ";
p.setAge(gets() + gets());


Which will get the name twice, resulting in First name + last name
Note: If the user enters something besides and int at line 59, the program will crash. This is not a big deal, but something to look into if you want to fix it.
Thank you that helped me to get the first and last names and prompt for age. Now, what I am missing is the ability to output "0 if unknown". If I hit enter, the program doesn't exit with 0 for the age. I think my loop is right but, I may be missing a terminator value. Is this correct?
Can you tell me exactly what your program is outputing based on what you input?

EDIT:

I think this will fix your problem.
note: this is a little extra code, but oh well :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cout << "\nEnter your age: ";
string t;
getline(cin, t, '\n');
if(t == "")
        temp = 0;

else
{
       stringstream h(t);
	if(!h >> temp)
	{
		temp = 0;
		h.clear();
	}
        //temp now contains either 0 or a int that the user inputed
}
Last edited on
If I put in an age, I get the name output as well as my age. (I did notice the names are backwards too). If I don't put in an age, the program just keeps upping a line until I put in some information. It is supposed to allow an empty value and return 0 or if the age is 1 or greater, return the age.
(I did notice the names are backwards too)

Does this mean the last name is before the first name or what?

It is supposed to allow an empty value and return 0 or if the age is 1 or greater, return the age.

The code I posted should do that.
Yes, if I type in firstname, last name it prints lastname, firstname
Do you want it the other way? If so,
1
2
3
4
5
6
cout << "\nEnter your first and last name:: ";
string a, b;
a = gets();
b = gets();

p.setName(a + b); // or if that is still switched, (b + a) 
Superdude, that got it. thanks so much.
Anytime ;)
@SuperDude:
Can you please explain how this works:
1
2
3
4
5
6
7
8
9
10
else
{
       stringstream h(t);
	if(!h >> temp)
	{
		temp = 0;
		h.clear();
	}
        //temp now contains either 0 or a int that the user inputed
}


I am having difficulty understanding how this
if(!h >> temp)
works for non-zero values
h is like cin. A stringstream. If you dont know what that is, look it up.
1
2
3
4
5
6
7
if(extracting the next unit of text in to temp fails) // exampe if the value //was non-numerical
{
        set temp to 0;
        clear the error state of h;
}

//temp now contains 0(if extraction failed) or the intput number 


Does that help?
@SuperDude: Thanks for your explanation.
I tried running this code with suggested changes and found that it did not work for me.
1
2
3
4
5
6
7
	else{
		stringstream stream(t);
		if(!stream >> temp){
			temp =0;
			stream.clear();
		}
	}


 Enter your first and last name:: AA BB

 Enter your age:: 15



                        :: Your Name and Age ::
 Name: AA BB
 Age: 7729096

Press any key to continue . . .


After a brief search I found this link online-http://stackoverflow.com/questions/11592597/stringstream-from-double-to-char-no-failed-no-exception

I made this change to code and it works fine.
1
2
3
4
5
6
7
	else{
		stringstream stream(t);
		if(!(stream >> temp)){
			temp =0;
			stream.clear();
		}
	}


 Enter your first and last name:: AA BB

 Enter your age:: 223



                        :: Your Name and Age ::
 Name: AA BB
 Age: 223

Press any key to continue . . .
Ok, thanks. :)
Topic archived. No new replies allowed.