So everything else in my code is working except for the last final bit in which I display a message. My output is a bit off I should be getting:
Enter the child’s name: Rob
Enter the child’s age: 10
Enter the child’s gender: M
Rob is a 11 year-old male child, who is in school
Rose is a 1 year-old female child, who is not in school
Press any key to continue . . .
Instead I'm getting:
Enter the child’s name: Rob
Enter the child’s age: 10
Enter the child’s gender: M
Rob is a 11 year-old male child, who is
not in school
Rose is a 1 year-old female child, who is not in school
Press any key to continue . . .
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
|
#include <iostream>
#include <string>
#include "Child.h"
using namespace std;
int main()
{
// Declare variables
string enteredName = " ";
int enteredAge = 0;
char enteredGender = ' ';
// Request the user to give you the name, age (1-17)
// and gender (‘M’ or ‘F’) of a child in this order
cout << "Enter the child's name: " << endl;
getline(cin, enteredName);
cout << "Enter the child's age (1-17): " << endl;
cin >> enteredAge;
cout << "Enter the child's gender (M/F): " << endl;
cin >> enteredGender;
toupper(enteredGender);
// Create a Child object using the data given and the
// three-parameter constructor
Child childOne;
childOne.setName(enteredName);
childOne.setAge(enteredAge);
childOne.setGender(enteredGender);
// The child has a birthday tomorrow. Change the child
// object to reflect the child’s new age
childOne.setAge(enteredAge + 1);
// Print a message about this child
childOne.childMsg();
std::cout << endl;
// Create another Child object using the
// default constructor
Child childTwo;
// Change its gender to female
childTwo.setGender('F');
// Change its name to “Rose”
childTwo.setName("Rose");
// Print a message about this child
childTwo.childMsg();
std::cout << endl;
system("pause");
return 0;
}
//default constructor
Child::Child()
{
name = "Roy";
age = 1;
gender = 'M';
inSchool = false;
}
//Set Functions
void Child::setName(string enteredName)
{
name = enteredName;
}
void Child::setAge(int enteredAge)
{
age = enteredAge;
}
void Child::setGender(char enteredGender)
{
gender = enteredGender;
}
void Child::setInSchool(int getAge())
{
if (getAge() >= 0 && getAge() <= 4)
{
inSchool = false;
}
else if (getAge() >= 5)
{
inSchool = true;
}
}
//Get Functions
string Child::getName()
{
return name;
}
int Child::getAge()
{
return age;
}
char Child::getGender()
{
return gender;
}
bool Child::getInSchool()
{
return inSchool;
}
//Output Function
void Child::childMsg()
{
if (getInSchool() == false && getGender() == 'M'|| getGender() == 'm')
{
cout << getName() << " is a " << getAge() << " year-old male child, who is not in school." << endl;
}
else if (getInSchool() == true && getGender() == 'M'|| getGender() == 'm')
{
cout << getName() << " is a " << getAge() << " year-old male child, who is in school." << endl;
}
else if (getGender() == 'F' || getGender() == 'f' && getInSchool() == false)
{
cout << getName() << " is a " << getAge() << " year-old female child, who is not in school." << endl;
}
else if (getGender() == 'F' || getGender() == 'f' && getInSchool() == true)
{
cout << getName() << " is a " << getAge() << " year-old female child, who is in school." << endl;
}
}
|