【问题标题】:what will i do if i want to enter a char to the value of a,b,c then it will say "error"?如果我想在 a,b,c 的值中输入一个字符,我会怎么做,然后它会说“错误”?
【发布时间】:2014-01-02 13:04:03
【问题描述】:
#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 不工作

【问题讨论】:

  • edit您的问题与您遇到的确切错误。
  • “我无法编译它,因为它说 g++.exe 不工作” - 也许你的 g++ 已损坏?
  • 如果您的桌面编译器不工作。暂时尝试在线的。 ideone.com

标签: c++ dev-c++


【解决方案1】:

如果我没看错的话,如果用户输入的不是浮点值,你想显示一条消息吗?然后您可以通过记住流可以用作boolean value 并记住例如输入运算符返回流。所以你可以做例如

if (!(cin >> a))
{
    std::cout << "Error: Illegal input\n";
}

要清除非法输入设置的错误位,请使用std::basic_ios::clear

【讨论】:

    【解决方案2】:

    您应该将其存储在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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2022-10-24
      • 1970-01-01
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多