#include <iostream>
#include <math.h>
using namespace std;
int main()
{
double a,b,c,d,X,Y;
float x1,x2;
char ans,i;
do
{
cout << "Welcome!" <<endl
<< "To my Simple Machine" <<endl
<<endl
<< "Enter the value of a:" <<endl;
cin >>a;
cout << "Enter the value of b:" <<endl;
cin >>b;
cout << "Enter the value of c:" <<endl;
cin >>c;
d=b*b-4*a*c;
x1=(-1*b+sqrt(b*b-4*a*c))/2*a;
x2=(-1*b-sqrt(b*b-4*a*c))/2*a;
X=-b/2*a;
如果用户在a,b,c上输入字符,我想显示错误
if(a==0)
{
cout << "You are now solvin a Linear Eq'n" <<endl;
Y=-c/b;
cout << "The value of Y is:" <<Y<<endl
<<endl
<< "Do you want to repeat?" <<endl
<< "Loading...." <<endl;
}
else if(d==0)
{
cout << "You are now solving a Quadratic Eq'n" <<endl
<< "The value of d is:" <<d<<endl
<< "x1 is:" <<x1<<endl
<< "x2 is:" <<x2<<endl
<<endl
<< "Do you want to repeat?" <<endl
<< "Loading...." <<endl;
}
else if(d>0)
{
cout << "You are now solving a Quadratic Eq'n" <<endl
<< "The value of d is:" <<d<<endl
<< "x1 is:" <<x1<<endl
<< "x2 is:" <<x2<<endl
<<endl
<< "Do you want to repeat?" <<endl
<< "Loading...." <<endl;
}
else if(d<0)
{
cout << "You are now solving a Quadratic Eq'n" <<endl
<< "The value of d is:" <<d<<endl
<< "x1 is:" <<X<<"+"<<sqrt(-d)/2*a<<'i'<<endl
<< "x2 is:" <<X<<"-"<<sqrt(-d)/2*a<<'i'<<endl
<<endl
<< "Do you want to repeat?" <<endl
<< "Loading...." <<endl;
}
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
}while(ans=='y' ||ans=='Y');
cout << "End of Program..." <<endl
<< "Thank You!" <<endl;
cin.get();
cin.get();
return 0;
}
我无法编译它'因为它说g ++。exe无法正常工作
答案 0 :(得分:1)
如果我读了你,如果用户输入浮点值以外的任何内容,你想显示一条消息吗?然后你可以通过记住一个流可以用作boolean value来记住,例如输入操作符返回流。所以你可以这样做。
if (!(cin >> a))
{
std::cout << "Error: Illegal input\n";
}
要清除非法输入设置的错误位,请使用std::basic_ios::clear
。
答案 1 :(得分:0)
您应该将其存储在char
变量中,检查它是否为数字条目,然后将其分配给double
。
double b;char tmpChar;
cout << "Enter the value of b:" <<endl;
cin >>tmpChar;
if ((tmpChar >= 'a' && tmpChar <= 'z') || (tmpChar >='A' && tmpChar <='Z'))
error();