#include <string>
#include <iostream>
usingnamespace 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 pname);
void setAge(int page);
int getAge() const;
void print();
private:
string name;
int age; //If 0 is unknown.
};
1. Try to match the name of the file and the name of the class. If the class name is Person, make it Person.h and Person.cpp instead of STUDENT.H and STUDENT.CPP. Name mismatch will only confuse other people that look at your files.
2. In STUDENT.H, remove parameter from setName and setAge because it seems like you want to ask user to input the data in the setters. Setters usually takes a parameter, but looking at your commented-out setters definition, you are asking user to enter the information in setters instead of main(). Not the way I'd do it, but I will go with your design.
3. In STUDENT.CPP ,you can remove cout << getName; and cout << getAge; in the setters. I can't figure out what you were trying to do there. If you want to print the name and age after the user enter the info, it should be
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
#include <iostream>
usingnamespace 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();
void setAge();
int getAge() const;
void print();
private:
string name;
int age; //If 0 is unknown.
};
#endif // PERSON_H_INCLUDED
#include "Person.h"
#include <iostream>
usingnamespace std;
Person::Person()
{
name = "";
age = 0;
}
Person::Person (string pname, int page)
{
pname = name;
page = age;
}
string Person::getName() const
{
return name;
}
int Person::getAge() const
{
return age;
}
void Person::print()
{
cout << "Name : " << name << endl;
cout << "Age : " << age << endl;
}
void Person::setName()
{
cout << "What is the name" << endl;
cin >> name;
}
void Person::setAge()
{
cout << "What is the age" << endl;
cin >> age;
}
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <vector>
#include "Person.h"
constexprint MAX_STUDENTS = 30;
int main()
{
std::vector<Person> people;
for( int i = 0 ; i < MAX_STUDENTS ; i++ ) {
Person student{};
// You have the 's' capitalized in setName() in STUDENT.CPP.
student.setName();
student.setAge();
people.push_back( student );
}
}
Ok, so this program will ask 30 users to enter their name and age and then it will close out.
I am understanding what you're saying. I meant to change the name of the files but never got to it because the program was not running. Thank you for the advice. I'll definitely do that today onwards to make it make more sense.
The inputs also make sense. So thank you for that.
I created a new program to look like what you have written and despite compiling successfully, it does not build.
It's giving me an error saying:
undefined reference to 'Person::Person()'
undefined reference to 'Person::setName()'
undefined reference to 'Person::setAge()'
I included the Person.cpp file in the main.cpp function which made the program run. So that works for now unless you can't do that.
Now when I am entering the data, there is no way for me to quit the program and print the number of students that were entered only.
The program also needs to run again if the age is a negative number or if no value for name is entered.
Explain if possible and also guide me through if you can. I'm having some minor issues doing these basics while using class, constructors, and deconstructors.
What it was doing before was just asking for name and then asks for age. And then keeps repeating those steps without printing them.
After I linked the files as you suggested, it asks for the name, then the age. and then in the next two lines it prints them. Which is perfect but not what I was initially trying to do so maybe you could tell me how i can change the code to do that.
Also, I have no way of just pressing Q and printing the names and date enterred.
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 mpark4656. 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)
EDIT: I posted before reading your last post. Will revise and repost code here
Ok, sorry for the delay. I had to go to work.
For right now, I will give you some suggestions after reading the program specification.
Must initialize the class object variable with initial value in the default constructor
Name: Noname
Age: 0
So basically, your default constructor should look like this
1 2 3 4 5
Person::Person()
{
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.
#include <cctype>
#include <iostream>
#include <vector>
#include <string>
#include "Person.h"
void promptUserForInput( std::vector<Person> &people )
{
std::string name;
int age;
std::cout << "\nEnter your name: ";
std::cin >> name;
while( !isalpha(name[0]) ) {
std::cout << "Invalid Name!"
<< "Re-enter your name: ";
std::cin >> name;
}
std::cout << "\nEnter your age: ";
std::cin >> age;
while( age > 140 || age < 0 ) {
std::cout << "Invalid Age!\n"
<< "Re-enter your age: ";
std::cin >> age;
}
Person person( name , age );
people.push_back( person );
}
void printInformation( const std::vector<Person> &people )
{
for( constauto &person : people ) {
person.print();
}
}
int main()
{
std::vector<Person> people;
int choice = 0;
while( people.size() < 30 && choice != 3 ) {
std::cout << "Menu\n\n";
std::cout << "1. Enter personal information\n"
<< "2. Print personal information\n"
<< "3. Exit\n"
<< "Enter your choice: ";
std::cin >> choice;
if( choice == 1 ) {
promptUserForInput( people );
}
elseif( choice == 2 ) {
printInformation( people );
}
elseif( choice == 3) {
std::cout << "\nGoodBye";
}
else {
std::cout << "\nInvalid Choice\n";
}
}
}
I do not have access to compiler so I couldn't test the code. You may find mistakes in my code, but this is basically what you would have to do to fulfill the requirements.
Anyways, Good Luck and let me know if you need further assistance.
wow I've been stuck at that for so long. It really helps me understand.
The program works like an angel.
But at the beginning of the program I need to ask the user
to either
1. Input name and age for person
2. Print out information that is already stored inside the array
3. End.
I really needed the End function in there that you helped me with. but I need to have it in the beginning as well as through out the program.
To do so should I create the options in the beginning of the main function to output and then use the for loop or switch method?
Or is there something different to be done? If so could you explain it to me?
I will try to work on it then and update you on whether I am able to figure it out on my own or if i need help.
I read a bit about try, catch, and throw. Is it the same if you were to use cin.clear, cin.clear?
No. It's got nothing to do with std::cin -- it's much more general than that. The three keywords are related to exceptions, which allow you to "unwind" the stack in case of an exceptional condition (usually an error) -- to "give up" on an operation and handle it elsewhere.
try...catch lets you handle those exceptional conditions (created with throw)without caring where they come from, only what the program should do next.
They are awfully confusing (at least when abused) in a large program because they can make the flow of control much less predictable. Most importantly, writing exception-safe code that doesn't break things or mess up your data when an exception is thrown is tricky and subtle, so I prefer to avoid exceptions if possible (or handle them ASAP) if I have no choice about it.
They are sometimes appropriate to use when a crash or deadlock (hang) would be inappropriate -- e.g., when you have work that you must do in a destructor somewhere that wouldn't run if the program just died. The process of throwing an exception destroys automatic objects, which can sometimes be really really important.
> I read a bit about try, catch, and throw. Is it the same if you were to use cin.clear, cin.clear?
No, try block is used if you suspect that some code such as function calls will throw an exception. Catch block is used to catch the thrown exception and handle it (Terminate Program, print error and continue, and etc).
For right now, you can simply use while loop to validate the input and not worry about exception. My original code was complicated for no good reason.
Here is the code that I have. I didnt do much on it other than what you suggested. I have been reading about the new inputs you have used to get a better understand. But here is all the code put together.
#ifndef PERSON_H_INCLUDED
#define PERSON_H_INCLUDED
#include <string>
#include <iostream>
usingnamespace 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
The name is used was Hamza Sheikh.
It's working with a single name but it crashes if a space is entered.
Hey! Could you please take a look at this new code and help me with it. I'm not sure what errors I have made. I would appreciate if you had any time to help.