涉及函数的开关语句的问题
problems with switch statement involving functions
这个程序没有提示我输入。我需要有关正确定义和调用函数的帮助。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();
相关文章:
- 我似乎对if/else的基本语句有问题:/
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- Craps游戏问题,忽略if语句
- Arduino 用于语句错误。令牌之前的预期')' ';'。如何解决这个问题?
- 有人能在我的代码中找到与C++中的IF语句相关的问题吗
- 面临在 if 语句之外打印变量数据的问题 完成使用 Qt 编程
- 如果 GPA 计算器的语句问题
- C++:关于for语句和参数的一般问题
- C - 如果语句问题.代码将不会运行其他,否则
- C++ 在循环时执行 if 语句问题
- C++二进制搜索树切换语句问题
- C++If Else语句问题
- C++中的条件语句问题
- Switch语句问题
- ASCII和isalpha if语句问题
- 语句问题中要字符串的CPP int
- If语句问题
- c++ If语句问题-显示错误信息
- c++ if/else语句问题
- Switchcase语句问题.从文本文件中读取信息