Thread: do.. while loops

  1. #16
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    i figured out how to incorporate the switch statements into this program, but i am having trouble making the error term come up if the user tries to divide by zero.
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	double n=0, n2=0;
    	char operation;
    
    	cout << "Current Total is " << n << endl;
    
    	do
    	{
    		cout << "Enter an operation: + - * / (or enter X to exit):" << endl;
    		cin >> operation;
    		if (operation == 'X')
    			break;
    		cout << "Enter a number: " << endl;
    		cin >> n2;
    		switch (operation)
    		{
    			case '+':
    				n += n2;
    				break;
    			case '-':
    				n -= n2;
    				break;
    			case '*':
    				n *= n2;
    				break;
    		/*	case (('/') && (n2==0)):		
    				cout << "Can not divide by zero!" << endl;
    				break;	
                   */	
    			case '/':
    				n /= n2;
    				break;
    		}
    		cout << "Current Total is " << n << endl;
    	} while (operation != 'X');
    
    }
    the part that is blocked off by the /* and */ is the part i dont know how to figure out. everything else works well with the program, but i dont know how to combine those two like an if statement would be able to do.

    thanks for any help.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Like this:
    Code:
    switch (operation)
    {
    case '+':
        n += n2;
        break;
    case '-':
        n -= n2;
        break;
    case '*':
        n *= n2;
        break;
    case '/':
        if (n2 == 0)
        {
            cout << "Can not divide by zero!" << endl;
        }
        else
        {
            n /= n2;
        }
        break;
    }
    You could have done something like this for your if-else chain version too. It is good practice to check for equality of floating point numbers by checking a range.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    Registered User
    Join Date
    Feb 2009
    Posts
    30
    thanks for the quick response. and for being very helpful.

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by laserlight View Post
    You could have done something like this for your if-else chain version too. It is good practice to check for equality of floating point numbers by checking a range.
    But in this case, the check should be exactly for zero, because you do not divide by zero unless you divide by zero If the value is non-zero the division should be allowed.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by brewbuck
    But in this case, the check should be exactly for zero, because you do not divide by zero unless you divide by zero
    I agree, but ask vart to explain otherwise:
    Quote Originally Posted by vart View Post
    also using == on doubles is not recomended...
    Possibility of overflow in this case? I am not sure.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM