C++ noob in need of help!

Sharky Forums


Results 1 to 5 of 5

Thread: C++ noob in need of help!

  1. #1
    Catfish
    Join Date
    Aug 2001
    Location
    St. Louis
    Posts
    147

    C++ noob in need of help!

    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!!
    DFI LanParty Ultra-D
    OCZ Powerstream 520W
    AMD 64 3200+ @ 2.4
    CoolerMaster Hyper 6/ Vantec Tornado
    eVGA 7800gt
    2gb OCZ PC4000 EBPE
    Sound Blaster Audigy 2
    300gb Maxtor Diamond Max 10 SATA
    NEC DVD-RW
    Lite-On 52x32x52
    Klipsch 4.1 Speakers
    KDS 19" CRT

  2. #2
    Tiger Shark
    Join Date
    Oct 2000
    Location
    Erie, PA, USA
    Posts
    693
    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".

    Code:
    #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?
    AMD AthlonXP 2600+ Thoroughbred B @ 200x10.5
    Shuttle AN35N nForce2 Ultra 400
    2x512MB Kingston PC3200 (3-3-3)
    ATI Radeon 9600 Pro
    40GB WD ATA-100 8MB cache
    Creative 12X DVD Drive
    Memorex 52X CD-RW
    Running Windows XP Pro

  3. #3
    Catfish
    Join Date
    Aug 2001
    Location
    St. Louis
    Posts
    147
    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.
    DFI LanParty Ultra-D
    OCZ Powerstream 520W
    AMD 64 3200+ @ 2.4
    CoolerMaster Hyper 6/ Vantec Tornado
    eVGA 7800gt
    2gb OCZ PC4000 EBPE
    Sound Blaster Audigy 2
    300gb Maxtor Diamond Max 10 SATA
    NEC DVD-RW
    Lite-On 52x32x52
    Klipsch 4.1 Speakers
    KDS 19" CRT

  4. #4
    NullPointerException rock's Avatar
    Join Date
    Sep 2000
    Location
    York, PA
    Posts
    6,203
    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.

    Open Source is free like a puppy is free.

    It's only when you look at an ant through a magnifying glass on a sunny day that you realise how often they burst into flames.

    Understanding Evolution

  5. #5
    Catfish
    Join Date
    Aug 2001
    Location
    St. Louis
    Posts
    147
    Thanks!, It works now.
    DFI LanParty Ultra-D
    OCZ Powerstream 520W
    AMD 64 3200+ @ 2.4
    CoolerMaster Hyper 6/ Vantec Tornado
    eVGA 7800gt
    2gb OCZ PC4000 EBPE
    Sound Blaster Audigy 2
    300gb Maxtor Diamond Max 10 SATA
    NEC DVD-RW
    Lite-On 52x32x52
    Klipsch 4.1 Speakers
    KDS 19" CRT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •