In: Computer Science
Write this program in a C++ language Calculate the ideal age of a spouse. Enter either m or f from the keyboard in lower case. You may use string data for the gender. Convert the gender to upper case
Enter an age from the keyboard, probably an integer
You will need prompts telling the user what to enter.
Use an IF statement to determine if the person is a male or female. You do one calculation if the person is a male else you do another calculation if the person is a female.
Use a loop where you enter this data (loop for 1 to 6 ) so you will only have to run the program 1 time instead of 6 times.
m 28
m 70
m 18
f 32
f 60
f 13
For each set of data, print out the Gender, age, and Ideal Spouse's age, along with the following messages when they apply (the messages will be in the last column):
If a male over 60, print “robbing the cradle.”
If a male under 25, print “too young to be married”
if a female over 60, print “a gold digger”
if a female < 19 print “jail bait”
Plato's formula. A little bit out of date. You will be a gold digger or robbing the cradle if your age is over 40 because back then people only lived to be about 35 or so on the average.
For a male, his ideal spouse’s age is his age/2+7
For a female, her age*2-14
So, inside the loop
1. Input from the keyboard either m or f and an age
2. convert the m or f to upper or upper case
3. enter age from keyboard
4. Use an if to determine if user is a male or female. Use the appropriate syntax for your language. Calculate Ideal age.
5. Print the Gender as “Male” or as “Female”, and the age, and the ideal age you calculated.
6. Then print any matching messages on the same line
7. Accumulate the total idea age of a male spouse and of a female spouse and the count of males and females.
8. When you exit the loop the print the average idea age of a spouse for a male and the average ideal age of a spouse for a female.
if gender=="M"
do male calculation
else
do female calculation
remember two = signs for comparison
You will need braces for code blocks
If you need any corrections/clarifications kindly comment
Please give a Thumps Up if you like the answer
Program
#include <iostream>
#include <cctype>
#include<string>
using namespace std;
int main()
{
char gender;
int age,
total_ideal_age[2] = {0}, count[2] = {0}, ideal_age;
for (int i = 0; i <
6; i++)
{
cout << "\nEnter gender(m or f):"; //Input from the keyboard
either m or f
cin >> gender;
gender=toupper(gender);//Converting to uppercase
cout<<"Enter age : ";// Enter age from keyboard
cin>>age;
if(gender=='M')//Determine if user is a male or female
{
ideal_age=(age/2)+7;//Calculate Ideal age.
// Print the Gender as “Male” , and the age, and the ideal age
calculated.
cout<<"Gender :Male"<<"\t"<<"Age :
"<<age<<"\t"<<"Ideal Age of spouse:
"<<ideal_age<<"\t";
if (age>60)
cout<<"Robbing the cradle."<<endl;
else if (age<25)
cout<<"Too young to be married"<<endl;
total_ideal_age[0]+=ideal_age;//Accumulate the total ideal age of a
spouse for a male
count[0]+=1;//Accumulate the total count of males.
}
else if (gender=='F')//Determine if user is a male or female
{
ideal_age=(age*2)-14;//Calculate Ideal age.
// Print the Gender as “Female”, and the age, and the ideal age
calculated.
cout<<"Gender :Female"<<"\t"<<"Age :
"<<age<<"\t"<<"Ideal Age of spouse:
"<<ideal_age<<"\t";
if (age>60)
cout<<"A gold digger"<<endl;
else if (age<19)
cout<<"Jail Bait"<<endl;
total_ideal_age[1]+=ideal_age;//Accumulate the total ideal age of a
spouse for a female
count[1]+=1;//Accumulate the total count of females.
}
}
cout<<"\nAverage
ideal age of a spouse for a male
:"<<(float)total_ideal_age[0]/count[0];
cout<<"\nAverage
ideal age of a spouse for a female
:"<<(float)total_ideal_age[1]/count[1];
return 0;
}
Output
Enter gender(m or f):3 m
Enter age : 28
Gender :Male Age : 28 Ideal Age of spouse:
21
Enter gender(m or f):m
Enter age : 70
Gender :Male Age : 70 Ideal Age of spouse:
42 Robbing the cradle.
Enter gender(m or f):m
Enter age : 18
Gender :Male Age : 18 Ideal Age of spouse:
16 Too young to be married
Enter gender(m or f):f
Enter age : 32
Gender :Female Age : 32 Ideal Age of
spouse: 50
Enter gender(m or f):f
Enter age : 60
Gender :Female Age : 60 Ideal Age of
spouse: 106
Enter gender(m or f):f
Enter age : 13
Gender :Female Age : 13 Ideal Age of
spouse: 12 Jail Bait
Average ideal age of a spouse for a male :26.3333
Average ideal age of a spouse for a female :56