C++ 涉及函数的switch语句的问题
此程序不会提示我输入。我需要帮助正确定义和调用函数。菜单应该显示一个文本字符串,并要求用户选择一个数字1-4,如果用户输入的数字超出该范围,它将要求用户输入一个适当的数字,直到输入1-4为止。menu的值将存储在变量“choice”中,并在开关语句中使用。 在switch语句中,调用了相应的选择函数[getSum、getFactor、getExpo和exit]。它们都需要向用户请求一个整数,并执行一些算术运算,然后向用户返回一些输出文本和计算值。 所有这些都在重复此过程的do while循环内,直到用户选择选项4退出程序,退出函数将向用户返回字符串退出消息,然后程序将终止C++ 涉及函数的switch语句的问题,c++,function,switch-statement,C++,Function,Switch Statement,此程序不会提示我输入。我需要帮助正确定义和调用函数。菜单应该显示一个文本字符串,并要求用户选择一个数字1-4,如果用户输入的数字超出该范围,它将要求用户输入一个适当的数字,直到输入1-4为止。menu的值将存储在变量“choice”中,并在开关语句中使用。 在switch语句中,调用了相应的选择函数[getSum、getFactor、getExpo和exit]。它们都需要向用户请求一个整数,并执行一些算术运算,然后向用户返回一些输出文本和计算值。 所有这些都在重复此过程的do while循环内,
#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";
}`
不要多次调用菜单:它将提示用户两次
无论如何,不要从完成所有工作的函数返回任何内容。成功
void getExpo(){ ...; return; }
电话:
你需要更好地解释你的问题。你是在要求别人向你解释你的代码在做什么吗?你得到的结果不正确吗?a问题是什么?b代码示例太长,请提供与您的问题匹配的最短代码。c这不是解决你的家庭作业的网站。getSum似乎少了几行?@Flovdis我没有要求任何人解决我的“家庭作业”。显然,我已经尝试自己解决这个问题,并试图清除任何编译器错误。我只是不明白如何用我定义的函数恰当地调用和传递值。@怀疑这只是个bug,很容易看到-请参阅我的答案和注释。我认为有足够的斗争需要帮助。我不相信我给菜单打了不止一次电话。我有一个原型,一个电话,然后是一个定义。感谢您的尝试,但是我尝试使用您的建议进行修复,仍然得到0个输入数据的提示,等等。您必须执行{choice=menu;…和…}while menu!=退出,1+1=2。只需尝试该程序:除了在第一次选择后提示两次,即只有一次出现其他错误时才会提示。对于“1”,考虑到用户输入的上限,32位int是不够的。对于“2”,结果不会显示,使用100作为上限不会在任何整数模式下产生有意义的结果。我没有检查“3”。谢谢@luane,我的IDE一直有问题。我以为我写的所有代码都不管用。我创建了一个新项目,从你的答案中吸取了一些技巧,我很乐意去做。再次感谢你。
getExpo();