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: jennyebrooke is an unknown quantity at this point 
Solved Threads: 0
jennyebrooke jennyebrooke is offline Offline
Newbie Poster

Yes/No to 1/0 and if else statement

 
0
  #1
Apr 11th, 2008
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.**
Last edited by jennyebrooke; Apr 11th, 2008 at 10:15 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Yes/No to 1/0 and if else statement

 
0
  #2
Apr 11th, 2008
assuming answer is a string
  1. bool result;
  2. if( answer == "YES" )
  3. result = true;
  4. else
  5. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: jennyebrooke is an unknown quantity at this point 
Solved Threads: 0
jennyebrooke jennyebrooke is offline Offline
Newbie Poster

Re: Yes/No to 1/0 and if else statement

 
0
  #3
Apr 11th, 2008
Thanks. I keep getting an error when I enter it. What would be the library for that?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Yes/No to 1/0 and if else statement

 
0
  #4
Apr 11th, 2008
post code and error message(s)
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: jennyebrooke is an unknown quantity at this point 
Solved Threads: 0
jennyebrooke jennyebrooke is offline Offline
Newbie Poster

Re: Yes/No to 1/0 and if else statement

 
0
  #5
Apr 12th, 2008
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?


  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main () {
  7. char name[50], house_owner[4], permanent_job[4], process[4];
  8. int age, monthly_income, credit_rating, completed = 0;
  9.  
  10. while(completed == 0) {
  11.  
  12. cout << "Enter your age: \n";
  13. cin >> age;
  14.  
  15. cout << "Enter your name: \n";
  16. cin >> name;
  17.  
  18. cout << "Enter your monthly income: \n";
  19. cin >> monthly_income;
  20.  
  21. cout << "What is your credit rating? Enter 1 for a good score or 0 for a bad score: \n";
  22. cin >> credit_rating;
  23.  
  24. cout << "Do you own a house?: \n";
  25. cin >> house_owner;
  26.  
  27. cout << "Do you have a permanent job?:\n";
  28. cin >> permanent_job;
  29.  
  30. cout << "\n\nKREDA CARD APPLICATION \n";
  31. cout << "Name: "<< name << endl;
  32. cout << "Age: "<< age << endl;
  33. cout << "Monthly Income: " << monthly_income << endl;
  34. cout << "Credit Rating: " << credit_rating << endl;
  35. cout << "Owns House: " << house_owner << endl;
  36. cout << "Has Permanent Job: " << permanent_job << endl;
  37.  
  38. if(age >= 16 && (credit_rating == 1 || house_owner == "yes" || (permanent_job == "yes" && monthly_income > 3000))) {
  39. cout << "*APPLICATION APPROVED*" << endl;
  40. } else {
  41. cout << "*APPLICATION DENIED*" << endl;
  42. }
  43.  
  44. cout << "\n\nWould you like to process another application?\n";
  45. cin >> process;
  46.  
  47. if(!strcmp(process, "no"))
  48. completed = 1;
  49. }
  50.  
  51. return 0;
  52. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Yes/No to 1/0 and if else statement

 
0
  #6
Apr 12th, 2008
>>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
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: jennyebrooke is an unknown quantity at this point 
Solved Threads: 0
jennyebrooke jennyebrooke is offline Offline
Newbie Poster

Re: Yes/No to 1/0 and if else statement

 
0
  #7
Apr 12th, 2008
Hum... it works the way I have it... =S
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Yes/No to 1/0 and if else statement

 
0
  #8
Apr 12th, 2008
>>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!

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
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 13
Reputation: jennyebrooke is an unknown quantity at this point 
Solved Threads: 0
jennyebrooke jennyebrooke is offline Offline
Newbie Poster

Re: Yes/No to 1/0 and if else statement

 
0
  #9
Apr 12th, 2008
lmao okay i'll try to fix that... what about the good and bad thing though?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,672
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Yes/No to 1/0 and if else statement

 
0
  #10
Apr 12th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 1192 | Replies: 17
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC