错误! 'float' 和 'const char [2]' 类型的无效操作数转换为二进制 'operator<<'
ERROR! invalid operands of types 'float' and 'const char [2]' to binary 'operator<<'
我正在尝试编写一个程序,它相当于一个四函数计算器的两个分数。错误出现在 cout 语句中的每个 switch 情况中。
错误是:
[Error] invalid operands of types 'float' and 'const char [2]' to
binary 'operator<<'
我尝试使用谷歌搜索,但没有找到任何结果,但我确实发现了同样的错误,但使用的是 int 而不是 float。不幸的是,他犯了一个明显的错误,他甚至没有使用 cout 语句并将 << 运算符放在最后。
另外,我尝试添加更多括号来指定首先执行哪些算术运算,但没有效果。
#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);
}
同样的错误在下面几行..
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;
我必须将此作为作业提交。
这是我想要的界面。注意:这里是用cout语句做的,后台没有计算。
检查括号:
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "
<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
^ ^
试试这个:
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;
括号不匹配
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<
(((N1*D2)+(N2*D1))<<"/"<<(D1*D2)) /*<-here*/
<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))
在一个街区内。
我正在尝试编写一个程序,它相当于一个四函数计算器的两个分数。错误出现在 cout 语句中的每个 switch 情况中。 错误是:
[Error] invalid operands of types 'float' and 'const char [2]' to binary 'operator<<'
我尝试使用谷歌搜索,但没有找到任何结果,但我确实发现了同样的错误,但使用的是 int 而不是 float。不幸的是,他犯了一个明显的错误,他甚至没有使用 cout 语句并将 << 运算符放在最后。 另外,我尝试添加更多括号来指定首先执行哪些算术运算,但没有效果。
#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);
}
同样的错误在下面几行..
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;
我必须将此作为作业提交。
这是我想要的界面。注意:这里是用cout语句做的,后台没有计算。
检查括号:
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "
<<(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
^ ^
试试这个:
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;
括号不匹配
cout<<"Addition of Given Fraction is: ("<<N1<<"/"<<D1<<") + ("<<N2<<"/"<<D2<<") = "<<
(((N1*D2)+(N2*D1))<<"/"<<(D1*D2)) /*<-here*/
<<" = "<<(((N1*D2)+(N2*D1))/(D1*D2))<<endl;
(((N1*D2)+(N2*D1))<<"/"<<(D1*D2))
在一个街区内。