Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c++ - Why is Converting std::double to std::string using std::stod giving an exception?

I am trying to get the addition done on the following method defined in my code, but after submitting the user input the program just return an exception instead of addition.

Exception:

terminate called after throwing an instance of 'std::invalid_argument' what(): stoi

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information.

Code:

#include <iostream>
#include <string>
using namespace std;
bool inputValidation_F(string userInput_VSTR1, string userInput_VSTR2)
{
    for (int inputChecker_V = 0; inputChecker_V < userInput_VSTR1.length() || userInput_VSTR1.length() && inputValidation_F; inputChecker_V ++)
        if (!(userInput_VSTR1[inputChecker_V] || userInput_VSTR2[inputChecker_V] >= 48 && userInput_VSTR1[inputChecker_V] || userInput_VSTR2[inputChecker_V] <= 57))
        return false;
    return true;
}
void mainMenu_F();
void userChoice_F();
void calCulations_F(double, double);
void resultAddition_F(double, double);
int main()
{
    mainMenu_F();
}
void mainMenu_F()
{
    cout << "Main Menu:" << '
';
    cout << "-------------------------------" << '
';
    cout << "Enter + for Addition" << '
';
    cout << "-------------------------------" << '
';
    cout << "Choose any Option from the List" << '
';
    cout << "-------------------------------" << '
';
    userChoice_F();
}
void userChoice_F()
{
    double addition_V_1;
    double addition_V_2;
    char uChoice_V;
    cin >> uChoice_V;
    switch (uChoice_V)
    {
    case '+':
    cout << "Addition: Enter the first value: "; cin >> addition_V_1;
    cout << "Addition: Enter the second value: "; cin >> addition_V_2;
    calCulations_F(addition_V_1, addition_V_2);
    }
}
void calCulations_F(double addition_V_1, double addition_V_2)
{
    string addition_V_1STR;
    string addition_V_2STR;
    addition_V_1 = stod (addition_V_1STR);
    addition_V_2 = stod (addition_V_2STR);
    bool additionChecker_F;
    additionChecker_F = inputValidation_F (addition_V_1STR, addition_V_2STR);
    if (!additionChecker_F)
        additionChecker_F = false;
    else resultAddition_F (addition_V_1, addition_V_2);
}
void resultAddition_F(double addition_V_1, double addition_V_2)
{
    double resultAddition_V = (addition_V_1 + addition_V_2);
    cout << "The result for the addition of the Entered values is: [" << resultAddition_V << "]" << '
';
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this solution will solve your Issues and Misunderstandings, Your code is edited and if you have any question, ask in comment or chat section, I will be happy to help you.

#include <iostream>
#include <string>
#include <limits>
using namespace std;
bool isvalid_(string);
bool inputValidation_F(string userInput_VSTR1, string userInput_VSTR2)
{
    for (int inputChecker_V = 0; inputChecker_V < userInput_VSTR1.length() || userInput_VSTR1.length() && inputValidation_F; inputChecker_V ++)
        if (!(userInput_VSTR1[inputChecker_V] || userInput_VSTR2[inputChecker_V] >= 48 && userInput_VSTR1[inputChecker_V] || userInput_VSTR2[inputChecker_V] <= 57))
        return false;
    return true;
}
void mainMenu_F();
void userChoice_F();
void calCulations_F(double, double);
void resultAddition_F(double, double);
int main()
{
    mainMenu_F();
}
void mainMenu_F()
{
    cout << "Main Menu:" << '
';
    cout << "Enter + for Addition" << '
';
    userChoice_F();
}
void userChoice_F()
{
    double addition_V_1, addition_V_2;
    char uChoice_V;
    cin >> uChoice_V;
    switch (uChoice_V)
    {
    case '+':
    cout << "Addition: Enter the first value: "; cin >> addition_V_1;
    cout << "Addition: Enter the second value: "; cin >> addition_V_2;
    while(!isvalid_("Ops! Entered invalid value, Try again."));
    calCulations_F(addition_V_1, addition_V_2);
    break;
    default:
        cout << "Invalid Choice," << '
';
    }
}
void calCulations_F(double addition_V_1, double addition_V_2)
{
    if (!inputValidation_F)
        bool isvalid_ = 0;
    else resultAddition_F (addition_V_1, addition_V_2);
}
void resultAddition_F(double addition_V_1, double addition_V_2)
{
    double resultAddition_V = (addition_V_1 + addition_V_2);
    cout << "The result for the addition of the Entered values is: [" << resultAddition_V << "]" << '
';
}
bool isvalid_(string err_msg)
{
    if(cin.rdstate())
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>:: max(), '
');
        system("cls");
        cout << "Invalid number entry! Try again from the Beginning." << '
';
        mainMenu_F();
        return false;
    }
    return true;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...