Reputation: 847
I am trying to limit user input execution to numbers only, when they enter a / b instead of 8 / 2 the result is zero, but I want to give them a warning when they tried to enter something like this.
When I execute my code I am getting error that not possible to convert char**
to double
in assignment. The character I have implemented for conversion is numvalidation
to validate the user input just for first value, if input is number then accept else return default message.
#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;
}
}
Upvotes: 1
Views: 234
Reputation: 847
I have just solved the Issue and make it possible to convert char** to double by using the Reference of Victor Gubin and Anonymous from the above comments on the questioned post. Here is the code but if you found any bug please let me know with a fix.
#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;
}
}
Upvotes: 2
Reputation: 7090
The following code asks for a double until it gets a valid double.
It uses std::string
. You can use isdigit()
only but it will be ridiculous (in my opinion).
#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;
}
Upvotes: 5