C++ If语句不停止

C++ If语句不停止,c++,C++,嗯,我正试图为黄道带符号编写一个代码。 条件没有被检测为错误,但当我键入单词而不是只显示一个输出时,它会显示不同条件下的每个输出 我试着改变条件,但还是一样 #include <iostream> using namespace std; int main() { int sign, Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, P

嗯,我正试图为黄道带符号编写一个代码。 条件没有被检测为错误,但当我键入单词而不是只显示一个输出时,它会显示不同条件下的每个输出

我试着改变条件,但还是一样

  #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";
#包括
使用名称空间std;
int main()
{
int星座,白羊座,金牛座,双子座,巨蟹座,狮子座,处女座,天秤座,天蝎座,
射手座、摩羯座、水瓶座、皮克斯;
白羊座、金牛座、双子座、巨蟹座、狮子座、处女座、天秤座、科尔皮奥、射手座、摩羯座、水瓶座和皮斯没有一个变量是初始化的,与他们相比,星座有一个未定义的行为

请注意,您不必仅在执行签名时检查输入;
,最好执行以下操作:例如,如果(!(cin>>签名))返回0;以确保签名已被读取


也许你想要

int sign;

enum Zodiac { Aries, Taurus, Gemini, Cancer, Leo, Virgo, Libra, Scorpio, Sagittarius, Capricorn, Aquarius, Pices };
在这种情况下,您可以用switch/cases替换if的序列,或者至少使用第二个if中的else if

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

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",
  ...
};
如果代码变为:

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

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;

当然,也可以将标志放置在地图中,其中键是标志的名称,而不是在数组中,以便于从名称中搜索标志等

a)您似乎缺少代码的一部分,B)请正确缩进代码是的,“}”,但这并不重要。只有密码白羊座的号码是多少?现在看看你的代码,在代码里你告诉计算机白羊座的数字是多少?这是通过希望你得到正确的东西来编程,而不是理解你需要做什么。你需要在你的代码中有类似于
intaries=1,Taurus=2,
(或者你为星座选择的任何数字)。哦。。是我的错。“我忘了在发表感谢你的回答之前编辑我该怎么做?”OddCritter我用几个建议编辑了我的答案,包括询问标志的名称而不是编号
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;
}
pi@raspberrypi:/tmp $ g++ -pedantic -Wextra z.cc
./a.oupi@raspberrypi:/tmp $ ./a.out
Enter you zodiac sign please : aze
invalid sign
pi@raspberrypi:/tmp $ ./a.out
Enter you zodiac sign please : Aries
Your Zodiac Sign is Aries
You get to show the world exactly who you are and what you can do!
Your lucky number is 17
Your lucky color is Cyan
pi@raspberrypi:/tmp $ ./a.out
Enter you zodiac sign please : Gemini
Your Zodiac Sign is Gemini
Trust your gut. step out of your comfort zone.
Your lucky number is 3
Your lucky color is Pink