| | |
Yes/No to 1/0 and if else statement
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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.**
**Fixed second problem with the if else statement but still need help with the previous problem.**
Last edited by jennyebrooke; Apr 11th, 2008 at 10:15 pm.
assuming answer is a string
C++ Syntax (Toggle Plain Text)
bool result; if( answer == "YES" ) result = true; else result = false;
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Apr 2008
Posts: 13
Reputation:
Solved Threads: 0
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?
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?
C++ Syntax (Toggle Plain Text)
#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; }
>>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
But if you change permanent_job to std::string then the comparison you made will work
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
>>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!
Another run -- note the negative income level
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!

•
•
•
•
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?
•
•
•
•
KREDA CARD APPLICATION
Name: melvin
Age: 500
Monthly Income: -45000
Credit Rating: 0
Owns House: nope
Has Permanent Job: nope
*APPLICATION DENIED*
Last edited by Ancient Dragon; Apr 12th, 2008 at 12:25 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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:
You don't want to approve the loan unless all those conditions are met.
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.
Last edited by Ancient Dragon; Apr 12th, 2008 at 12:34 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help, Assertion Problem :((
- Next Thread: Another looping question...
Views: 1192 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






