温馨提示:本文翻译自stackoverflow.com,查看原文请点击:其他 - Is there a way to convert 'char**' to 'double' in assignment in C++?
c++

其他 - 有没有办法在C ++中将赋值的'char **'转换为'double'?

发布于 2020-04-04 00:00:27

我想将用户输入执行限制为仅数字,当他们输入a / b而不是8/2时结果为零,但是我想在他们尝试输入类似内容时给他们警告。当我执行代码时,出现错误,无法转换char**double赋值。我实现的转换字符是numvalidation仅针对第一个值验证用户输入,如果输入为数字,则接受否则返回默认消息。

#include <iostream>
using namespace std;
int main()
{
    char * numvalidation;
    double var1, var2;
    beginning:
    cout << "Enter the First value: ";
    cin >> var1;
    cout << "Enter the second value: ";
    cin >> var2;
    if (var1 != '\0')
    {
        var1 = (&numvalidation);
        if (*numvalidation != '\0')
        {
            cout << "First Value was not a number,Try again with a correct value." << endl;
        }
        {
            cout << "Do you want to continue using the Calculator? (Y/N)" << endl;
            char restart;
            cin >> restart;
            if (restart == 'y' || restart == 'Y')
                goto beginning;
        }
    }
    cout << "What do you want to do?" << endl;
    cout << "+ :Addition is available" <<endl;
    cout << "- :Subtraction is available" <<endl;
    cout << "* :Multiplication is available" <<endl;
    cout << "/ :Division is available" <<endl;
    cout << "Please chose one of the option below" <<endl;
    char decision;
    cout << "Decision: ";
    cin >> decision;
    system ("cls");
    switch (decision)
    {
        case '+':
            cout << var1 << " + " << var2 << " = " << "" << ( var1 + var2 ) <<endl;
            break;
        case '-':
            cout << var1 << " - " << var2 << " = " << "" << ( var1 - var2 ) <<endl;
            break;
        case '*':
            cout << var1 << " * " << var2 << " = " << "" << ( var1 * var2 ) <<endl;
            break;
        case '/':
            if (var2 == !0)
                cout << var1 << " / " << var2 << " = " << "" << ( var1 / var2 ) <<endl;
            else
                cout << "The value you have entered is invalid because you cannot divide any number by zero " <<endl;
            break;
        default:
            cout << "You have not entered the correct data";
    }
    {
        cout << "Do you want to continue using the Calculator? (Y/N)" << endl;
        char decision2;
        cin >> decision2;
        if (decision2 == 'y' || decision2 == 'Y')
            goto beginning;
    }
}

查看更多

提问者
Саша
被浏览
13
Саша 2020-01-31 20:59

我刚刚解决了问题,并通过使用上面有关问题​​帖子的评论中Victor GubinAnonymous的引用,将char **转换为double成为可能 这是代码,但是如果您发现任何错误,请通过修复让我知道。

#include <iostream>

using namespace std;
int main() {
  char numvalidation[256] = {'\0'};
  double var1, var2;
  var1 != '\0';
  beginning:
    cout << "Enter the First value: ";
  cin >> var1;
  cout << "Enter the second value: ";
  cin >> var2;
  cout << "What do you want to do?" << endl;
  cout << "+ :Addition is available" << endl;
  cout << "- :Subtraction is available" << endl;
  cout << "* :Multiplication is available" << endl;
  cout << "/ :Division is available" << endl;
  cout << "Please chose one of the option below" << endl;
  char decision;
  cout << "Decision: ";
  cin >> decision;
  system("cls");
  switch (decision) {
  case '+':
    cout << var1 << " + " << var2 << " = " << "" << (var1 + var2) << endl;
    break;
  case '-':
    cout << var1 << " - " << var2 << " = " << "" << (var1 - var2) << endl;
    break;
  case '*':
    cout << var1 << " * " << var2 << " = " << "" << (var1 * var2) << endl;
    break;
  case '/':
    if (var2 == !0)
      cout << var1 << " / " << var2 << " = " << "" << (var1 / var2) << endl;
    else
      cout << "The value you have entered is invalid because you cannot divide any number by zero" << endl;
    break;
  default:
    cout << "Only Number are Allowed, Please try again ";
    break;
  } {
    cout << "Do you want to continue using the Calculator? (Y/N)" << endl;
    char decision2;
    cin >> decision2;
    if (decision2 == 'y' || decision2 == 'Y')
      goto beginning;
  }
}

最新帖子

最新文章