Aritmetic operators-- I need 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)
closed account (9N7koG1T)
#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");
}
So what have you done so far?
closed account (9N7koG1T)
I tried to write but I'm not sure that is true,,I have a problem with if statement. I thınk there is some missing part,, please help.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
using namespace 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; }
	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; }
	//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)
} 
closed account (9N7koG1T)
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.

Topic archived. No new replies allowed.