涉及函数的switch语句的问题

时间:2014-04-07 08:29:34

标签: c++ function switch-statement

此程序不会提示我输入。我需要帮助定义和正确调用函数。 menu()应该显示一串文本,并要求用户选择数字1-4,如果用户输入一个数字,它将要求用户输入一个合适的数字,直到输入1-4 。 menu()的值将存储在变量' choice'并在switch语句中使用。 在switch语句内部调用相应的选择函数[getSum(),getFactor(),getExpo()和exit()]。它们每个都需要向用户询问整数,并执行一些算术运算,并将一些输出文本返回给具有计算值的用户。 所有这些都在do while循环中重复此过程,直到用户选择选项4退出程序,其中exit()函数将向用户返回字符串退出消息,然后程序将终止。

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int menu();
long int getSum();
long int getFactor();
long int getExpo();
string exit();


int main()
{
        const int SUM = 1, // Summation choice
    FACTOR = 2, // Factorial choice
    EXPO = 3, // Exponential choice
    QUIT = 4; // Exit choice


    int choice; // Numerical menu choice
    long int sums, facts, expos;
    string quits;


    // Force the console to print standard notation
    cout << fixed << setprecision(0);

    // Do-While loop controlled by the users choices
    do
    {
        choice = menu();



        switch (choice) // Start switch option
        {
            case SUM: // 1st choice
            sums = getSum();
            break;

        case FACTOR: // 2nd choice
            facts = getFactor();
            break;

        case EXPO: // 3rd choice
            expos = getExpo();
            break;

        case QUIT: // 4th choice
            quits = exit();
            break;

        default: // Default choice
            // Error message for input outside domain
            cout << "Please make a selection of either 1,2,3 or 4.\n\n";
            cin >> choice; // Repeat attempt to gather input from user
        }
    }
    while (menu() != QUIT);

    return 0;
}

int menu()
{
    int choice;
    cout << "\n\t\tMathematical Menu\n" // Header
    << "1) Summation\n" // 1st choice
    << "2) Factorial\n" // 2nd choice
    << "3) Exponential\n" // 3rd choice
    << "4) Exit Program\n\n" // 4th choice
    << "Please make a selection\n" // Ask user for imput choice
    << "of either 1,2,3 or 4.\n\n";
    cin >> choice; // Gather input from user
    return choice;
}

long int getSum()
{
    int total = 0, userNum, counter;

    // Ouput statement to user
    cout << "Please enter a positive integer value greater than 0 \n"
    << "and less than 10,000,000.\n\n";
    cin >> userNum; // Repeat attempt to gather input from user

    // Compare input to domain
    if (userNum < 0 || userNum > 10000000)
    {
        // Error message for input outside domain
        cout << "Please check your entry and try again.\n\n";
    cin >> userNum; // Repeat attempt to gather input from user
}

// Perform arithmetic summation
for (counter = 1; counter <= userNum; counter++)
{
    total += counter; // Running count
}
cout << "The total value for the added numbers 1 to \n"
<< userNum << " is:\n"<<total;
return total;
}

long int getFactor()
{
int total, userNum, counter;

total = 1;
// Output statement to user
cout << "Please enter a positive integer from 0 \n"
<< "and less than 100.\n\n";
cin >> userNum; // Gather input from user

// Compare input to domain
if (userNum > 100 || userNum < 0)
{
    // Error message if input is outside domain
    cout << "Please check your entry and try again.\n\n";
    cin >> userNum; // Repeat attempt to gather input from user
}

// Perform arithmetic factorial
for (counter = 1; counter <= userNum; counter++)
{
    total *= counter; // Running count
}

// Display arithmetic output to user
cout << "The total value for the multiplied numbers 1 to \n"
<< userNum << " is:\n";
return total;
}

long int getExpo()
{
int total, userNum, counter;

total = 0;
// Output statement to user
cout << "Please enter a positive integer from 0 \n"
<< "and less than 100.\n\n";
cin >> userNum; // Gather input from user

// Compare input to domain
if (userNum > 100 || userNum < 0)
{
    // Error message if input is outside domain
    cout << "Please check your entry and try again.\n\n";
    cin >> userNum; // Repeat attempt to gather input from user
}

// Perform arithmetic exponential
for (counter = 1; counter <= userNum; counter++)
{
    total = pow(2.0, userNum); // Running count
}

// Display arithmetic output to user
cout << "The total value for the exponential function is \n";
return total;
}

string exit()
{
// Exit message
return "Don't be gone for too long...\n";

}`

1 个答案:

答案 0 :(得分:0)

不要多次拨打menu():它会提示用户两次。

不要从完成所有工作的函数返回任何内容。做到这一点

void getExpo(){ ...; return; }

呼叫:

getExpo();