Solved

cannot divide by zero c++

Posted on 2011-09-23
6
351 Views
Last Modified: 2012-05-12
Hello i'm trying to get my program to work correctly.. the program is very simple all it does is caculate various basic math operations.. i have posted what it is suppose to do
Please enter two integer values separated by whitespace: 5 2
Here are the results:
5 plus 2 equals 7
5 minus 2 equals 3
5 multiplied by 2 equals 10
5 divided by 2 equals 2 with a remainder of 1
But with the second operand having a value of zero, we should see the following:

Please enter two integer values separated by whitespace: 5 0
Here are the results:
5 plus 0 equals 5
5 minus 0 equals 5
5 multiplied by 0 equals 0
Division by zero is undefined...

although my code something is wrong and still passing the zero to the function even though it's suppose to say "Division by zero is undefined..."  i have posted my code below..

#include <iostream>
using namespace std;
void CalcIntResults(int value1, int value2,  int &addition_total, int &minus_total, int &mulplication, int &division, int &reminder);

int main(void)
{
	int number1;
	int number2;

	int addition;
	int minus;
	int mulplication;
	int division;
	int reminder;

cout <<"Please enter two integer values seperated by whitespace: ";
cin >> number1 >>number2;
CalcIntResults(number1, number2, addition, minus, mulplication, division, reminder);

cout <<number1 <<" plus " <<number2 <<" equals " <<addition <<endl;
cout <<number1 <<" minus "<<number2 <<" equal " <<minus <<endl;
cout <<number1 <<" multiplied " <<number2 <<" equals " <<mulplication <<endl;

if(number2!=0)
{
cout <<number1 <<" divided " <<number2 <<" equals " <<division;
}

else 

{
	cout <<"Division by zero is undefined";
}


if((number1 % number2)!=0)
{
	cout <<" reminder of " << reminder;

}
else 
{
	cout <<" no reminder";
}


}
void CalcIntResults(int value1, int value2, int &addition_total, int &minus_total, int &mulplication, int &division, int &reminder)
{
	addition_total = value1+value2;
	minus_total= value1-value2;
	mulplication= value1*value2;

	if(value2 !=0)
	{
		division = value1/value2;
	}
	else
	{
		cout <<"Division by zero is undefined";
	}
	

}

Open in new window

0
Comment
Question by:businessesatoz
  • 5
6 Comments
 
LVL 13

Expert Comment

by:themrrobert
ID: 36589008
try changing line 24 to this:

if(number2!='0')
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36589018
if that doesnt work (which it probably will) try this:

if(number2!="0")

the operators in c++ are extermely sensitive, and a char or string (as returned from stding) will not be cast to an int unless explicitly specified. another thing to try is

if(int<number2>!=0)

/\ this is probably best solution
0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36589029
I see that you are casting it to an int, i'm going to pull out my compiler and check it out

(try my solution just for fun, it may work :) )
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 13

Accepted Solution

by:
themrrobert earned 500 total points
ID: 36589049
I found the problem: use this code


Problem solved B)
if(number2!=0)
{
cout <<number1 <<" divided " <<number2 <<" equals " <<division;
if((number1 % number2)!=0)
{
	cout <<" reminder of " << reminder;

}
else 
{
	cout <<" no reminder";
}
}

else 

{
	cout <<"Division by zero is undefined";
}

Open in new window

0
 
LVL 13

Expert Comment

by:themrrobert
ID: 36589074
Complete code, just to make it easier: (sorry for spamming, my brain is on overload right now )
#include <iostream>
using namespace std;
void CalcIntResults(int value1, int value2,  int &addition_total, int &minus_total, int &mulplication, int &division, int &reminder);

int main(void)
{
	int number1;
	int number2;

	int addition;
	int minus;
	int mulplication;
	int division;
	int reminder;

cout <<"Please enter two integer values seperated by whitespace: ";
cin >> number1 >>number2;
CalcIntResults(number1, number2, addition, minus, mulplication, division, reminder);

cout <<number1 <<" plus " <<number2 <<" equals " <<addition <<endl;
cout <<number1 <<" minus "<<number2 <<" equal " <<minus <<endl;
cout <<number1 <<" multiplied " <<number2 <<" equals " <<mulplication <<endl;

if(number2!=0)
{
	cout <<number1 <<" divided " <<number2 <<" equals " <<division;
	if((number1 % number2)!=0)
	{
		cout <<" reminder of " << reminder;

	}
	else 
	{
		cout <<" no reminder";
	}
}

else 

{
	cout <<"Division by zero is undefined";
}


}
void CalcIntResults(int value1, int value2, int &addition_total, int &minus_total, int &mulplication, int &division, int &reminder)
{
	addition_total = value1+value2;
	minus_total= value1-value2;
	mulplication= value1*value2;

	if(value2 !=0)
	{
		division = value1/value2;
	}
	else
	{
		cout <<"Division by zero is undefined";
	}
	

}

Open in new window

0
 

Author Closing Comment

by:businessesatoz
ID: 36589105
thanks i was very close this one was a bit tricky... thanks !!!!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Problem to ASCII 1 176
Using Diagnostic tools in VS2015: Unresoved allocations 19 84
What is atomic operation? 6 59
If a cell in a range equals "YES" return specific Test. 4 30
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question