Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序运行两次后关闭_C++ - Fatal编程技术网

C++ 程序运行两次后关闭

C++ 程序运行两次后关闭,c++,C++,很多代码,所以通读起来有点难。如果你愿意花时间,谢谢你 为了解释我所做的,它是一个将某些度量单位转换为其他度量单位的程序。用户选择他们希望转换的单位以及该单位的多少 一旦程序运行,用户将被发送回主选项菜单,在那里他们可以关闭程序,或继续进行另一次转换 然而,我的问题是,程序运行良好,除了在两次迭代后停止之外,其他一切都正常。如果用户转换了某个内容,然后再次转换,则程序将结束,此时程序将继续,直到用户选择该选项为止 #include <iostream> #include <st

很多代码,所以通读起来有点难。如果你愿意花时间,谢谢你

为了解释我所做的,它是一个将某些度量单位转换为其他度量单位的程序。用户选择他们希望转换的单位以及该单位的多少

一旦程序运行,用户将被发送回主选项菜单,在那里他们可以关闭程序,或继续进行另一次转换

然而,我的问题是,程序运行良好,除了在两次迭代后停止之外,其他一切都正常。如果用户转换了某个内容,然后再次转换,则程序将结束,此时程序将继续,直到用户选择该选项为止

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int answer, subAnswer;

void printIntro();
double mainMenu();
void printOutro();
double length();
double weight();
double volume();
double area();
void printAnswer(double, string, double, string);

// for length conversions
double inches, yards, miles;
double cm, ms, km;

// for weight conversions
double ounces, pounds, tons;
double grams, kGrams;

// for volume conversions
double pints, quarts, gallons;
double liters;

// for area conversions
double sq_milo, sq_meters;
double sq_inches, sq_feet, acres;

int main()
{
    printIntro();
    mainMenu();
    printOutro();

    system("pause");
    return 0;
}

void printIntro()
{
    cout << "\tWelcome to the Unit Conversion helper." << endl;
    cout << "\nPick from the following menu to get started." << endl;
    cout << "Example: Type '1' to select length conversions.\n" << endl;
}

double mainMenu()
{
    cout << "***************************************************\n";
    cout << "1. Lengths\n";
    cout << "2. Weights\n";
    cout << "3. Volumes\n";
    cout << "4. Areas\n";
    cout << "5. Quit program\n";
    cout << "***************************************************" << endl;
    cin >> answer;

    if (answer == 1)
    {
        length();
    }

    if (answer == 2)
    {
        weight();
    }

    if (answer == 3)
    {
        volume();
    }

    if (answer == 4)
    {
        area();
    }

    if (answer == 5)
    {
        exit(0);
    }
    return 0;
}

double length()
{
    cout << "\nWhat would you like to convert?" << endl;
    cout << "1. Convert Inches to centimeters." << endl;
    cout << "2. Convert Yards to meters." << endl;
    cout << "3. Convert Miles to kilometers.\n" << endl;
    cin >> subAnswer;

    if (subAnswer == 1)
    {
        cout << "How many inches?\n" << endl;
        cin >> inches;

        cm = inches * 2.54;
        printAnswer(inches, " Inches is equal to ", cm, " Centimeters.\n");
    }

    if (subAnswer == 2)
    {
        cout << "How many yards?\n" << endl;
        cin >> yards;

        ms = yards * 0.9144;
        printAnswer(yards, " Yards is equal to ", ms, " Meters.\n");
    }

    if (subAnswer == 3)
    {
        cout << "How many miles?\n" << endl;
        cin >> miles;

        km = miles * 1.609344;
        printAnswer(miles, " Miles is equal to ", km, " Kilometers.\n");
    }

    return cm, ms, km;
}

double weight()
{
    cout << "\nWhat would you like to convert?" << endl;
    cout << "1. Convert Ounces to grams." << endl;
    cout << "2. Convert Pounds to kilograms." << endl;
    cout << "3. Convert Tons to kilograms.\n" << endl;
    cin >> subAnswer;

    if (subAnswer == 1)
    {
        cout << "How many ounces?\n" << endl;
        cin >> ounces;

        grams = ounces * 28.349523;
        printAnswer(ounces, " Ounces is equal to ", grams, " Grams.\n");
    }

    if (subAnswer == 2)
    {
        cout << "How many pounds?\n" << endl;
        cin >> pounds;

        kGrams = pounds * 0.453592;
        printAnswer(pounds, " Pounds is equal to ", kGrams, " Kilograms.\n");
    }

    if (subAnswer == 3)
    {
        cout << "How many tons?\n" << endl;
        cin >> tons;

        kGrams = tons * 907.18474;
        printAnswer(tons, " Tons is equal to ", kGrams, " Kilograms.\n");
    }

    return grams, kGrams;
}

