如果语句不停止

时间:2019-02-09 12:43:46

标签: c++

好吧,我正在尝试编写十二生肖的代码。 该条件没有被检测为错误,但是当我键入单词时,它不仅显示一个输出,还显示了不同条件下的每个输出

我尝试更改条件不变。

  #include <iostream>
 using namespace std;
 int main()
 {
int sign, Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, 
 Sagittarius, Capricorn, Aquarius, Pices;
cout<<"Aries        Taurus     Gemini    Cancer"<<endl;
cout<<"Leo          Virgo      Libra     Scorpio"<<endl;
cout<<"Sagittarius  Capricorn  Aquarius  Pices"<<endl;

cout<<endl;

cout<<"Enter the number Your Zodiac Sign Please: ";
cin>>sign;

if (sign==Aries)
{
 cout<<"Your Zodiac Sign is Aries"<<endl;
 cout<<"You get to show the world exactly who you are and what you can do!"<<endl;
 cout<<"Your lucky number is 17"<<endl;
 cout<<"Your lucky color is Cyan";
}
if (sign==Taurus)
{
 cout<<"Your Zodiac Sign is Taurus"<<endl;
 cout<<"Your partner is in-charge of you today"<<endl;
 cout<<"Your lucky number is 666"<<endl;
 cout<<"Your lucky color is Red";
}
if (sign==Gemini)
{   
 cout<<"Your Zodiac Sign is Gemini"<<endl;
 cout<<"Trust your gut. step out of your comfort zone."<<endl;
 cout<<"Your lucky number is 3"<<endl;
 cout<<"Your lucky color is Pink";

例如,我输入白羊座作为我的标志。我希望仅在白羊座下看到输出,而没有其他内容。

但是此代码的输出是:输入白羊座。我得到的输出是白羊座假定输出之后的一切。

1 个答案:

答案 0 :(得分:4)

白羊座,金牛座,双子座,巨蟹座,狮子座,处女座,天秤座,天蝎座,人马座,摩Cap座,水瓶座和皮克斯的所有变量均未初始化,以比较 sign 与他们有未定义的行为。

还请注意,您不要仅在进行int height(TreeNode node) { if(node == null) { return 0; } else { return 1 + Math.max(height(node.left), height(node.right)); } } 时检查输入,例如最好执行cin>>sign;以确保已读取 sign


也许你想要

if (! (cin>>sign)) return 0;

,在这种情况下,您可以将 if 的序列替换为 switch / cases ,或者至少从第二个开始使用 else if if

无论如何,您总是打印文字字符串,可以将它们放入数组以减小代码大小:

int sign;

enum Zodiac { Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, Pices };

如果代码变为:

const char * ZodiacDescr[] = {
  "Your Zodiac Sign is Aries\nYou get to show the world exactly who you are and what you can do!\nYour lucky number is 17\nYour lucky color is Cyan",
  "Your Zodiac Sign is Taurus\nYour partner is in-charge of you today\nYour lucky number is 666\nYour lucky color is Red",
  "Your Zodiac Sign is Gemini\nTrust your gut. step out of your comfort zone.\nYour lucky number is 3\nYour lucky color is Pink",
  ...
};

如果您还总是写下您的十二生肖是幸运数字幸运颜色,那么您也可以使用 struct 喜欢:

if ((sign >= Aries) && (sign <= Pices))
  cout << ZodiacDescr[sign] << endl;
else
  cout <<"invalid sign number" << endl;

代码变成

struct { 
  const char * name;
  const char * beh; 
  int number;
  const char * color; 
} ZodiacDescr[] = {
  { "Aries", "You get to show the world exactly who you are and what you can do!", 17, "Cyan"},
  { "Taurus", "Your partner is in-charge of you today", 666, "Red" },
  { "Gemini", "Trust your gut. step out of your comfort zone.", 3, "Pink" },
  ...
};

但是如果要求输入数字(如果不切实际的话),最好要求输入标牌的名称,例如:

if ((sign >= Aries) && (sign <= Pices))
  cout << "Your Zodiac Sign is " << ZodiacDescr[sign].name << '\n'
       << ZodiacDescr[sign].beh << '\n'
       << "Your lucky number is " << ZodiacDescr[sign].number << '\n'
       << "Your lucky color is " << ZodiacDescr[sign].color << endl;
else
  cout <<"invalid sign number" << endl;

编译和执行:

#include <iostream>
#include <string>
using namespace std;

struct Zodiac { 
  const char * name;
  const char * beh; 
  int number;
  const char * color; 
};

const Zodiac ZodiacDescr[] = {
  { "Aries", "You get to show the world exactly who you are and what you can do!", 17, "Cyan"},
  { "Taurus", "Your partner is in-charge of you today", 666, "Red" },
  { "Gemini", "Trust your gut. step out of your comfort zone.", 3, "Pink" },
  // ...
};

int main()
{
  string sign;

  cerr << "Enter you zodiac sign please : "; // cerr to flush
  if (cin >> sign) {
    for (auto s : ZodiacDescr) {
      if (s.name == sign) {
        cout << "Your Zodiac Sign is " << s.name << '\n'
          << s.beh << '\n'
            << "Your lucky number is " << s.number << '\n'
              << "Your lucky color is " << s.color << endl;
        return 0;
      }
    }
  }
  cout << "invalid sign" << endl;

  return 0;
}

当然,也可以将标记放置在 map 中,其中键是标记的名称,而不是放置在数组中,以方便从标记的名称等中搜索标记< / p>