Results 1 to 7 of 7

Thread: [resolved]

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    6

    [resolved]

    Code:
    #include <iostream.h>
    typedef short int si;
    
    int main()
    {
    	si theShoe = 20;
    	si theTie, theWatch;
    	cout << "enter the price for your watch\n";
    	cin >> theTie;
    	cout << "Enter a price for your tie\n";
    	cin >> theWatch;
    	{
    	if ( (theTie >= theWatch) && ( (theTie + theWatch) > theShoe) )
    		cout << "the shoe is less then the watch and the tie combined\n";
    	else 
    		if ( (theTie == theWatch) || ((theTie + 10) == theWatch) )
    			cout << "the tie is either worth as much as the watch or the tie is 10$ more then the then watch\n";
    		return 0;
    	}
    }
    Ok when it asks for me to type in the price of the tie and watch if I type in 11 then it will print the shoe is less then the watch... but if I type in something else wher eit makes the statement true it will not print the shoe is less then the watch.

    Can anyone tell me what I have done wrong or something I can do to fix it please

    Thanks,
    Bb
    Last edited by Bebo; Nov 17th, 2002 at 11:41 PM.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    cout << "enter the price for your watch\n";
    cin >> theTie;
    cout << "Enter a price for your tie\n";
    cin >> theWatch;
    shouldn't it be the other way around?
    cout << "the tie is either worth as much as the watch or the tie is 10$ more then the then watc
    || is or/and in english, in this case its not possible to have and, but exclusive or("either or" in english) is ^ in C++, nothing wrong, just so you know the difference
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    6
    lol, I didnt put much thought in what it said. Just trying to get the program to do what I want

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    6
    o wait nvm I see what your saying :\

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2002
    Posts
    6
    Code:
    #include <iostream.h>
    typedef short int si;
    
    int main()
    {
    	si theShoe = 20;
    	si theTie, theWatch;
    	cout << "enter the price for your watch\n";
    	cin >> theWatch;
    	cout << "Enter a price for your tie\n";
    	cin >> theTie;
    	{
    	if ( (theTie > theWatch) && ( (theTie + theWatch) > theShoe) )
    	
    		cout << "the shoe is less then the watch and the tie combined\n";
    	
    	else 
    		if ( (theTie == theWatch) ^ ((theTie + 10) == theWatch) )
    		
    			cout << "the tie is either worth as much as the watch or the tie is 10$ more then the then watch\n";
    		
    		else
    		
    			cout << "the watch is worth more then everything\n";
    	}
    		return 0;
    	
    }
    Here is what I have now... I still wouldnt mind any suggestions

    thanks,
    Bb

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    ^ is only a bitwise operator! There is no logical XOR in C++!

    You have to simulate it:
    cond1 XOR cond2
    can be written as
    (cond1 AND NOT cond2) OR (NOT cond1 AND cond2)
    in C++ syntax:
    (cond1 && !cond2) || (!cond1 && cond2)

    And it's
    #include <iostream>
    using namespace std;

    not
    #include <iostream.h>
    which is deprecated!

    You also don't need to write short int, you can just write short. Which makes the typedef unnecessary.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    But in your case you don't need XOR. Here are the truth tables of OR and XOR:
    Code:
    OR 0 1   XOR 0 1
     0 0 1     0 0 1
     1 1 1     1 1 0
    Since it is logically impossible that the watch has the same price as the tie AND has a price exactly 10 higher than the tie you can remove the lower right field from the table wich leaves you with two identical tables. So you can use the simple OR here.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width