程序在运行两次后关闭

时间:2018-05-07 06:23:00

标签: c++

很多代码,所以它的阅读顺序有点高。如果你愿意花时间,谢谢!

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

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

然而,我的问题是,程序运行良好,一切正常,除了它在两次迭代后停止。如果用户转换了某些内容,然后再次转换,程序将结束,直到用户选择该选项为止。

#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;
}

2 个答案:

答案 0 :(得分:1)

第一件事:

double length()
{
    ...
    ...
    return cm, ms, km;
}

这总是返回km,其他的将不会返回。

逻辑错误:

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

程序在运行两次后关闭,因为你以这种方式编写它。看看上面的线条。您打印介绍,下一个打印主菜单,并执行用户想要的,然后打印outro。 Outro再次呼叫主菜单。之后该计划已经结束。

答案 1 :(得分:0)

按如下方式修改主菜单:

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

在printOutro()

中删除cout << mainMenu();