Program works but when input is invalid it keeps looping c++ - c++

#include "stdafx.h"
#include <iostream>
#include <stdexcept>
using namespace std;
inline double multplication(double num1, double num2);
inline double division(double num1, double num2);
inline double addition(double num1, double num2);
inline double substraction(double num1, double num2);
int main()
{
cout << "Welcome to 'The calculator' just enter a number the operator '*, /, + and -' then the second number enjoy" << endl;
char redo = 'r'; double result = 0; int cnt = 0;
while (redo == 'r' || redo == 'R')
{
try
{
if (cnt > 0)
cout << "Ok same rules as before" << endl;
int num1 = 0, num2 = 0; char inputedOperator = '*';
cin >> num1 >> inputedOperator >> num2;
switch (inputedOperator)
{
case('*') :
result = multplication(num1, num2);
break;
case('/') :
result = division(num1, num2);
break;
case('+') :
result = addition(num1, num2);
break;
case('-') :
result = substraction(num1, num2);
break;
default:
throw runtime_error("Invalid operator");
}
}
catch (runtime_error err)
{
cout << err.what() << endl;
cout << "Enter y to redo or anykey to exit" << endl;
cin >> redo;
++cnt;
continue;
}
cout << "The answer is " << result << "\nPlease enter y to redo or anykey to exit" << endl;
cin >> redo;
++cnt;
}
cout << "Thanks for using" << endl;
return 0;
}
inline double multplication(double num1, double num2)
{
return num1 * num2;
}
inline double division(double num1, double num2)
{
return num1 / num2;
}
inline double addition(double num1, double num2)
{
return num1 + num2;
}
inline double substraction(double num1, double num2)
{
return num1 - num2;
}
Whenever I run '3 * 2' as the input it works fine but for '3 * d' it keeps looping at the end of my code also '3 d 3' is fine since it just sais invalid operator. So my question is why does it keep looping and how do I fix it?

added
if (cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<int>::max(), '\n');
}
To clear the invalid input and buffer. Found info on cplusplus.com/forum/beginner/2957/

Related

How to make a calculator in c++ using if else and functions?

