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,char**) {
int x,y,z;
char o;
cout<<"Enter an integer number"<<endl;
cin>>x;
cout<<"Enter a C++ aritmetic operator"<<endl;
cin>>o;
if (o!=o){
cout<<"***error: This is not a C++ aritmetic operator"<<endl;
exit(1); }
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; }
system("pause");
}
#include <iostream>
usingnamespace std;
//You forgot argc & argv, the identifiers
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;
//This will never be true, how can (o != o) ???
//What you want is an "else" claus at the end of you "if else-if" chain...
/*
if (o!=o){
cout<<"***error: This is not a C++ aritmetic operator"<<endl;
exit(1); }
*/ //MOVED TO LINE 33
cout<<"Enter an another integer"<<endl;
cin>>y;
if (o=='+'){
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl; }
elseif (o=='-'){
z=x-y;
cout<<x<<"-"<<y<<"="<<z<<endl; }
elseif (o=='*'){
z=x*y;
cout<<x<<"*"<<y<<"="<<z<<endl; }
elseif (o=='/'){
z=x/y;
cout<<x<<"/"<<y<<"="<<z<<endl; }
//NEW
else {
cout<<"***error: This is not a C++ aritmetic operator"<<endl;
exit(1); }
system("pause"); //you really should do this (but there's a whole 'nother thread for that)
}
what about this part
--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.
Also I didn't understand else,where should I put and, should I use (o!=o) ? How can I explain this statement--Print an error message and exit the program if the user enters an invalid operator.