Thread: Help Please

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    2

    Help Please

    How would i make this create an error message if a number was divided by 0

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
    	// Variable Declarations
    	int num1, num2, num3, num4;
    	char letter = 'y', symbol;
        
        // Prompt 
    	cout << "This Program will add, subtract, multiply, and divide" 
    		 << "the two numbers you enter." << endl;
        cout << endl;
    
    	do
    	{
            cout << "Please enter two numbers: ";
    		cin >> num1 >> num2;
    		cout << endl;
    
    		cout << "Please input the operation you would like to use: ";
    		cin >> symbol;
    		cout << endl;
    
    		switch (symbol)
    		{
    		case '+': num3 = num1 + num2;
    			cout << "The sum is: " << num3 << endl;
    		    break;
    		case '-': num3 = num1 - num2;
    			cout << "The difference is: " << num3 << endl;
    		    break;
    		case '*': num3 = num1 * num2;
    			cout << "The product is: " << num3 << endl;
    			break;
    		case '/': num3 = num1 / num2;
    			num4 = num1 % num2;
    			cout << "The Quotient is: " << num3 << " The Remainder is: "<< num4 << endl;
    		}
            cout << "Would you like to run the program again [y/n] ";
    		cin >> letter;
    		cout << endl;
    	}
    	while (letter == 'y');
    	    cout << "End of Program" << endl;
    
    	return 0;
    }

  2. #2
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Simply do a check if num2 is zero BEFORE you do any division on it, then print an error.

Popular pages Recent additions subscribe to a feed