# Formatting / Dividing by 0 (My first Calculator)

Page 1 of 1

## 6 Replies - 2499 Views - Last Post: 28 January 2013 - 02:44 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309559&amp;s=e87764db977700e51e28e0a3e2125a4e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 bigbang10

Reputation: 0
• Posts: 2
• Joined: 26-January 13

# Formatting / Dividing by 0 (My first Calculator)

Posted 26 January 2013 - 09:14 PM

I put parameters to not allow for characters.. but formatting goes out of whack. Try putting a letter as an integer. Need help with getting it to look clean.

Also, I also put parameters so it cannot divide by 0 based on earlier feedback. It says enter a new integer then it crashes!

```#include <iostream>
using namespace std;

int sum(int iValue1, int iValue2) {
return iValue1 + iValue2;
}

int subtraction (int iValue1, int iValue2) {
return iValue1 - iValue2;
}

int multiply (int iValue1, int iValue2) {
return iValue1 * iValue2;
}

int division (int iValue1, int iValue2) {
return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter the initial value as an integer:";
cin >> iValue1;
do {

cout << "Enter the next value as an integer:";
cin >> iValue2;
if (cin.fail())
cout << "No integer found. Please use a number" << endl;
cin.clear();

cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;

if (chOperator == '+') {
cout << "The result is " << sum(iValue1, iValue2) << endl;
iValue1 = sum(iValue1, iValue2);
}

else if (chOperator == '-') {
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
iValue1 = subtraction(iValue1, iValue2);
}

else if (chOperator == '*') {
cout << "The result is " << multiply(iValue1, iValue2) << endl;
iValue1 = multiply(iValue1, iValue2);
}

else if (chOperator == '/') {
if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
cin.clear();
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);
}

} while (iValue1 !=0);
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Formatting / Dividing by 0 (My first Calculator)

### #2 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Formatting / Dividing by 0 (My first Calculator)

Posted 26 January 2013 - 09:29 PM

All you did was tell the user that they are dividing by zero, however you don't prevent it.

```if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
cin.clear();
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);

```

An if-else structure will branch execution into two paths (like a fork in the road). After you prompt the error message, you need to NOT actually divide by zero.

```if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
else
cout << "The result is " << division(iValue1, iValue2) << endl;

```

This post has been edited by jjl: 26 January 2013 - 09:29 PM

### #3 bigbang10

Reputation: 0
• Posts: 2
• Joined: 26-January 13

## Re: Formatting / Dividing by 0 (My first Calculator)

Posted 26 January 2013 - 09:41 PM

I see what happened now. I updated the code but its still not taking. Been at this for a while and am kind of burnt I think.

```#include <iostream>
using namespace std;

int sum(int iValue1, int iValue2) {
return iValue1 + iValue2;
}

int subtraction (int iValue1, int iValue2) {
return iValue1 - iValue2;
}

int multiply (int iValue1, int iValue2) {
return iValue1 * iValue2;
}

int division (int iValue1, int iValue2) {
return iValue1 / iValue2;
}

int main () {
int iValue1;
int iValue2;
char chOperator;

cout << "Enter the initial value as an integer:";
cin >> iValue1;
do {

cout << "Enter the next value as an integer:";
cin >> iValue2;
if (cin.fail())
cout << "No integer found. Please use a number" << endl;
cin.clear();

cout << "Enter the operation you want to perform (-,+,/,*):";
cin >> chOperator;

if (chOperator == '+') {
cout << "The result is " << sum(iValue1, iValue2) << endl;
iValue1 = sum(iValue1, iValue2);
}

else if (chOperator == '-') {
cout << "The result is " << subtraction(iValue1, iValue2) << endl;
iValue1 = subtraction(iValue1, iValue2);
}

else if (chOperator == '*') {
cout << "The result is " << multiply(iValue1, iValue2) << endl;
iValue1 = multiply(iValue1, iValue2);
}

else if (chOperator == '/') {
if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
else
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);
}

} while (iValue1 !=0);
return 0;
}

```

jjl, on 26 January 2013 - 09:29 PM, said:

All you did was tell the user that they are dividing by zero, however you don't prevent it.

```if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
cin.clear();
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);

```

An if-else structure will branch execution into two paths (like a fork in the road). After you prompt the error message, you need to NOT actually divide by zero.

```if (iValue2 == 0)
cout << "You cannot divide by 0. Enter another integer ";
else
cout << "The result is " << division(iValue1, iValue2) << endl;

```

### #4 jjl

• Engineer

Reputation: 1270
• Posts: 4,998
• Joined: 09-June 09

## Re: Formatting / Dividing by 0 (My first Calculator)

Posted 26 January 2013 - 09:57 PM

Here is an example of validating integer input

```#include <iostream>
#include <limits>

int main() {
int x;

for(;;)/> {
std::cout<<"Enter integer: ";
if(std::cin>>x)
break;
else {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}

return 0;
}

```

This post has been edited by jjl: 26 January 2013 - 09:57 PM

### #5 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Formatting / Dividing by 0 (My first Calculator)

Posted 27 January 2013 - 11:03 PM

Well, I have a small suggestion here. If you are aware of switch control, then I would suggest you to use that here in the place of the if- else if like this.

```cout << "Enter the operation you want to perform (-,+,/,*):";
36	    cin >> chOperator;
37
switch(chOperator)
{
38	         Case '+' :
39	           cout << "The result is " << sum(iValue1, iValue2) << endl;
40	           iValue1 = sum(iValue1, iValue2);
41	          break;
42
43	         Case '-' :
44	           cout << "The result is " << subtraction(iValue1, iValue2) << endl;
45	           iValue1 = subtraction(iValue1, iValue2);
46	          break;
47
48	         Case '*' :
49	           cout << "The result is " << multiply(iValue1, iValue2) << endl;
50	           iValue1 = multiply(iValue1, iValue2);
51	          break;
52
53	         Case '/' :
54	           if (iValue2 == 0)
55	            cout << "You cannot divide by 0. Enter another integer ";
56	           else
57	            cout << "The result is " << division(iValue1, iValue2) << endl;
58	            iValue1 = division(iValue1, iValue2);
59	          break;

default : cout<<"Invalid Character. Please enter again";
}
```

regards,
Raghav

### #6 andrewsw

• RequestedRangeNotSatisfiable

Reputation: 6552
• Posts: 26,563
• Joined: 12-December 12

## Re: Formatting / Dividing by 0 (My first Calculator)

Posted 28 January 2013 - 02:26 AM

You are still dividing by zero:

```else
cout << "The result is " << division(iValue1, iValue2) << endl;
// the following line still will divide by zero
iValue1 = division(iValue1, iValue2);
```

### #7 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Formatting / Dividing by 0 (My first Calculator)

Posted 28 January 2013 - 02:44 AM

Thank you for that andrewsw. I guess a few braces would do the trick.

```else
{
cout << "The result is " << division(iValue1, iValue2) << endl;
iValue1 = division(iValue1, iValue2);
}
break;
```

regards,
Raghav