Help Please

This is a discussion on Help Please within the C++ Programming forums, part of the General Programming Boards category; How would i make this create an error message if a number was divided by 0 Code: #include <iostream> using ...

  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
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    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

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