I'm trying to make a program that is an equivalent of a four function calculator for two fractions.. The Error comes up in each of the switch cases within the cout statements. The Error is:
[Error] invalid operands of types 'float' and 'const char [2]' to binary 'operator<<'
I tried googling, nothing found but I did find the same error but with int instead of float. Unfortunately, he was making a clear mistake, he wasn't even using cout statement and putting << operator in the end. Also, I tried adding more parentheses to specify which athematic operations to perform first but it had no effect.
#include <iostream>
#include <conio.h>
using namespace std;
main()
{ //This Program is an equivalent of a four function calculator for fraction
float N1, D1, N2, D2;
char OP, slash;
system("cls");
cout<<"Hint: Enter Both Fractions with Operator in between; 1/2 + 6/3";
cout<<"\nEnter Fractionaly Binary Operation: ";
cin>>N1>>slash>>D1>>OP>>N2>>slash>>D2;
if(slash=='/')
{
switch(OP)
{
case '+':
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
break;
case '-':
cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)-(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
break;
case '*':
cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<((N1*N2)<<"/"<<(D1*D2))<<" = "<<((N1*N2)/(D1*D2))<<endl;
break;
case '/':
cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<((N1*D2)<<"/"<<(D1*N2))<<" = "<<((N1*D2)/(D1*N2))<<endl;
break;
default:
cout<<"INVALID OPERATOR!";
getch();
system("cls");
return(0);
}
}
else
{
cout<<"INVALID INPUT!";
getch();
system("cls");
return(0);
}
getch();
system("cls");
return(0);
}
The same error is in the following lines..
case '+':
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
break;
case '-':
cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<(((N1*D2)-(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
break;
case '*':
cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<((N1*N2)<<"/"<<(D1*D2))<<" = "<<((N1*N2)/(D1*D2))<<endl;
break;
case '/':
cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<((N1*D2)<<"/"<<(D1*N2))<<" = "<<((N1*D2)/(D1*N2))<<endl;
I have to submit this as an assignment.
This is my Desired Interface. Note: This is made using cout statements, there is no computing in the back-end.
check your parentheses:
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "
<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
^ ^
Try this:
case '+':
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<((N1*D2)+(N2*D1))<<"/"<<(D1*D2)<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
break;
case '-':
cout<<"Subtraction of Given Fraction is: ("<<N1<<"/"<<D1<<") - ("<<N2<<"/"<<D2<<") = "<<((N1*D2)-(N2*D1))<<"/"<<(D1*D2)<<" = "<<(((N1*D2)-(N2*D1))/(D1*D2))<<endl;
break;
case '*':
cout<<"Multiplication of Given Fraction is: ("<<N1<<"/"<<D1<<") * ("<<N2<<"/"<<D2<<") = "<<(N1*N2)<<"/"<<(D1*D2)<<" = "<<((N1*N2)/(D1*D2))<<endl;
break;
case '/':
cout<<"Division of Given Fraction is: ("<<N1<<"/"<<D1<<") / ("<<N2<<"/"<<D2<<") = "<<(N1*D2)<<"/"<<(D1*N2)<<" = "<<((N1*D2)/(D1*N2))<<endl;
break;