I am a new contributor and here I am trying to make a simple calculator but having errors in my code. When I compile the code I get:
Error: C:\Users\IJLAL\Documents\collect2.exe [Error] ld returned 1 exit status while compiling
In case it could help, here is the screen shot of error when I pressed compile button or F11 in Dev C++:
Here is my code:
#include<iostream>
using namespace std;
void fun(float a, float b);
int main()
{
float a, b, sum, sub, mul, divide, mod;
char op;
//operands and operators are enterd by the user
cout<<"Enter any two operands with operator=";
cin>>a>>op>>b;
fun(a, b);
return 0;
}
void op(float a, float b)
{
if(a+b)
{
float sum=a+b;
cout<<"\nAddition of two numbers is="<<sum;
}
else if(a-b)
{
float sub=a-b;
cout<<"\nSubtraction of two numbers is="<<sub;
}
else if(a*b)
{
float mul=a*b;
cout<<"\nMultiplication of two numbers is="<<mul;
}
else if(a/b)
{
float divide=a/b;
cout<<"\nDivision of two number is="<<divide;
}
else
{
cout<<"\nInvalid operator.......";
}
}
Please tell me the solution of this problem, so that I can compile the code successfully. If there is any better solution to make a simple calculator on beginner level please mention it in answer.
You're not so far from a result. The problem is that you have not defined the function fun(). Furthermore, in the function op() that you have defined, you do not use the operator of the input.
So first thing to do is to change the signature of the function:
void fun(char op, float a, float b);
Then you need to invoke your function in main(), passing also the operation that was requested by the user:
fun(op, a, b);
Finally you need to change all your if to check if op is the matching operator:
void fun(char op, float a, float b)
{
if(op=='+')
{
...
}
else if(op=='-')
{
...
You should then get the expected result.
Online demo
Aditional infos
if (a+b) just calculates the expression using the two values of the user, and if it's non zero, it's considered as true.
once you got this program to work, you can look for the switch statement
You can use additional functions to make a better calculator. You can use this code. Hope this code will be helpful for you.
The header <iomanip> is part of the Input/output library of the C++ Standard Library and <math.h> is used when we perform mathematical operations.
#include<iostream>
#include<conio.h>
#include<math.h>
#include<iomanip>
char op;
using namespace std;
void sum()
{
int sum = 0;
int n;
int numberitems;
cout << "Enter number of items: \n";
cin >> numberitems;
for(int i=0;i<numberitems;i++)
{
cout<< "Enter number "<<i<<":\n\n" ;
cin>>n;
sum+=n;
}
cout<<"sum is: "<< sum<<endl<<endl;
}
void diff()
{
int diff;
int n1,n2;
cout<<"enter two numbers to find their difference:\n\n";
cout<<"enter first number:";
cin>>n1;
cout<<"\nenter second number:";
cin>>n2;
diff=n1-n2;
cout<<"\ndifference is:"<<diff<<endl<<endl;
}
void pro()
{
int pro=1;
int n;
int numberitems;
cout<<"enter number of items:\n";
cin>>numberitems;
for(int i=0;i<=numberitems;i++)
{
cout<<"\nenter item "<<i<<":";
cin>>n;
pro*=n;
}
cout<<"product is:"<<pro<<endl<<endl;
}
void div()
{
int div;
int n1;
int n2;
cout<<"enter 2 numbers to find their quotient\n\n";
cout<<"enter numerator:";
cin>>n1;
cout<<"\nenter denominator:";
cin>>n2;
div=n1/n2;
cout<<"\nquotient is:"<<div<<endl<<endl;
}
void power()
{
long int p;
int res=1,n;
cout<<"enter number:";
cin>>n;
cout<<"\nenter power:";
cin>>p;
for(int i=1;i<=p;i++)
{
res=n*res;
}
cout<<n<<"\n power "<<p<<" is :"<<res<<endl;
}
void sq()
{
float s;
int n;
cout<<"enter number to find its square root:";
cin>>n;
s=sqrt(n);
cout<<"\nsquare root of "<<n<<" is :"<<s<<endl;
}
void fact()
{
long int f=1;
int c=1,n;
cout<<"enter number to find its factorial:";
cin>>n;
while(c<=n)
{
f=f*c;
c+=1;
}
cout<<"\nfactorial of "<<n<<" is :"<<f<<endl;
}
void expo()
{
long double res=1,p;
double e=2.718281828;
cout<<"enter power of exponential function:";
cin>>p;
for(int i=1;i<=p;i++)
{
res=e*res;
}
cout<<" e^ "<<p<<" is :"<<res<<endl;
}
int main()
{
system("cls");
do
{
system("pause");
system("cls");
cout<<"***which operation you want to perform***\n";
cout<<"press 0 for exit\n";
cout<<"press 1 for addition \n";
cout<<"press 2 for subtraction\n";
cout<<"press 3 for multiplication\n";
cout<<"press 4 for division\n";
cout<<"press 5 for power calculation\n";
cout<<"press 6 for square root \n";
cout<<"press 7 for factorial calculation\n";
cout<<"press 8 for exponential calculation\n";
cout<<"press option:";
cin>>op;
switch(op)
{
case '1':
sum();
break;
case '2':
diff();
break;
case '3':
pro();
break;
case '4':
div();
break;
case '5':
power();
break;
case '6':
sq();
break;
case '7':
fact();
break;
case '8':
expo();
break;
case '0':
exit(0);
default:
cout<<"invalid input" ;
system("cls");
}
}
while(op!='0');
getch();
}
Hey I'm still a newbie on c++ but hope this would help, I used while loop to loop the calculator but I don't have a good error handler for example when users try to input a letter instead of numbers.
#include <iostream>
#include <cmath>
using namespace std;
int result (int a, int b, char op);
int main()
{
char optr, choice;
int nr1, nr2;
while (true){
cout << "Enter first number: ";
cin >> nr1;
cout << "Enter an operator (+ - / * %) : ";
cin >> optr;
cout << "Enter second number: ";
cin >> nr2;
result (nr1, nr2, optr);
cout<<"Would you like to perform other calculation?(Y/N): ";
cin >> choice;
if (choice =='N'||choice =='n'){
break;
}
}
}
int result (int a, int b, char op)
{
int result;
if (op == '+'){
result = a + b;
cout << "Result to " << a << " + " << b << " = " << result << endl;
} else if (op == '-'){
result = a - b;
cout << "Result to " << a << " + " << b << " = " << result << endl;
} else if (op == '*'){
result = a * b;
cout << "Result to " << a << " * " << b << " = " << result << endl;
} else if (op == '/'){
result = a / b;
a / b;
cout << "Result to " << a << " / " << b << " = " << result << endl;
} else if (op == '%'){
result = a % b;
cout << "Remainder to " << a << " % " << b << " = " << result << endl;
} else {
cout <<"Error 404: " << a << op << b <<" Wrong input format. Program terminated." << endl;
// i still dont know how to properly use error handling
}
}
I won't criticize your solution as I have a better solution as you mentioned "please mention a better solution in answer"
Here's my solution with comments to make you understand what each statement does.
#include <iostream>
using namespace std;
// I am going to show How to Design a program
// We have to break down our progress a bit by bit
// and do the progress of one thing with function
// For calculator we have to take input from user keyboard twice,
// And an opperator from user
int userInput() // this requires to get input from user keyboard
{
cout << "Enter a number: ";
int no{};
cin >> no;
return no;
}
char userOpr() // Using type char to store the ASCI Opperator means requires char to store +,*,/,-
{
cout << "Enter opperator: ";
char opr{};
cin >> opr;
return opr;
}
int calculate(int input1, char opper, int input2)
{
if (opper == '+')
{
return input1 + input2;
}
else if (opper == '-')
{
return input1 - input2;
}
else if (opper == '*')
{
return input1 * input2;
}
else if (opper == '/')
{
return input1 / input2;
}
return 0;
}
int main()
{
// get the first no. from user
// getUserInput();
// get the math oppperator from the user
// getMathOpperator();
// get the second no. from the user
// getUserInput();
// calculate the values
// calculateResults();
// print out the results
// printResults();
cout << "Hello This is a simple calculator program Designed by Shankhui!\n\n";
while (true)
{
int input1{ userInput() };
int input2{ userInput() };
char opper{ userOpr() }; // Using type char to store the ASCI Opperator means requires char to store +,*,/,-
cout << input1 << " " << opper << " " << input2 << " = " << calculate(input1, opper, input2) << '\n' << '\n';
}
return 0;
}
Just copy this code and modify however you want to.
#include <iostream>
int main() {
int math_ques_1, math_ques_2;
std::cout << "What is the base of the question?" << std::endl;
std::cin >> math_ques_1;
std::cout << "What do you want to add to the base?" << std::endl;
std::cin >> math_ques_2;
std::cout << "The answer is " << math_ques_1 + math_ques_2 << "." << std::endl; }

Postfix Notation Calculator

I'm trying to code a postfix calculator, but I keep running into two issues-
first: When the calculator encounters a space, it sort of just exits immediately
second: when it encounters a non operator/non digit (ie- z) it doesn't display the error message that I coded.
int main()
{
stack <int> calcStack;
string exp;
char ans;
cout << "\nDo you want to use the calculator?" << endl;
cin >> ans;
while (ans == 'y')
{
cout << "\nEnter your exp" << endl;
cin >> exp;
for (int i = 0; i < exp.size(); i++)
{
if (isspace(exp[i]))
{
}
else if (isdigit(exp[i]))
{
int num = exp[i] - '0';
calcStack.push(num);
}
else
doOp(exp[i], calcStack);
}
while (!calcStack.empty())
{
calcStack.pop();
}
cout << "\nDo you want to use the calculator again?" << endl;
cin >> ans;
}
system("pause");
return 0;
}
This is the function--
void doOp(const char & e, stack <int>& myS)
{
if (myS.size() == 2)
{
int num1, num2, answ;
num2 = myS.top();
myS.pop();
num1 = myS.top();
myS.pop();
if (e == '+')
answ = num1 + num2;
else if (e == '-')
answ = num1 - num2;
else if (e == '*')
answ = num1 * num2;
else if (e == '/')
answ = num1 / num2;
else if (e == '%')
answ = num1 % num2;
else
cout << "\nError- Invalid operator" << endl;
cout << "\nCalculating..." << endl << answ << endl;
myS.push(answ);
}
else
cout << "\nInvalid stack size- too few, or too many" << endl;
}
In your main loop, you're reading strings with the string extractor:
cin >> exp;
THe string extractor is space sensitive. So as soon as a space char is encountered in the input, the string reading stops, and the witespace is not included in exp.
If you want to get a full line including spaces, you should opt for:
getline (cin, exp);
Edit:
The issue you experience with getline() is realted to the char extraction when you ask if user wants to use the calculator. Entering y is not sufficient. So you'l enter yenter. Only the y will be put into ans, so that getline() will start reading an empty line.
To solve this, update your initial input:
cin >> ans; // as before
cin.ignore (INT_MAX, '\n'); // add this to skip everything until newline included
Here an online demo showing that it works (including error message in case of wrong operator)

Enter two integer and display the largest number using predefined function

I already type the function but I can't get the output. Can anyone help me. It must use predefined function. My program is running but it have some errors so please help me.
#include <iostream>
using namespace std;
float largeNum (float a, float b)
{
float largeNum;
if(a>b)
cout<<"a is larger";
if(b>a)
cout<<"b is larger";
return (largeNum);
}
int main()
{
float num1, num2;
cout<<"Enter number";
cin>>num1;
cout<<"Enter number";
cin>>num2;
cout<<largeNum<< "is larger"<<endl;
return 0;
}
#include <iostream>
using namespace std;
float largeNum (float a, float b)
{
if(a>b)
return a;
return b;
}
int main()
{
float num1, num2;
cout<<"Enter number";
cin>>num1;
cout<<"Enter number";
cin>>num2;
cout<<largeNum(num1, num2)<< "is larger"<<endl;
return 0;
}
Try to change your code to below. You have not given any thing to largeNum, so you cannot return it.
#include <iostream>
using namespace std;
float largeNum(float a, float b)
{
if (a > b)
cout << "a is larger";
if (b > a)
cout << "b is larger";
return 0;
}
int main()
{
float num1, num2;
cout << "Enter number";
cin >> num1;
cout << "Enter number";
cin >> num2;
largeNum(num1, num2);
getchar();
getchar();
return 0;
}
#include <iostream>
using namespace std;
void largeNum(float a, float b)
{
if (a > b)
{
cout << a << " larger";
}
if (b > a)
{
cout << b << " larger";
}
}
int main()
{
float num1, num2;
cout << "Enter number";
cin >> num1;
cout << "Enter number";
cin >> num2;
largeNum(num1,num2);
return 0;
}
in your code you just define the function but you are not calling this function main so thats why u get error

Creating a calculator c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My current code is:
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
char cAgain, type;
int x, y=0;
double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2),
multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;
do
{
cout << "How many operations would you like to do?" << endl;
cin >> x;
if (x <= 0)
{
cout << "Error: You must have 1 or more operations." << endl;
}
else
{
while (x != 0, x--)
{
y++;
cout << "Enter in your "<<y<< " operation. (First Number (+,-,*,/,^) Second Number)" << endl;
cin >> num1 >> type >> num2;
switch (type)
{
case '+':
total = addition(num1, num2);
cout << num1 << " + " << num2 << "= " << total<< endl;
break;
case'-':
total = subtraction(num1, num2);
cout << num1 << " - " << num2 << "= " << total<< endl;
break;
case'/':
total = division(num1, num2);
cout << num1 << " / " << num2 << "= " << total<< endl;
break;
case'*':
total = multiplication(num1, num2);
cout << num1 << " * " << num2 << "= " << total << endl;
break;
case'^':
total = exponential(num1, num2);
cout << num1 << " ^ " << num2 << "= " << total << endl;
break;
}
}
}
cout << "Would you like to run the program again.(Y/N)" << endl;
cin >> cAgain;
} while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
double addition(double num1, double num2)
{
double total;
total = num1 + num2;
return (total);
}
double subtraction(double num1, double num2)
{
double total;
total = num1 - num2;
return (total);
}
double division(double num1, double num2)
{
double total;
total = num1 / num2;
return (total);
}
double multiplication(double num1, double num2)
{
double total;
total = num1 * num2;
return (total);
}
double exponential(double num1, double num2)
{
double total;
total = pow(num1,num2);
return (total);
}
Currently the code works as it is suppose too.
It starts by asking
How many operations would you like to do?
You enter a number say 3
Then it ask you to enter in your operation
5+5
10
Then it ask for second
10^2
100
Then it ask for third
100-10
90
Then it ask if you want to start over and run the program again.
What I am trying to change it too is to 5+5^2-10= and it give me 20.
Essentially I want the user to be able to enter in the entire operation like 5+5^2-10= and it give me the correct answer. Allowing the user to type in a function and use '=' and return to start the calculation.
You can use following technique to solve an expression :
#include<iostream>
#include<cmath>
#include<stdio.h>
#include<cstdlib>
#include<map>
#include<stack>
using namespace std;
int expression_value(string str)
{
map<char,int>priority;
priority['^']=3;
priority['*']=2,priority['/']=2;
priority['+']=1,priority['-']=1;
stack<char>op_stack;
stack<int>val_stack;
int val=0;
for(int i=0;str[i];i++)
{
if(str[i]>='0'&&str[i]<='9')
val=val*10+str[i]-'0';
else
{
if(op_stack.empty()) // first operator
{
val_stack.push(val);
op_stack.push(str[i]);
}
else if(priority[op_stack.top()] < priority[str[i]]) // current operator is more prior then previous operator. so push it to stack.
{
val_stack.push(val);
op_stack.push(str[i]);
}
else // current operator is less prior then previous operator. so calculate previous operators resultant value
{
int num1,num2;
num1=val_stack.top(); val_stack.pop();
num2=val;
if(op_stack.top()=='+')
//val_stack.push(addition(num1, num2));
val_stack.push(num1 + num2);
else if(op_stack.top()=='-')
//val_stack.push(subtraction(num1, num2));
val_stack.push(num1 - num2);
else if(op_stack.top()=='*')
//val_stack.push(multiplication(num1, num2));
val_stack.push(num1 * num2);
else if(op_stack.top()=='/')
//val_stack.push(division(num1, num2));
val_stack.push(num1 / num2);
else
//val_stack.push(exponential(num1, num2));
val_stack.push(pow(num1 , num2));
op_stack.pop(); // as operator's value calculation done, pop it from the stack
op_stack.push(str[i]); // push the new operator
}
val=0;
}
}
val_stack.push(val); // last value
// calculate remaining operators value
while(!op_stack.empty())
{
int num1,num2;
num2=val_stack.top(); val_stack.pop();
num1=val_stack.top(); val_stack.pop();
if(op_stack.top()=='+')
//val_stack.push(addition(num1, num2));
val_stack.push(num1 + num2);
else if(op_stack.top()=='-')
//val_stack.push(subtraction(num1, num2));
val_stack.push(num1 - num2);
else if(op_stack.top()=='*')
//val_stack.push(multiplication(num1, num2));
val_stack.push(num1 * num2);
else if(op_stack.top()=='/')
//val_stack.push(division(num1, num2));
val_stack.push(num1 / num2);
else
//val_stack.push(exponential(num1, num2));
val_stack.push(pow(num1 , num2));
op_stack.pop();
}
return val_stack.top();
}
Here you have to pass the whole expression as string.
Operators will be stored in op_stack and value in val_stack
You should implement a
binary expression tree
I have just modify your program to meet your requirement:
#include <iostream>
#include <cstdio>
using namespace std;
char op;
double multiplication()
{
double product;
cin>>product;
while(1)
{
cin>>op;
if(op == '*')
{
double temp;
cin>>temp;
product *= temp;
}
else if(op == '/')
{
double temp;
cin>>temp;
product /= temp;
}
else break;
}
return product;
}
double addition()
{
double sum = multiplication();
while(op != '=')
{
if(op == '+')
sum += multiplication();
else if(op == '-')
sum -= multiplication();
else
{
cout<<"Operator error"<<endl;
return 0.0;
}
}
return sum;
}
int main()
{
char cAgain;
int x;
do
{
cout << "How many operations would you like to do?" << endl;
cin >> x;
if (x <= 0)
{
cout << "Error: You must have 1 or more operations." << endl;
}
else
{
while (x != 0, x--)
{
double result = addition();
cout<<result<<endl;
}
}
cout << "Would you like to run the program again.(Y/N)" << endl;
cin >> cAgain;
} while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
Calculate some expressions like a+b,a-b,a^b,a*b etc is simple. But if you want to solve expressions like this:5+5^2-10,I think you need to learn something about Reverse Polish notation . It is not very difficult . Good Luck.

Order numbers in c++ and recognize the repeated ones

I was trying to make a program in c++ that should order 3 random numbers (the user will write them) and then print which is the higher and the lower, but if two numbers or more are equal should print it.
New code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all numbers are equal";
}
else if (num1 == num2){
cout << "num1 and num2 are equal";
}
else if (num2 == num3){
cout << "num2 and num3 are equal";
}
else if(num3 == num1){
cout << "num1 and num3 are equal";
}
else{
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "higher is num1";
}
else if(num2 > num1 && num2 > num3){
cout << "higher is num2";
}
else if(num3 > num1 && num3 > num2){
cout << "higher is num3";
}
}
}
return 0;
}
New problem:
The programm needs to know which is the lowest too, so how can I do that?
Old code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all your numbers are equal";
}
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "num1";
}
else if(num2 > num1 && num2 > num3){
cout << "num2";
}
else /*(num3 > num1 && num3 > num2)*/{//Here I tried to use and else if
cout << "num3";
}
}
return 0;
}
Old problem
This code is all wrong, but I don't know what I'm doing wrong, please help me.
And I have a last question, do I have a limit of if's into a if sentence? or I just can't write two else if or..? Thanks.
#include <iostream>
#include <set>
int main()
{
std::set<int> numbers;
int input;
for (int i=1; i<=3; ++i) {
std::cout << "Enter number " << i << ": ";
std::cin >> input;
numbers.insert(input);
}
if (numbers.size() < 3) {
std::cout << "You entered the same number more than once, silly!" << std::endl;
std::cout << "Nevertheless, ";
}
std::cout << "the maximum number is " << *numbers.rbegin() << std::endl;
}
The important feature of this code is that it takes advantage of the properties of std::set which are:
It keeps all its elements in sorted order
It does not allow duplicate elements
std::set::rbegin is used to get the last element, which is the largest number (because the numbers are automatically sorted).
This code does not display which numbers are the largest and smallest, but this can easily be added. For example, std::set::insert returns information that can allow you to determine which insertion failed in the set. An std::set can contain only single copies of its contained objects. Therefore, if the user enters a number more than once the calls to insert will fail.
The following piece of code solves your purpose. It has three functions findLargest, findSmallest and checkEqual. This is a very basic program and can be modified to your needs on how to display and what to return etc etc.
#include <iostream>
using namespace std;
void findLargest(int n1,int n2,int n3)
{
if(n1>n2 && n1>n3)
{
cout<<"Largest number is :"<<n1;
cout<<"\n";
}
else if((n2>n1) && (n2>n3))
{
cout<<"Largest number is :"<<n2;
cout<<"\n";
}
else
{
cout<<"Largest number is :"<<n3;
cout<<"\n";
}
}
void findSmallest(int n1,int n2,int n3)
{
if(n1<=n2 && n1<=n3)
{
cout<<"Smallest number is :"<<n1;
cout<<"\n";
}
else if((n2<=n1) && (n2<=n3))
{
cout<<"Smallest number is :"<<n2;
cout<<"\n";
}
else
{
cout<<"Smallest number is :"<<n3;
cout<<"\n";
}
}
int checkEqual(int n1,int n2,int n3)
{
if(n1==n2 && n2==n3 && n3==n1)
{
cout<<"All three are equal";
cout<<"\n";
}
else if(n1==n2||n2==n3||n3==n1)
{
cout<<"Two numbers are equal";
cout<<"\n";
}
else
{
cout<<"None are equal.. Finding Largest and Smallest....!!";
cout<<"\n";
}
}
int main() {
int num1,num2,num3;
cout<<"Enter the numbers";
cin>>num1>>num2>>num3;
cout<<"\n";
checkEqual(num1,num2,num3);
findSmallest(num1,num2,num3);
findLargest(num1,num2,num3);
return 0;
}
Ideone link: http://ideone.com/hz4keQ
Hope it helps. :)