This is my program:

There is a secret government organization called PIB which only accepts recruits who fit
their criteria (shown below). If you fit any of the combinations of criteria, then you can
apply to work for PIB. Write a program which asks the user appropriate questions (ask
all the questions up front) and then determines if the candidate is acceptable. Use the
screen shots as a guide.
Here are the rules:

• If you are male between the ages of 18 and 30 (inclusive) you may apply.
• If you are female between the ages of 18 and 32 (inclusive) you may apply.
• If you are male between the ages of 18 and 35 (inclusive) and either were in the
military or you can do at least 50 pushups in a row you may apply.
• If you are female between the ages of 18 and 40 (inclusive) and either were in the
military or you can do at least 30 pushups in a row you may apply.
• No one else is eligible.

I have this so far:

#include <iostream>
#include <string>
using namespace std;
int main ()
{
int age;
int pushups;
string name;
string gender = "m";
string answer = "yes";

while (answer == "yes")
{
cout << "What is your full name? ";
getline(cin, name);

cout << "How old are you? ";
cin >> age;

cout << "Were you ever in the military <yes/no>? ";
cin >> answer;

cout << "How many pushups can you do in a row? ";
cin >> pushups;

cout << "Are you <m>ale or <f>emale? ";
cin >> gender;

}
}

I need my program to out put the message: Yes, <name>, you may apply.....If they meet the criteria above. If they don't meet the criteria, then it needs to say "Sorry, <name>, you are not eligible."

I have been working on it all day and I can't seem to figure out a way to compare everything. It has to be in a while loop with if statements.

Recommended Answers

All 7 Replies

We would like it if you could post in your attempt and then we will sort it out to show your mistakes.

I just decided to not use a loop because it is unnecessary. Does this program look good?

#include <iostream>
#include <string>
using namespace std;
int main ()
{
int age;
int pushups;
string name;
string gender;
string answer;

cout << "What is your full name? ";
getline(cin, name);

cout << "How old are you? ";
cin >> age;

cout << "Were you ever in the military <yes/no>? ";
cin >> answer;

cout << "How many pushups can you do in a row? ";
cin >> pushups;

cout << "Are <m>ale or <f>emale? ";
cin >> gender;

if (((((age >= 18) && (age <= 30) && (gender == "m")) || 
	((age >= 18) && (age <= 32) && (gender == "f"))) ||
	((gender == "m") && (age >= 18) && (age <= 35) && (answer == "yes" || pushups >=50))) ||
	((gender == "f") && (age >= 18) && (age <= 40) && (answer == "yes" || pushups >=30)))

cout << "Yes, " << name << ", you may apply." << endl;

else
cout << "Sorry, " << name << ", you are not eligible." << endl;


}

No :)
But I believe it does what you want it to do.

Ok, I like this style more (it's the same concept), but it's just a suggestion:

bool male = (gender == "m");
bool is_for_army;
bool was_in_army = (answer == "yes");

if (age>=18){
    if (male){
        if (age <= 30) is_for_army = true;
        else if (age <= 35 && (was_in_army || pushups >=50)) is_for_army = true;
        else is_for_army = false;
    }
    else{
        if (age <= 32) is_for_army = true;
        else if (age <= 40 && (was_in_army || pushups >=30)) is_for_army = true;
        else is_for_army = false;
    }
}
else is_for_army = false;

if (is_for_army)
    cout << "Yes, " << name << ", you may apply." << endl;
else
    cout << "Sorry, " << name << ", you are not eligible." << endl;

Yes, yours does look much nicer. I always seem to write in a sloppy manner which will hurt me with longer programs. My teacher gives us examples, which never seem to help, and he didn't use bools so neither did I. I really appreciate the help Sci@phy. I'm sure I'll be posting more in the near future. These assignments are getting difficult for me.

And you should also consider . New lines during your input. It will also make the program presentable.

Yes, yours does look much nicer. I always seem to write in a sloppy manner which will hurt me with longer programs. My teacher gives us examples, which never seem to help, and he didn't use bools so neither did I. I really appreciate the help Sci@phy. I'm sure I'll be posting more in the near future. These assignments are getting difficult for me.

You don't have to use bools for simple stuff, like:

if (a >= b) //code here
else //code here

I used bool for two reasons:
1. To assing values to stuff that is calculated more than once
You had four checks if (age >= 18) and each time computer has to calculate once again if it is true (to protect myself from experts: maybe :) ). In this way it calculates only once.
2. To make code more readable
There's no much explanation to this, but as I said earlyer, don't overuse bools

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.