DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Yes/No to 1/0 and if else statement (http://www.daniweb.com/forums/thread118691.html)

jennyebrooke Apr 11th, 2008 8:47 pm
Yes/No to 1/0 and if else statement
 
I need to know how to have the user answer a question using yes or no and the program translating it into 1 or 0. I know this process involves a string... but anymore than that and I'm kinda lost.

**Fixed second problem with the if else statement but still need help with the previous problem.**

Ancient Dragon Apr 11th, 2008 9:17 pm
Re: Yes/No to 1/0 and if else statement
 
assuming answer is a string
bool result;
if( answer == "YES" )
    result = true;
else
    result = false;

jennyebrooke Apr 11th, 2008 9:48 pm
Re: Yes/No to 1/0 and if else statement
 
Thanks. I keep getting an error when I enter it. What would be the library for that?

Ancient Dragon Apr 11th, 2008 10:43 pm
Re: Yes/No to 1/0 and if else statement
 
post code and error message(s)

jennyebrooke Apr 11th, 2008 11:04 pm
Re: Yes/No to 1/0 and if else statement
 
Well I actually figured that out but I do have another problem I could use some help with if you don't mind.

The part that asks the credit rating, I need the user to be able to input good or bad. Then later on in the code the condition say if the credit rating is good it's approved. Know how to do that?


#include <iostream>
#include <string>

using namespace std;

int main () {
        char name[50], house_owner[4], permanent_job[4], process[4];
        int age, monthly_income, credit_rating, completed = 0;

        while(completed == 0) {
       
                cout << "Enter your age: \n";
                cin >> age;

                cout << "Enter your name: \n";
                cin >> name;

                cout << "Enter your monthly income: \n";
                cin >> monthly_income;

                cout << "What is your credit rating? Enter 1 for a good score or 0 for a bad score: \n";
                cin >> credit_rating;

                cout << "Do you own a house?: \n";
                cin >> house_owner;

                cout << "Do you have a permanent job?:\n";
                cin >> permanent_job;

                cout << "\n\nKREDA CARD APPLICATION \n";
                cout << "Name: "<< name << endl;
                cout << "Age: "<< age << endl;
                cout << "Monthly Income: " << monthly_income << endl;
                cout << "Credit Rating: " << credit_rating << endl;
                cout << "Owns House: " << house_owner << endl;
                cout << "Has Permanent Job: " << permanent_job << endl;

                if(age >= 16 && (credit_rating == 1 || house_owner == "yes" || (permanent_job == "yes" && monthly_income > 3000))) {
                        cout << "*APPLICATION APPROVED*" << endl;
                } else {
                        cout << "*APPLICATION DENIED*" << endl;
                }
               
                cout << "\n\nWould you like to process another application?\n";
                cin >> process;
               
                if(!strcmp(process, "no"))
                        completed = 1;
        }
       
        return 0;
}

Ancient Dragon Apr 11th, 2008 11:09 pm
Re: Yes/No to 1/0 and if else statement
 
>>permanent_job == "yes"
You can not compare character arrays like that -- only std::string. So you will have to call strcmp() to compare two character arrays
if( strcmp(permanent_job,"yes") == 0)

But if you change permanent_job to std::string then the comparison you made will work

jennyebrooke Apr 11th, 2008 11:15 pm
Re: Yes/No to 1/0 and if else statement
 
Hum... it works the way I have it... =S

Ancient Dragon Apr 11th, 2008 11:18 pm
Re: Yes/No to 1/0 and if else statement
 
>>it works the way I have it...
Depends on your definition of works. If you mean compiles, then the compiler is comparing addresses, not strings because the compiler isn't smart enough to know any different. Try running it and you will see that it doesn't do what you think its doing.

Humm-- this is the output of your program -- no wonder we have a housing crises in this country! :)

Quote:

KREDA CARD APPLICATION
Name: melvin
Age: 65
Monthly Income: 0
Credit Rating: 1
Owns House: maybe
Has Permanent Job: no
*APPLICATION APPROVED*


Would you like to process another application?
Another run -- note the negative income level
Quote:

KREDA CARD APPLICATION
Name: melvin
Age: 500
Monthly Income: -45000
Credit Rating: 0
Owns House: nope
Has Permanent Job: nope
*APPLICATION DENIED*

jennyebrooke Apr 11th, 2008 11:23 pm
Re: Yes/No to 1/0 and if else statement
 
lmao okay i'll try to fix that... what about the good and bad thing though?

Ancient Dragon Apr 11th, 2008 11:30 pm
Re: Yes/No to 1/0 and if else statement
 
do you mean you want to be able to enter "yes" or "no", or "good" and "bad" instead of 1 and 2? Just make it strings like all the others.

You have other logic errors because no matter what I enter my application is always approved. Even after changing the character arrays to std::string.

I think what you probably want is this:
                if(age >= 16 && credit_rating == 1 && house_owner == "yes" && permanent_job == "yes" && monthly_income > 3000) {

You don't want to approve the loan unless all those conditions are met.


All times are GMT -4. The time now is 1:33 pm.

Forum system based on vBulletin Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
©2003 - 2010 DaniWeb® LLC