do.. while loops

This is a discussion on do.. while loops within the C++ Programming forums, part of the General Programming Boards category; i figured out how to incorporate the switch statements into this program, but i am having trouble making the error ...

  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
    22,159
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    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
    22,159
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 2 of 2 FirstFirst 12
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, 02: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, 06:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 09:46 PM

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