I couldn't write some part,please help.

closed account (9N7koG1T)
Prompt the user to enter an integer number.

Prompt the user to enter a C++ arithmetic operator (i.e. + - / * %). Print an error message and exit the program if the user enters an invalid operator.

Prompt the user to enter another integer number. If the arithmetic operator is divide, then print an error message and exit the program if this integer input is 0.

Print the result obtained when the arithmetic operator is applied to the entered numbers. If the result is a quotient, then print a warning message that integer arithmetic was executed.
Example Outputs

The angle brackets are a notation used to represent user inputs. For example, <8> implies the user entered an 8.
enter an integer: <3>
enter arithmetic operator: <+>
enter an integer: <5>

3 + 5 = 8

enter an integer: <2>
enter arithmetic operator: <*>
enter an integer: <7>

2 * 7 = 14

enter an integer: <7>
enter arithmetic operator: </>
enter an integer: <0>

*** error: cannot divide by zero

enter an integer: <5>
enter arithmetic operator: </>
enter an integer: <2>

5 / 2 = 2 (warning: integer division executed)
******************************************************************

#include <iostream>
using namespace std;
int main(int argc,char**argv) {
int x,y,z;
char o;
cout<<"Enter an integer number"<<endl;
cin>>x;
cout<<"Enter a C++ aritmetic operator"<<endl;
cin>>o;
cout<<"Enter an another integer"<<endl;
cin>>y;
if (o=='+'){
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl; }
else if (o=='-'){
z=x-y;
cout<<x<<"-"<<y<<"="<<z<<endl; }
else if (o=='*'){
z=x*y;
cout<<x<<"*"<<y<<"="<<z<<endl; }
else if (o=='/'){
z=x/y;
cout<<x<<"/"<<y<<"="<<z<<endl;}
else {
cout<<"***error: This is not a C++ aritmetic operator"<<endl;
exit(1); }



system("pause");
}
*****************************************************************
I couldn't write these two part
1-) If the arithmetic operator is divide, then print an error message and exit the program if this integer input is 0.
2-)Print the result obtained when the arithmetic operator is applied to the entered numbers. If the result is a quotient, then print a warning message that integer arithmetic was executed.
Like,
enter an integer: <5>
enter arithmetic operator: </>
enter an integer: <2>

5 / 2 = 2 (warning: integer division executed)
*********************************************************************
Using the same conditional logic you have used to get to this point, before you do the arithmetic, check if the denominator is 0 and handle your error message, otherwise continue business as usual.
closed account (9N7koG1T)
can you type these two parts please? I got this;
...
..
.
else if (y==0){
cout<<"*** error: cannot divide by zero"<<endl;
exit(1); }
else if (o=='/'){
z=x/y;
cout<<x<<"/"<<y<<"="<<z<<endl;}
..
.
what is other?--If the result is a quotient, then print a warning message that integer arithmetic was executed.
closed account (9N7koG1T)
any help? I need these part..
If there was a remainder then give a warning.
closed account (9N7koG1T)
where am I supposed to put? and put where? can you type only that part?
Remainder is % so

1
2
3
4
5
6
7
8
9
double x, temp;
cout << "Enter a number" << endl;
cin >> x

temp = x%10 // if there is a remainder after divided by 10, temp holds that value
if (temp==0) // if there is no remainder
    //some code
else // if there IS a remainder
    //some code 


Hope that helps!
closed account (9N7koG1T)
I already know this part, I couldn't figure out that part;
If the result is a quotient, then print a warning message that integer arithmetic was executed.
like;
enter an integer: <5>
enter arithmetic operator: </>
enter an integer: <2>
closed account (9N7koG1T)
The last code is...

#include <iostream>
using namespace std;
int main(int argc,char**argv) {
int x,y,z;
char o;
cout<<"Enter an integer number"<<endl;
cin>>x;
cout<<"Enter a C++ aritmetic operator"<<endl;
cin>>o;
cout<<"Enter an another integer number"<<endl;
cin>>y;
if (o=='+'){
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl; }
else if (o=='-'){
z=x-y;
cout<<x<<"-"<<y<<"="<<z<<endl; }
else if (o=='*'){
z=x*y;
cout<<x<<"*"<<y<<"="<<z<<endl; }
else if (y==0){
cout<<"*** error: cannot divide by zero"<<endl;
exit(1); }
else if (o=='/'){
z=x/y;
cout<<x<<"/"<<y<<"="<<z<<endl;}
else {
cout<<"***error: This is not a C++ aritmetic operator"<<endl;
exit(1); }
system("pause");
}
You still are missing the error if there is a remainder. I suggest using modulus (%, used for remainder) in an if statement to see if there is a remainder, example:
1
2
3
4
if (x%y==o)
//code for evenly divisible here
else
//error code here 

I suggest putting them in the division block of code like this:
1
2
3
4
5
6
7
else if (o=='/')
{
if (x%y==o)
//code for evenly divisible here
else
//error code here 
}

Make sense?
BR8N03epsilon, I suggest using actual zeros '0' instead of letter 'o'
;)
closed account (9N7koG1T)
I found the answer, the final and right answer code is..

#include <iostream>
using namespace std;
int main(int ,char**) {
int x,y,z;
char o;
cout<<"Enter an integer number"<<endl;
cin>>x;
cout<<"Enter a C++ aritmetic operator"<<endl;
cin>>o;
cout<<"Enter an another integer number"<<endl;
cin>>y;
if (o=='+'){
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl; }
else if (o=='-'){
z=x-y;
cout<<x<<"-"<<y<<"="<<z<<endl; }
else if (o=='*'){
z=x*y;
cout<<x<<"*"<<y<<"="<<z<<endl; }
else if (y==0){
cout<<"*** error: cannot divide by zero"<<endl;
exit(1); }
else if (o=='/'){
if( x % y)
cout <<"(warning: integer division executed)"<<endl;
z=x/y;
cout<<x<<"/"<<y<<"="<<z<<endl;}
else {
cout<<"***error: This is not a C++ aritmetic operator"<<endl;
exit(1); }
system("pause");
}
L B, sorry, Syntax Soviet (play off of Grammar Nazi). I saw that the char was o, so I got confused and used that.
Oh I see, it is confusing. @OP: I suggest using "op" short for "operator" rather than the confusing "o" for 'op'
Topic archived. No new replies allowed.