输入验证仅接受数字

时间:2017-03-30 06:03:24

标签: c++ validation c++11

我正在创建程序,如果输入了一个字母,它应该只允许接受数字,它应该重新要求用户输入一个数字。当我运行代码时,我得到一个错误,说有一个问题就是我的代码副本

#include <iostream>
using namespace std;

class Triangle
{
    private:
        double base;
        double height;
    public:
        void setBase(double);
        void setHeight(double);
        double getBase() const;
        double getHeight() const;
        double getArea() const;
};

void Triangle::setBase(double b)
{
    base = b;
}

void Triangle::setHeight(double hg)
{
    height = hg;
}

double Triangle::getBase() const
{
    return base;
}

ouble Triangle::getHeight() const
{
    return height;
}

double Triangle::getArea() const
{
    return .50 *(base * height);
}

int main()
{
    double number; 
    double totalArea;
    Triangle one; 
    Triangle two;
    Triangle three;

    do
    {
        cout << "What is the base of Triangle One:"; 
        cin >> number;

        one.setBase(number); 
            cout << "You have entered " << number << " for the base of the triangle.\n";

        cout << "What is the height of Triangle One:"; 
        cin >> number;
        one.setHeight(number); 
            cout << "You have entered " << number << " for the height of the triangle.\n";

            cout << "What is the base of Triangle Two: "; 
        cin >> number;
        two.setBase(number); 
            cout << "You have entered " << number << " for the base of the triangle.\n";

        cout << "What is the height of Triangle Two:"; 
        cin >> number;
        two.setHeight(number); 
            cout << "You have entered " << number << " for the height of the triangle.\n";

            cout << "What is the base of Triangle Three:"; 
        cin >> number;
        three.setBase(number); 
            cout << "You have entered " << number << " for the base of the triangle.\n";

        cout << "What is the height of Triangle Three:"; 
        cin >> number;
        three.setHeight(number); 
             cout << "You have entered " << number << " for the height of the triangle.\n";
    }   
    while (0); 
    {
        if (cin >> number) 
        {
            break;
        } 
        else 
        {
            cout << "Invalid Input! Please input a numerical value." << endl;
                    cin.clear();
            while (cin.get() != '\n') ; 
        }
    }


    totalArea = one.getArea() + two.getArea() + three.getArea(); 

    cout << "The total area of the three triangles is " << totalArea << endl; 

    return 0; 

}

此处error即时接收

3 个答案:

答案 0 :(得分:0)

你错过了双倍的d:

#include <stdio.h>
#include <string.h>        

int check(char *password)
{
    char pin_buffer[4];

    int authority = 0;

    strcpy(pin_buffer,password);

    if(strcmp(pin_buffer,"abcd")==0)

        authority=1;
    return authority;

}

int main(int argc, char *argv[])
{   
    if(argc < 1)
    {           
        printf(argv[0]);
        exit(0);
    }

    if(check(argv[1]))
    {
       printf("access granted");
    } 
    else
    {
        printf("access denied};
}

答案 1 :(得分:0)

执行语法应如下所示:

do{

}while(x == 0);

你是怎么做到的:

do{

}while();
{ <--- //this

} <--- //and this

尝试删除这两个括号{}。它们不是必需的。

答案 2 :(得分:0)

  1. 做{ ... }而(0);
  2. 表示您的循环完成一次。它会永远退出。所以你可以省去这个do-while循环。

    1. while(0)之后的代码;看起来像是一个应该测试用户输入的函数。
    2. 我会介绍字符串和异常并按如下方式编写:

      #include <iostream>
      #include <stdexcept>
      #include <string>
      using namespace std;
      
      //Work-Class 
      class Triangle {
      private:
          double base;
          double height;
      
      public:
          void setBase(double);
          void setHeight(double);
          double getBase() const;
          double getHeight() const;
          double getArea() const;
      };
      
      void Triangle::setBase(double b) {
          base = b;
      }
      
      void Triangle::setHeight(double hg) {
          height = hg;
      }
      
      double Triangle::getBase()const {
          return base;
      }
      
      double Triangle::getHeight() const {
          return height;
      }
      
      double Triangle::getArea() const {
          return (base*height) / 2;
      }
      
      //conversion and test function
      double getDoubleString(string questString) {
          string userAnswer;
          double d;
      
          cout << questString;
          getline(cin, userAnswer);
          try {
              d = stod(userAnswer);
          }
          catch (const std::invalid_argument& ia) {
              cerr << "Invalid argument: " << ia.what() << '\n';
              d = 0.0;
          }
          return d;
      }
      
      //Enter the hell
      int main() {
          double number;
          double totalArea;
          Triangle one;
          Triangle two;
          Triangle three;
      
          cout << "--------------------------------------------------------------\n\n";
          //Triangle one
          one.setBase(getDoubleString("What is the base of Triangle One: "));
          cout << "You have entered " << one.getBase() << " for the base of the triangle.\n";
          one.setHeight(getDoubleString("What is the height of Triangle One: "));
          cout << "You have entered " << one.getHeight() << " for the height of the triangle.\n\n";
      
          //Triangle two
          two.setBase(getDoubleString("What is the base of Triangle Two: "));
          cout << "You have entered " << two.getBase() << " for the base of the triangle.\n";
          two.setHeight(getDoubleString("What is the height of Triangle Two: "));
          cout << "You have entered " << two.getHeight() << " for the height of the triangle.\n\n";
      
          //Triangle three
          three.setBase(getDoubleString("What is the base of Triangle Three: "));
          cout << "You have entered " << three.getBase() << " for the base of the triangle.\n";
          three.setHeight(getDoubleString("What is the height of Triangle Three: "));
          cout << "You have entered " << three.getHeight() << " for the height of the triangle.\n\n";
      
          totalArea = one.getArea() + two.getArea() + three.getArea();
      
          cout << "--------------------------------------------------------------\n\n";
          cout << "The total area of the three triangles ist " << totalArea << endl;
          return 0;
      }