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.**

Recommended Answers

All 17 Replies

assuming answer is a string

bool result;
if( answer == "YES" )
    result = true;
else
    result = false;

Thanks. I keep getting an error when I enter it. What would be the library for that?

post code and error message(s)

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;
}

>>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

Hum... it works the way I have it... =S

>>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*

lmao okay i'll try to fix that... what about the good and bad thing though?

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.

Okay wait... I should have posted this as well.

Approval conditions :
• nobody under 16 can get the card.
• everybody over (or exactly) 16 who has a good credit will have their applications approved.
• for those that do not meet the above condition, application will be approved if they own the house.
• for those that do not meet the above condition, application will be approved if they have a permanent job, and their monthly income is over $3000.00.
• all others will have their application denied.


I kinda get what you're saying but does that still make what I have wrong now that you know the conditions?

using those requirements

bool approved = false;
        if( age >= 16)
        {
            if( credit_rating == 1)
                approved = true;
            else if( house_owner == "yes" )
                approved = true;
            else if( permanent_job == "yes" && monthly_income > 3000)
                approved = true;
        }
        if(approved) {
            cout << "*APPLICATION APPROVED*" << endl;
        } else {
            cout << "*APPLICATION DENIED*" << endl;
        }

Okay I'm not sure what I did lol... I just switched credit rating but now it pretty much blows up when I put in any input.

#include <iostream>
#include <string>

using namespace std;

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

	while(completed == 0) {
		
		while(age <= 0 || age > 200) {
			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?\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 << "KREDA 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 == "good" || 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;
}

Okay so that fixed everything except that I still have to put in 0 or 1 for the credit score.. Is there any easy way to fix this? If not I'm just gonna turn it in and hope for the best lol... I've been working on it way too long and I'm sick of looking at code.

Thanks so much for your help btw... You probably saved me from a failing grade and I wouldn't have even realized it.

Every time I read the assignment page I realize I've left something out lol. I'm supposed to show at the end the percentage of approved applications.

To do this I would need a counter of course but how would I get the percentage... I think I've been staring at this way too long... Simple math seems to elude me at the moment.

Okay so that fixed everything except that I still have to put in 0 or 1 for the credit score.. Is there any easy way to fix this? .

Of course there is -- I already said all you have to do is change the data type to std::string and then test it like you did the others.

>>To do this I would need a counter of course but how would I get the percentage
You need two counters -- one that counts the total number of applications and the other that totals the number of approved applictions. The percentage is (approved / total applications) * 100

It does the same thing when I change it to that. It compiles and runs fine until I enter anything for the credit rating then it just scrolls continuously through the program.

Well it was due about an hour ago so I turned it in without having the credit rating as a string. Oh well I'm sure I made a much better grade than I would have without your help.

Thanks soooo much. =D You're awesome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.