C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 308,424 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 3,248 people online right now. Registration is fast and FREE... Join Now!




Need help Combining Comparisons

 

Need help Combining Comparisons

mvb5142

3 Jul, 2009 - 01:35 PM
Post #1

New D.I.C Head
*

Joined: 29 Jun, 2009
Posts: 25

In this program I' trying to combine comparisons, below I have given the layout of the program I am trying to build. Also I have included what I come up with so far. From what I have so far I can build the program but I cannot figure out have to stop it from cout'ing. So if someone can help me get it to stop looping, it will be highly appreciated.





Combining Comparisons

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.


*/

cpp

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


int main()
{
string fullname, military, yes, no;
cout <<"What is your full name? " <<endl;
getline (cin, fullname);

int age, pushups;
cout << "How old are you? " <<endl;
cin >> age;

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

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

char m, f;
cout << "Are (m)ale or (f)emale?" << endl;
cin >> m, f;

while (m == m)
{
if ((age >= 18) && (age <=30))
cout << "Yes, "<< fullname << ", you may apply." << endl;
if ((age >= 18 && age <=35) && (military == yes))
cout << "Yes, "<< fullname << ", you may apply." << endl;
if ((age >= 18 && age <=35) && (pushups >=50))
cout << "Yes, "<< fullname << ", you may apply." << endl;
else
cout << "Sorry, "<< fullname << ", you are not eligible." << endl;
break;
}


while (f == f)
{
if ((age >= 18) && (age <=32))
cout << "Yes, "<< fullname << ", you may apply." << endl;
if ((age >= 18 && age <=40) && (military == yes))
cout << "Yes, "<< fullname << ", you may apply." << endl;
if ((age >= 18 && age <=40) && (pushups >=30))
cout << "Yes, "<< fullname << ", you may apply." << endl;
else
cout << "Sorry, "<< fullname << ", you are not eligible." << endl;
break;
}




}


*** MOD EDIT: Added code tags. Please code.gif ***

User is offlineProfile CardPM
+Quote Post


ComputerAnalysis

RE: Need Help Combining Comparisons

3 Jul, 2009 - 01:52 PM
Post #2

D.I.C Head
**

Joined: 29 Jun, 2009
Posts: 79



Thanked: 5 times
My Contributions
Without changing your program flow the cheap and easy way to do this would be to add a break statment in each if block. It's always a good idea to use {} on your if statments even if they're only one line so that it is easier to go back and make changes, it also makes code easier to read. Lastly please use the code brackets when you create posts. It makes everything easier to read.
CODE

if ((age >= 18) && (age <=30))
{
cout << "Yes, "<< fullname << ", you may apply." << endl;
break;
}
if ((age >= 18 && age <=35) && (military == yes))
{
cout << "Yes, "<< fullname << ", you may apply." << endl;
break;
}
if ((age >= 18 && age <=35) && (pushups >=50))
{
cout << "Yes, "<< fullname << ", you may apply." << endl;
break;
}
else
{
cout << "Sorry, "<< fullname << ", you are not eligible." << endl;
break;
}


This post has been edited by ComputerAnalysis: 3 Jul, 2009 - 01:53 PM
User is offlineProfile CardPM
+Quote Post

apw5020

RE: Need Help Combining Comparisons

3 Jul, 2009 - 02:30 PM
Post #3

D.I.C Addict
****

Joined: 26 Mar, 2009
Posts: 577



Thanked: 58 times
My Contributions
cin >> m, f;

That line is a problem. Instead, you should do this:
CODE

//...
char gender;        
cout << "Are (m)ale or (f)emale?" << endl;        
cin >> gender;
//...
while(gender == 'm')
{
//...
}
while(gender == 'f')
{
//...
}


Also, where do you use the string variables "yes" and "no"?

This post has been edited by apw5020: 3 Jul, 2009 - 02:32 PM
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/24/09 01:39PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month