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>
#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>
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.
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.
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
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>
#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
elseif (o=='/')
{
if (x%y==o)
//code for evenly divisible here
else
//error code here
}
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");
}