[英]Is there a way to convert 'char**' to 'double' in assignment in C++?
我试图将用户输入的执行限制为仅数字,当他们输入 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;
}
}
下面的代码要求一个双精度直到它得到一个有效的双精度。
它使用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;
}
我刚刚解决了这个问题,并可以通过使用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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.