answersLogoWhite

0


Best Answer

#include<iostream>

#include<string>

#include<sstream>

unsigned input_num (std::string prompt)

{

unsigned id = 0;

while (1)

{

std::cout<<prompt<<": ";

std::string input="";

getline (std::cin, input);

std::stringstream ss (input);

if (ss>>id)

break;

std::cout<<"Invalid input.\n";

}

return (id);

}

char input_op (std::string ops)

{

char op = 0;

while (1)

{

std::cout<<"Enter an operator ("<<ops<<"): ";

std::string input="";

getline (std::cin, input);

std::stringstream ss (input);

if (ss>>op && ops.find (op) != std::string::npos)

break;

std::cout<<"Invalid input.\n";

}

return (op);

}

int main()

{

unsigned num1 = input_num ("Enter a number");

unsigned num2 = input_num ("Enter another number");

// division is invalid if num2 is zero

char op = input_op (num2?"+-*/":"+-*");

unsigned result = 0;

switch (op)

{

case ('+'): result = num1+num2; break;

case ('-'): result = num1-num2; break;

case ('*'): result = num1*num2; break;

case ('/'): result = num1/num2;

}

std::cout<<num1<<op<<num2<<'='<<result<<std::endl;

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a program in c plus plus to read 2 numbers and an operator then calculate result defined of this operator?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How can user defined operator overloading harm the readability of the program?

Because the built in operator has the precision and compiler knows all the precision between the operators, and it works on that precision. User can also create its own operator but the compiler does not come to know thow to make precision of this operator. Therefore we dont use user defined operator


How a program in c that will add 2 numbers without using any operator?

Not possible. Let's not forget than in C the followings are all operators:+, -+=, -=++, --=&, *, []function-call


How a program in c that will add 2 numbers without using any plus operator?

int main() { int a=10,b=20; while(a--) b++; printf("%d",b); } or: a -= -b;


Program in 'c' to find the LCM of any given five numbers?

Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.Just write a method or function that calculates the LCM for two numbers at a time. Then calculate the LCM for the first two numbers, get the LCM of the result with the third number, etc.


C program to implement arithmetic assignment operator?

y=2x2+3x+1

Related questions

How can user defined operator overloading harm the readability of the program?

Because the built in operator has the precision and compiler knows all the precision between the operators, and it works on that precision. User can also create its own operator but the compiler does not come to know thow to make precision of this operator. Therefore we dont use user defined operator


How do operators provide the necessary data for Java program?

Technically, they don't. Operators only compare values and/or assign new ones. In the case of operator overloading for class objects, the specifics of how a particular operator interacts with class data is defined within the class method.


What is the program to calculate the average of first 'n' natural numbers?

printf ("%g\n", (n+1.0)/2);


C program to find the largest among three numbers using ternary operator?

max = a &gt; b ? a : b; max = max &gt; c ? max : c;


What is c program to calculate product of all even numbers from entered number down to 1?

c is programming laungage


How do you write a c program to find biggest among two numbers using conditional operator?

int max (int a, int b) { return a&gt;b?a:b; }


How a program in c that will add 2 numbers without using any operator?

Not possible. Let's not forget than in C the followings are all operators:+, -+=, -=++, --=&, *, []function-call


C plus plus program calculate the power value of input base and exponent numbers?

cn = c0 *( 1 + i ) pow n


How a program in c that will add 2 numbers without using any plus operator?

int main() { int a=10,b=20; while(a--) b++; printf("%d",b); } or: a -= -b;


How can you download c compiler?

write a c program to fine largest/smallest of 3no (using ?:ternary operator/conditional operator)


How is the word program properly defined?

Program is a noun that is properly defined as a public notice, a plan or system, or a written explanation to be followed. Examples of programs are: a program of regular doctor appointments or, "The program will show the time of the intermission".


What is the order of precedence with regard to the operator used in embedded C program?

Operator precedence in embedded C is exactly the same as in standard C.