Click to See Complete Forum and Search --> : C++ noob in need of help!
RyanA19
10-21-2004, 02:13 AM
Hi, I am very new to C++ and I seem to be having a problem getting a program to work right. My program has to display true or false depending on the conditions that need to be met. For this program it deals with credit approval, so I assigned "bool creditOK". Now when I run the program I get either a 1 or 0 instead of true or false? Do I need boolalpha in my program somewhere? I looked throught my book and havnt found my answer yet and was hoping you guys could help me answer this simple question. Thanks!!
Malone
10-21-2004, 06:15 PM
Standard C++ has a bool type which takes a value of either true or false. Sending this to an outputstream should give you a string "true" or "false".
#include <iostream>
using namespace std;
int main()
{
bool creditOK = true;
cout << creditOK << endl;
return 0;
}
The above code should print:
true
If that doesn't compile, then your compiler is out of date. If it does compile, I don't know why it's printing 0 or 1 instead of true or false. Can you post your code to show us what it does exactly?
RyanA19
10-21-2004, 10:44 PM
I compiled the code you had and it returned true. Here is my code that I have so far:
#include <iostream>
using namespace std;
int main()
{
double income, assets, lia;
bool creditOK;
cout << "\nEnter your income: $";
cin >> income;
cout << "\nEnter your assets: $";
cin >> assets;
cout << "\nEnter liabilities: $";
cin >> lia;
creditOK = (((income >= 25000) || (assets >= 100000) && (lia < 50000)));
cout << "Credit = "<< creditOK << endl;
return 0;
}
Everytime I run it, it displays Credit = 1 or Credit = 0 instead of true or false and I do not why. Thanks for the help.
You need to set the boolalpha flag, part of iomanip:
Add #include <iomanip> at the top, and then change your cout line to:
cout << "Credit = " << boolalpha << creditOK << endl;
Alternatively, you can explicitly set the flag separately and use the same cout code. Just add
cout.setf(ios_base::boolalpha);
before the output.
RyanA19
10-23-2004, 12:06 AM
Thanks!, It works now.