Question: I use visual studio Can someone fix it? It says error @ value = stod (input); #include #include using namespace std; //prototype declaration for functions
I use visual studio
Can someone fix it? It says error @ "value = stod (input);"
#include#include using namespace std; //prototype declaration for functions menuSelect & isValid char menuSelect(); bool isValid(string, double &); //main function int main(int argc, char *argv[]) { //required variables double inches = 0.0; double centimeters = 0.0; string inch, cent; char select; //loop till the user selects to quit do { //call the menuSelect function to display the menu select = menuSelect(); //if user selects to convert English to Metric if (select == 'E') { //loop till we get a valid input from the user do { // prompting user for inches cout << "Enter number of inches : "; //reading inches values in string format cin >> inch; //call the isValid function to check if the input is valid if (!isValid(inch, inches)) { //if not then display illegal input & get another input cout << "Illegal input!" << endl; } } while (!isValid(inch, inches)); // reading inches from user centimeters = inches * 2.54; // english representation cout << inches << " inches is equal to " << centimeters << " centimeters" << endl; } //if user selects to convert Metric to English else if (select == 'M') { //loop till we get a valid input from the user do { // prompting user for centimeters cout << "Enter number of centimetrs : "; // reading centimeters value in string format cin >> cent; //call the isValid function to check if the input is valid if (!isValid(cent, centimeters)) { //if not then display illegal input & get another input cout << "Illegal input!" << endl; } } while (!isValid(cent, centimeters)); // converting into inches inches = centimeters / 2.54; // english representation cout << centimeters << " centimeters is equal to " << inches << " inches " << endl; } } while (select != 'Q'); return 0; } //function definition for menuSelect function char menuSelect() { //variable to store user's selection char selection; //loop till the user selects 'E', 'M' or 'Q' do { cout << endl; //display menu & prompt for user's selection cout << "Enter E to convert English to Metric , M to convert Metric to English, Q to Quit "; // reading selection from user cin >> selection; // convert user's selection to uppercase character selection = toupper(selection); } while (selection != 'E' && selection != 'M' && selection != 'Q'); //return the selection return selection; } //function definition for isValid function which checks for non numeric & negative input bool isValid(string input, double &value) { //variable to count the number of dots in the input string int dotNum = 0; //loop through each character in the input string for (int i = 0; i < input.length(); i++) //check if each character is a digit if (!isdigit(input[i])) { //if not then check if it is a dot & that it is the 1st dot in the input if (input[i] == '.' && dotNum == 0) { //if true then continue the loop & increment the count of dots dotNum++; continue; } //return false if we encounter a non digit character or more than one dot in the input string else return false; } //if the string is in a valid double format than convert it into a double value = stod(input); //return false if the double value is less than 0 i.e.,negative if (value < 0) return false; //if the input is numeric & a non negative than return true return true; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
