簡體   English   中英

如果語句沒有停止

[英]If statement not stopping

好吧,我正在嘗試為十二生肖編寫代碼。 條件沒有被檢測為錯誤,但是當我輸入單詞而不是只顯示一個輸出時,它會顯示不同條件下的每個輸出

我嘗試改變條件仍然相同。

  #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";

例如,我輸入白羊座作為我的星座。 我希望只在 Aries 下看到輸出,而沒有其他內容。

但我為這段代碼得到的輸出是:我輸入了 Aries。 我得到的輸出是 Aries 假設輸出之后的一切。

白羊座、金牛座、雙子座、巨蟹座、獅子座、處女座、天秤座、天蠍座、射手座、摩羯座、水瓶座和雙魚座的變量均未初始化,與它們進行比較的符號具有未定義的行為。

另請注意,您不會僅在執行cin>>sign;檢查輸入cin>>sign; , 最好做例如if (! (cin>>sign)) return 0; 確保已閱讀標志


也許你想要

int sign;

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

在這種情況下,您可以通過switch / case替換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;

如果你也總是寫你的星座是幸運數字幸運顏色,你也可以使用如下結構

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;
}

編譯和執行:

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

當然,也可以將標志放置在地圖中,其中鍵是標志的名稱而不是數組,以便於從名稱等中搜索標志等

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM