我想将用户输入执行限制为仅数字,当他们输入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;
}
}
我刚刚解决了问题,并通过使用上面有关问题帖子的评论中的Victor Gubin和Anonymous的引用,将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;
}
}