Thread: Need quick help

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

    Need quick help

    I need to create an error message when a number is divided by 0. How would i go about doing that
    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;
    
    	while (letter == 'y')
    	{ 
    		cout << "Please enter two numbers: ";
    		cin >> num1 >> num2;
    		cout << endl;
    
    		cout << "Please input the operation you would like to use: ";
    		cin >> symbol;
    		cout << endl;
    
    		if (symbol == '+')
    		 {
    			 num3 = num1 + num2;
    		     cout << "The sum is: " << num3 << endl;
    		 }
            else if (symbol == '-')
    		 {  
    		     num3 = num1 - num2;
    		     cout << "The difference is: " << num3 << endl;
    		 }
    		else if (symbol == '*')
    		 {   
    		     num3 = num1 * num2;
    		     cout << "The product is: " << num3 << endl;
    		  }
    		else if (symbol == '/')
    		  {  
    		     num3 = num1 / num2;
    		     num4 = num1 % num2;
    			 cout << "The quiotient is: " << num3 << " The remainder is: " << num4 << endl;
    		  }
    		else
    			cout << "Invalid symbol." << endl;
    	
    		cout << "Would you like to run the program again [y/n] ";
    		cin >> letter;
    		cout << endl;
    	}
            cout << "End of Program" << endl;
    	return 0;
    }

  2. #2
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Code:
    else if (symbol == '/')
    {
    if(num2 == 0)
    cout << "error" << endl;
    I think.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  3. #3
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Well, it would be wrong if 0/10 too, so check both numbers:

    Code:
    else if (symbol == '/')
    {
    if(num2 == 0 || num1==0 )
    cout << "error" << endl;

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    Quote Originally Posted by wildex999
    Well, it would be wrong if 0/10 too, so check both numbers:

    Code:
    else if (symbol == '/')
    {
    if(num2 == 0 || num1==0 )
    cout << "error" << endl;
    umm, 0/10 = 0, and is perfectly legal in mathimatics

    10/0 == ERRRRRRRRRRR
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  5. #5
    Registered User wildex999's Avatar
    Join Date
    Feb 2006
    Posts
    15
    Damn, need to check my math skills again -_-'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM