有没有办法在C++的赋值中将"char**"转换为"double"?



我试图将用户输入执行限制为仅数字,当他们输入 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 != '')
{
var1 = (&numvalidation);
if (*numvalidation != '')
{
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;
}
}

下面的代码要求双精度,直到它获得有效的双精度。

它使用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;
}

我刚刚解决了这个问题,并使用维克多·古宾匿名者的参考资料将 char** 转换为双倍 从上述评论中对被质疑的帖子。 这是代码,但如果您发现任何错误,请通过修复告诉我。

#include <iostream>
using namespace std;
int main() {
char numvalidation[256] = {''};
double var1, var2;
var1 != '';
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;
}
}

最新更新