GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 23-Oct-2009, 09:56
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Combining Comparisons


Please see attachement for rules.

New Assignment:


Here is what I have, Is this the best way to do this?

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;


int main()
{
        string name, military, gender, yes, no;
        cout <<"What is your full name? " <<endl;
        getline (cin, name);
        
        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 >> gender;
        
         while (gender == "m") 
         {
         if ((age >= 18) && (age <=30)) 
         {
         cout << "Yes, "<< name << ", you may apply." << endl;
         break;
         }
         if ((age >= 18 && age <=35) && (military == yes)) 
         {
         cout << "Yes, "<< name << ", you may apply." << endl;
         break;
         }
         if ((age >= 18 && age <=35) && (pushups >=50))
         {
         cout << "Yes, "<< name << ", you may apply." << endl;
         break;
         }
         else
         {
         cout << "Sorry, "<< name << ", you are not eligible." << endl;
         break;
         }
}
                

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

}

Attached Files
File Type: txt Rules.txt (914 Bytes, 4 views)
Last edited by admin : 23-Oct-2009 at 11:37. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 23-Oct-2009, 10:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Combining Comparisons


Quote:
Originally Posted by TitanGuy
...Is this the best way to do this

Not in my opinion.

First of all, there is some stuff that absolutely won't work:

CPP / C++ / C Code:
    cin >> m, f;
This reads a single character into the variable m. It doesn't change the value of the uninitialized variable f

CPP / C++ / C Code:
    while (m == m) {
This compares the value of the variable m with the value of the variable m, so the loop is entered regardless of the value of the variable m.

What you probably meant to do was
CPP / C++ / C Code:
    while (m == 'm') {
.
.
.

Similarly look at
CPP / C++ / C Code:
        if ((age >= 18 && age <= 35) && (military == yes)) {

The second part of the if condition compares the value of the string military, which was entered by the user to the value of your string variable yes. You didn't put anything into the variable named yes, so it's an empty string.

You really need something like
CPP / C++ / C Code:
        if ((age >= 18 && age <= 35) && (military == "yes")) {
Now it is comparing the user's response with the string literal "yes"


However I have a really important issue with the style:

To do what you want, a loop is utterly uncalled for

I would say that you should consider something like this:
CPP / C++ / C Code:
    char sex;
    cin >> sex;

    if (sex == 'm') { // All of the stuff for the guys
        if ((age >= 18) && (age <= 30)) {
            cout << "Yes, " << name << ", you may apply." << endl;
        }
        else if ((age >= 18 && age <= 35) && (military == "yes")) {
            cout << "Yes, " << name << ", you may apply." << endl;
        }
        else if ((age >= 18 && age <= 35) && (pushups >= 50)) {
            cout << "Yes, " << name << ", you may apply." << endl;
        }
        else {
            cout << "Sorry, " << name << ", you are not eligible." << endl;
        }
    }

    else if (sex == 'f') { // All of the stuff for the ladies
 //  etc.
    }

    else { // Invalid entry for sex: neither 'm' or 'f'
        //Print a message about invalid entry for sex.
    }



Regards,

Dave

Footnote: To make your C++ code look prettier in your posts, surround it with language-specific code tags:

Put [c++] before the first line and put [/c++] after the last line.
  #3  
Old 23-Oct-2009, 11:16
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: Combining Comparisons


Ok, Dave, thank you, If I understand correctly, here is what I have, what do you think:

CPP / C++ / C Code:
#include <iostream>
#include <string>
using namespace std;


int main()
{
        string name, military, yes, no;
        cout <<"What is your full name? " <<endl;
        getline (cin, name);
        
        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 gender;
        cout << "Are (m)ale or (f)emale?" << endl;
        cin >> gender;
        
        {
        if (gender == 'm')
            if ((age >= 18) && (age <= 30)){
            cout << "Yes, " << name << ", you may apply." << endl;
            }
            else if ((age >= 18 && age <= 35) && (military == "yes")) {
            cout << "Yes, " << name << ", you may apply." << endl;
            }
            else if ((age >= 18 && age <= 35) && (pushups >= 50)) {
            cout << "Yes, " << name << ", you may apply." << endl;
            }
            else {
            cout << "Sorry, " << name << ", you are not eligible." << endl;
            }
  
        else if (gender == 'f')
            if ((age >= 18) && (age <= 32)) {
            cout << "Yes, " << name << ", you may apply." << endl;
            }
            else if ((age >= 18 && age <= 40) && (military == "yes")) {
            cout << "Yes, " << name << ", you may apply." << endl;
            }
            else if ((age >= 18 && age <= 40) && (pushups >= 30)) {
            cout << "Yes, " << name << ", you may apply." << endl;
            }
            else {
            cout << "Sorry, " << name << ", you are not eligible." << endl;
            }
}
}
Last edited by admin : 23-Oct-2009 at 11:37. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #4  
Old 27-Oct-2009, 11:21
TitanGuy TitanGuy is offline
New Member
 
Join Date: Oct 2009
Posts: 29
TitanGuy is on a distinguished road

Re: Combining Comparisons


Just wanted to Thank Dave again, and also Apologize for not using the

correct cpp and /cpp coding. I overlooked that several times and was using just code and /code.

So I am sincerely am sorry for missing that. I would of replied sooner but was a banned for a couple days.

Thanks
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
combining localtime() members into a single long long Howard_L C Programming Language 10 11-May-2007 23:58
Combining Static & Dynamic content rajumurugan Apache Web Server Forum 0 09-May-2007 09:05
OO Language Comparisons Hazy Miscellaneous Programming Forum 2 22-Nov-2006 04:36
Combining of mfc and c sources riswallow MS Visual C++ / MFC Forum 1 06-Nov-2006 02:12
Combining Dll and Application for Pocket PC 2003 ivriedel .NET Forum 4 20-Dec-2005 08:46

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:39.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.