需要帮助创建带有switch语句的函数和错误
Need help creating a function and error with switch statement
我使用switch语句来处理一些代码,我将在main中创建一个函数来调用这些代码。我的switch语句中的switch数量有问题。此外,我将如何为这10种情况创建一个函数?非常困惑,非常感谢您的帮助!谢谢这是我当前的代码:
#include <iostream>
using namespace std;
void constitution(); // the function that will call my amendments in my main function
// how will i create this will 10 possible amendments to choose from?
// There are 4 pirates and they will vote either yes or no,
int main()
{
cout << "Below is a list of the 10 amendments that the two pirates will vote on according to the ships constitution" << endl;
string amendments = 0;
cin >> amendments; // selecting the amendment
switch (amendments)
{
// for outputting the amendment(s) voted on, which will
// be passed on to a function in main to call
case 1: cout << "What does the fox say? Whatever WE tell it to"; //case 1-10 are the 10 amendments to be voted on
break;
case 2: cout << "From now on the annual Cinco de Mayo party will be held on March 8th ";
break;
case 3: cout << "Beginning this year, sharks shall have a week dedicated to us";
break;
case 4: cout << "Pirates are now allowed to talk about fight club";
break;
case 5: cout << "When in Rome, the Romans will do as WE do.";
break;
case 6: cout << "Our mothers will immediately get tattoos that say SON";
break;
case 7: cout << "From now on the President will take our birthdays off.";
break;
case 8: cout << "If we say something costs an arm and a leg, it does";
break;
case 9: cout << "Freemasons are ordered to learn OUR secret handshake.";
break;
case 10: cout << "If a tree falls in the forest and no one is around, it will make a sound only with our permission ";
break;
default: cout << "This won't be used since a amendment will always be voted on, thus never be shown or checked I believe.. (Please correct me) ";
break;
}
return 0;
}
您唯一需要更改的是将修正值设为整数,因为switch需要一个int。
int main()
{
cout << "Below is a list of the 10 amendments that the two pirates will vote on according to the ships constitution" << endl;
int amendments = 0;
cin >> amendments; // selecting the amendment
switch (amendments)
{
...
}
如果您想将修正案用作字符串,恐怕您无法通过开关进行检查。相反,您应该使用if/else-if语句:
if(amendments == '1') {
...
} else if (amendments == '2') {
...
}
相关文章:
- 有一个打印语句的函数是一种糟糕的编程实践吗
- Insert函数不适用于2 if语句C++
- 当调用switch语句中的函数时(即使函数不包含循环),似乎是永不结束的循环的问题
- 如何编写一个使用n倍三元条件语句的C++布尔函数
- 在 COUT 语句中使用 COUT 调用函数
- 函数中的 c++ using 语句,后跟函数名称(对于 ADL?
- 返回非 void 的函数中没有返回语句警告
- 如何释放 googletest ASSERT_THROW语句中的函数使用的资源?
- 错误:调用 .. at return 语句时没有匹配函数
- 函数中的切换大小写语句不会切换C++
- 如何在不同的开关大小写语句上使用对象的类成员函数?
- 有没有办法制作一个 c++ 宏(或类似的东西),它使用户语句被 while 循环的预定义函数包围?
- 表单显示对话框函数错误,并且不执行下面的语句
- 在可变函数调用中执行一次语句
- 在c++中的复制构造函数/一个声明语句中的初始化的延续中使用chain方法
- 在 if 语句中调用重载构造函数失败
- C++实用程序,用于将长开关语句转换为封装开关案例阶梯的简洁函数调用
- 复制构造函数和复制赋值运算符是否应具有相同的语句?
- 为什么当 while 循环中的 if 条件变为 false 时,我的函数不输出最后一条语句?
- 如果语句表达式调用函数,则需要测试是否为 true