class Person program

Person.cpp
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
#include "Person.h"
#include <iostream>
#include <stdexcept>

using namespace std;

Person::Person()
{
	name = "";
	age = 0;
}

Person::Person (string pname, int page)
{
	name = pname;

	if( page >= 0 && page < 120 ) {
        age = page;
	}
	else {
        throw std::invalid_argument( "Invalid Age" );
	}
}

string Person::getName() const
{
	return name;
}

int Person::getAge() const
{
	return age;
}

void Person::print() const
{
	cout << "Name : " << name << endl;
	cout << "Age : " << age << endl;
}

void Person::setName( string name )
{
	this->name = name;
}

void Person::setAge( int age )
{
    if( age >= 0 && age < 120 ) {
        this->age = age;
    }
    else {
        throw std::invalid_argument( "Invalid Age" );
    }
}


main.cpp
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
#include <vector>
#include <iostream>
#include <string>
#include <stdexcept>
#include "Person.h"

constexpr int MAX_STUDENTS = 30;

int main()
{
    
    
    std::vector<Person> people;

    bool invalidAge;

    for( int i = 0 ; i < MAX_STUDENTS ; i++ ) {
        Person student{};
        std::string name;
        int age;

        std::cout << "Enter your name or End if finished: ";
        std::cin >> name;

        // If user enters "End", break out of loop
        if( name == "End" ) {
            break;
            }


        student.setName( name );

        do {
            invalidAge = false;
            std::cout << "Enter your age: ";
            std::cin >> age;

            try {
                student.setAge( age );
            }
            catch( std::invalid_argument invalidArg ) {
                std::cerr << "\nError: " << invalidArg.what() << std::endl;
                invalidAge = true;
            }
            
            
            
        } while( invalidAge );

        people.push_back( student );
    }
    
    
    

    // Print the result
    for( auto person : people ) {
        person.print();
    }

    return 0;
}


Person.h
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
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED

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

//Write a program to read a user's input and store it in the class object below.

class Person
{
	public:
		Person();
		Person(string pname, int page);
		string getName() const;
		void setName( string name );
		void setAge( int age );
		int getAge() const;
		void print() const;

	private:
		string name;
		int age; //If 0 is unknown.
};

#endif // PERSON_H_INCLUDED  


So what I am trying to fix is:
Enter the names and ages without printing them unless q is enterred. When q is entered it prints out

Person 1:
Name:
Age:

Person 2:
Name:
Age:

etc..

Right now it looks like this.

Enter name: John
Enter age: 21

name: John
age: 21

.
I hope im making sense.

I am also attaching the assignment itself below if it helps you understand my assignment a little better.

I appreciate the help. Thank you again

Lab: Person’s class:

Write a program read a user’s input and store the information in a class object below:

class Person

{

public:

Person();

Person (string pname, int page);

string getName() const;

void setName(string pname);

void setAge(int page);

int getAge() const;

private:

string name;

int age; // 0 if unknown

};



You program must have the following:

Your program must include the following:
Implement all the class methods defined above
Must initialize the class object variable with initial value in the default constructor
Name: Noname
Age: 0
Your program will have an user’s menu similar to the one below:
Enter person’s information:
Print out person’s information
Exit
If user selects “1”, the program will ask user to enter person’s name and age. The program must check all user’s input to ensure it is valid input. After the input, your program needs to store the information in an array (maximum 30 person array) and then the program will print out the menu again and waiting for user’s input.
Validate user’s input:
Person’s age must between 0 and 140
Person’s name can’t start with non-alphabet letter (ie: 1st letter of the person’s name must be alphabet letter)
If user selects “2”, the program will print out all the person’s information have entered so far (which is stored in an array). After prints out all the person’s information, the program will print out the menu again and waiting for user’s input.
If user selects “3”, the program will exit.
Your program must include a function that print out your information (similar to lab1 requirement)



Name: print out your name (Your firstname, Lastname)

Class: Class section name (CS-106-02 or CS-106-03)

Date: Today’s date
Last edited on
So what's wrong with your code? It looks fine.
Topic archived. No new replies allowed.