double volume()
{
    cout << "\nWhat would you like to convert?" << endl;
    cout << "1. Convert pints to liters." << endl;
    cout << "2. Convert quarts to liters." << endl;
    cout << "3. Convert gallons to liters.\n" << endl;
    cin >> subAnswer;

    if (subAnswer == 1)
    {
        cout << "How many pints?\n" << endl;
        cin >> pints;

        liters = pints * 0.4731631;
        printAnswer(pints, " Pints is equal to ", liters, " Liters.\n");
    }

    if (subAnswer == 2)
    {
        cout << "How many quarts?\n" << endl;
        cin >> quarts;

        liters = quarts * 0.946326;
        printAnswer(quarts, " Quarts is equal to ", liters, " Liters.\n");
    }

    if (subAnswer == 3)
    {
        cout << "How many gallons?\n" << endl;
        cin >> gallons;

        liters = gallons * 3.785306;
        printAnswer(gallons, " Gallons is equal to ", liters, " Liters.\n");
    }

    return liters;
}

double area()
{
    cout << "\nWhat would you like to convert?" << endl;
    cout << "1. Convert square inches to square millimeters." << endl;
    cout << "2. Convert square feet to square meters." << endl;
    cout << "3. Convert acres to square meters.\n" << endl;
    cin >> subAnswer;

    if (subAnswer == 1)
    {
        cout << "How many square inches?\n" << endl;
        cin >> sq_inches;

        sq_milo = sq_inches * 645.16;
        printAnswer(sq_inches, " Square Inches is equal to ", sq_milo,
            " Square Millimeters.\n");
    }

    if (subAnswer == 2)
    {
        cout << "How many square feet?\n" << endl;
        cin >> sq_feet;

        sq_meters = sq_feet * 0.09290304;
        printAnswer(sq_feet, " Square feet is equal to ", sq_meters,
            " Square Meters.\n");
    }

    if (subAnswer == 3)
    {
        cout << "How many acres?\n" << endl;
        cin >> acres;

        sq_meters = acres * 4046.86;
        printAnswer(acres, " Acres is equal to ", sq_meters,
            " Square Meters.\n");
    }

    return sq_milo, sq_meters;
}

void printOutro()
{
    cout << "Returning to the main menu. If you're done, type 5.\n" << endl;
    cout << mainMenu();
}

void printAnswer(double cTemp, string sTemp, double dTemp, string rTemp)
{
    cout << endl;
    cout << cTemp << sTemp << dTemp << rTemp << endl;
}
#包括
#包括
#包括
使用名称空间std;
回答,副回答;
void printIntro();
双主菜单();
无效打印输出();
双倍长度();
双倍重量();
双卷();
双面积();
无效打印应答(双精度,字符串,双精度,字符串);
//用于长度转换
双英寸、双码、双英里;
双厘米,毫秒,千米;
//重量换算
双盎司,磅,吨;
双克,千克拉姆;
//用于体积转换
双品脱、夸脱、加仑;
双升;
//用于面积转换
双平方米,平方米;
双平方英寸,平方英尺,英亩;
int main()
{
printIntro();
主菜单();
printOutro();
系统(“暂停”);
返回0;
}
void printIntro()
{
第一件事:

double length()
{
    ...
    ...
    return cm, ms, km;
}
这个总是返回km,其他的不会返回

还有一个糟糕的逻辑:

printIntro();
mainMenu();
printOutro();

程序运行两次后关闭,因为您是这样编写的。请看上面几行。您可以打印简介,下一个打印主菜单,然后执行用户想要的内容,然后打印outro。outro将再次调用主菜单。在该程序关闭后。

修改主菜单,如下所示:

int main()
{
    while(1)
    {
        printIntro();
        mainMenu();
        printOutro();
    }
    return 0;
}

去掉<代码> CUT谢谢!这是有效的。埃斯特班还考虑了别人指出的。你的代码中似乎没有什么坏的做法。谢谢,我会研究一下。请把你的代码减少到你的问题中。你的当前代码包含了很多你的问题的外设。最小的样本通常看起来类似于GOO。d单元测试:仅执行一项任务,输入值指定为再现性。