需要帮助使用switch语句创建函数和错误

时间:2015-11-26 17:10:12

标签: c++ c++11 switch-statement

我使用switch语句来处理一些代码,我将在main中创建一个函数来调用。我用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;
}

1 个答案:

答案 0 :(得分:1)

你唯一需要改变的是修改为整数,因为switch需要一个int。

class User extends BaseUser
{
    /**
     * @ORM\OneToOne(targetEntity="UserInfo", inversedBy="user", cascade={"all"}, orphanRemoval=true)
     */
    private $info;
}

如果你想将修正案用作字符串,恐怕你无法通过开关检查它。相反,您应该使用if / else if语句:

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)
  { 

  ...
}
相关问题