boolean operators

This is a discussion on boolean operators within the C++ Programming forums, part of the General Programming Boards category; Could someone tell me what is wrong with the boolean operators in this program thanks!! Code: #include <iostream> #include "test2.h" ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    boolean operators

    Could someone tell me what is wrong with the boolean operators in this program
    thanks!!
    Code:
    #include <iostream>
    #include "test2.h"
    
    using namespace std;
    
    
    
    int main()
    {
    
    	
    	double balance, chargeAmt,c,payAmt, bal,C ,P,D, Q;
    	string trans;
    	
    	cout<<"Enter beginning balance. ";
    	cout<<endl;
    	cin>>balance;
    	cout<<endl;
    	
    	if (cin.fail())
    	{
    		cout<<"ERROR! Bad Input!!";
    		return 1;
    	}
    
    	cout<<"How can we be of service today?";
    	cout<<endl;
    
    	do 
    	{
    
    		cout<<"Enter transaction: (C, P, D, Q) ";
    		cin>>trans;
    		cout<<endl;
    	}
    
    	
    	while (trans != Q);
    
    	if(trans == C)	
    	{
    		cout<<"Amount of charge: ";
    		cin>>chargeAmt;
    		cout<<endl;
    	
    		bal = charge_amount(balance,chargeAmt);
    		cout<<"Your new balance is "<<bal;
    	}
    
    	else if (trans == P)
    	{	
    		cout<<"Payment amount: ";
    		cin>>payAmt;
    		cout<<endl;
    	
    		bal = pay_amount(balance,payAmt);
    		cout<<"Your new balance is "<<bal;
    	}
    	
    	else if (trans == D)
    	{
    		cout<<"Your balance is "<<bal;
    		cout<<endl;
    	}
    
    
    	else if (trans == Q)
    	{
    		cout<<"Good Bye!";
    		cout<<endl;
    	}
    	
    	else 
    		cout<<"Enter valid input!";
    
    
    return 0;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    > while (trans != Q);
    You mean apart from the fact that trans is an initialised string, and Q is an uninitialised double?

    Perhaps you meant
    while (trans != "Q" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    i had tried that but i got the same error:


    binary '==' : std::string' does not define this operator or a conversion to a type acceptable to the predefined operator

  4. #4
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Quote Originally Posted by bj31t
    i had tried that but i got the same error:


    binary '==' : std::string' does not define this operator or a conversion to a type acceptable to the predefined operator
    add #include <string> so you could make use of the overloaded operators.
    I see that you could've declared trans as a char instead of a string.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    OK I just looked over your code again, after misreading it the first time...

    Your loop is kind of messed up. Ignoring syntax errors, what it does is display a prompt, then get user input. If the input isn't 'Q' for quit, then it displays the prompt again and inputs again, etc. until the user DOES enter 'Q'.

    First, you don't need all those doubles called c, C, Q, P, D. I don't see why you have them declared, since you never use them.

    Then, you need to stick all of the 'if' and 'else if' in the loop.

    Ok, now that the program makes logical sense, you need to get the syntax right. Declare trans as a char instead of a string, for the sake of simplicity. Then C, P, D, Q need to have single quotes around them, otherwise they'll be interpreted as variables (which I suspect is why you have them declared as doubles - if the compiler spits out an error, don't randomly make changes and hope that it'll stop giving you errors, otherwise you'll just get bigger problems, like this. Take the time to figure out what's wrong.). The single quotes means to interpret it as a char.

    Once you've done all this, I *think* it should work, although I'm not 100% sure because I didn't try it.

    Hope this helps!
    Last edited by Hunter2; 03-30-2004 at 07:12 PM. Reason: Misread the code...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Thanks!!! That helped a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. boolean operators
    By forkpie hat in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2008, 02:35 AM
  2. Bolean Operators hurt my head. (Trouble understanding) :(
    By Funcoot in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2008, 06:42 PM
  3. C++ and Boolean Operators
    By Rockskin in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2006, 02:45 PM
  4. Boolean operators
    By Trogdor27 in forum C++ Programming
    Replies: 10
    Last Post: 09-12-2005, 06:46 AM
  5. Boolean Operators?
    By civilwarsearch in forum C++ Programming
    Replies: 11
    Last Post: 12-15-2003, 08:50 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21