涉及函数的开关语句的问题

problems with switch statement involving functions

本文关键字:语句 问题 开关 函数      更新时间:2023-10-16

这个程序没有提示我输入。我需要有关正确定义和调用函数的帮助。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.nn";
            cin >> choice; // Repeat attempt to gather input from user
        }
    }
    while (menu() != QUIT);
    return 0;
}
int menu()
{
    int choice;
    cout << "nttMathematical Menun" // Header
    << "1) Summationn" // 1st choice
    << "2) Factorialn" // 2nd choice
    << "3) Exponentialn" // 3rd choice
    << "4) Exit Programnn" // 4th choice
    << "Please make a selectionn" // Ask user for imput choice
    << "of either 1,2,3 or 4.nn";
    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.nn";
    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.nn";
    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.nn";
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.nn";
    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.nn";
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.nn";
    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";
}`

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

无论如何,不要从执行所有工作的函数返回任何内容。来得及

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

叫:

getExpo();