我试图将用户输入执行限制为数字,当他们输入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;
}
}
答案 0 :(得分:5)
以下代码要求获得双精度,直到获得有效的双精度。
它使用std::string
。您只能使用isdigit()
,但这很荒谬(在我看来)。
#include<string>
#include<iostream>
#include<cctype>
int main(){
double var1{};
std::string temp{};
size_t processed_chars_no{};
do{
std::cout<<"Enter a valid double value: \n";
std::cin >> temp;
if( isdigit(temp[0]) )var1 = std::stod( temp, &processed_chars_no );
}
while ( processed_chars_no != temp.size() );
std::cout<<"\n"<<var1;
}
答案 1 :(得分:2)
我刚刚解决了该问题,并通过使用上述受质疑的评论中的 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;
}
}