I need to write a program that decides if you can apply for the job or not. After asking a series of questions (all the questions) the program then needs to print out whether you can apply or not.
Here are the requirments for the job
• 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 started this code and it compiles, but it prints out you may apply twice and sometimes even says you cannot apply, any help is greatly appreciated

#include <iostream>
#include <string>
using namespace std;
int main ()
{
int age, pushups;
string firstname,lastname, military, sex;

cout << "What is your full name? ";
	cin >> firstname >> lastname;
cout << "How old are you? ";
	cin >> age;
cout << "Were you ever in the military (yes/no)? ";
	cin >> military;
cout << "How many pushups can you do in a row? ";
	cin >> pushups;
cout << "Are you <m>ale of <f>emale? ";
	cin >> sex;

if ((age >= 18 && age <= 30 && sex == "m") || (age >= 18 && age <= 35 && sex == "m" && pushups < 50 || military == "yes"))
{
	cout << "You can apply" << endl;
}
if ((age >= 18 && age <= 32 && sex == "f") || (age >= 18 && age <= 40 && sex == "f" && pushups < 30 || military == "yes"))
{

	cout << "You can Apply" << endl;
}

else if (age <=18 || age >= 40 ||pushups > 50 )
{	
	cout << "You MAY NOT apply";
}
}
tux4life commented: For using code tags on first post :) +11

Dani AI

Generated

Short summary and safe fixes (builds on advice from and )

The duplicate/contradictory output came from two separate issues: the two sex-checks were independent if statements so more than one branch could print, and the boolean expression grouping (and the pushup comparison direction) was not what you intended because of operator-precedence. was right to suggest making the second branch an else/else if. correctly pointed out the need to group the (pushups >= threshold || military) part and to use >= for the "at least" tests.

Safer pattern and a few improvements

  • Compute a single eligible boolean and print once. That eliminates accidental double output.
  • Normalize and validate input (lowercase the sex and the military answer, guard against negative ages/pushups).
  • Read the full name with getline if you want spaces.
  • Use parentheses to group &&/|| expresions so the logic is explicit.

A compact, clear approach (keeps logic in readable branches)

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

// read name with getline, normalize inputs, compute one `eligible` flag
// then print a single message.
int main() {
    string name;
    getline(cin, name);

    int age = 0, pushups = 0;
    string military;
    char sex = ' ';

    cin >> age >> military >> pushups >> sex;
    for (auto &c : military) c = tolower(static_cast<unsigned char>(c));
    sex = tolower(static_cast<unsigned char>(sex));
    bool wasMil = (military == "yes");

    bool eligible = false;
    if (age >= 18) {
        if (sex == 'm') {
            if (age <= 30) eligible = true;
            else if (age <= 35 && (pushups >= 50 || wasMil)) eligible = true;
        } else if (sex == 'f') {
            if (age <= 32) eligible = true;
            else if (age <= 40 && (pushups >= 30 || wasMil)) eligible = true;
        }
    }

    cout << (eligible ? "You may apply\n" : "You may NOT apply\n");
}

Test edge cases after changes: exactly the threshold ages, exactly the pushup thresholds, and malformed inputs (e.g., uppercase "YES", unexpected sex letters).

Recommended Answers

All 3 Replies

put an else between the first two if statements, like you did between the last two statements.

A few things: first of all, make the second if statement else-if; second, make the last else-if statement else. Also, pushups should be >50/>30. You should be more carefull with your brackets too:

if ((age >= 18 && age <= 30 && sex == "m") || (age >= 18 && age <= 35 && sex == "m" && (pushups >= 50 || military == "yes")))

Aso, you can use the distributive law to simplify the expression with sex == "m" and age >= 18 .

if (age >= 18 && sex == "m" && (age <= 30 || (age <= 35 && (pushups >= 50 || military == "yes"))))

Though this is not necessary.

thanks guys, it works perfect now!

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.