6 Replies - 540 Views - Last Post: 28 January 2013 - 02:44 AM Rate Topic: -----

#1 bigbang10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 bigbang10  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
}




View Postjjl, 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;


Was This Post Helpful? 0
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • 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

Was This Post Helpful? 0
  • +
  • -

#5 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3248
  • View blog
  • Posts: 10,891
  • 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);

Was This Post Helpful? 1
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1