If语句未停止

If statement not stopping

本文关键字:语句 If      更新时间:2023-10-16

好吧,我正在尝试为黄道十二宫写一个代码。条件没有被检测为错误,但当我键入单词时,它显示了不同条件的每个输出,而不是只显示一个输出

我试着把条件改成原来的样子。

#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。我得到的输出是在白羊座的预期输出之后的一切。

所有变量均未初始化,以将符号与它们进行比较。

还请注意,您不检查仅执行cin>>sign;的输入,最好执行例如if (! (cin>>sign)) return 0;,以确保符号已读取


也许你想要

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

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

不管怎样,你总是打印文字字符串,你可以把它们放进一个数组来减少代码大小:

const char * ZodiacDescr[] = {
"Your Zodiac Sign is AriesnYou get to show the world exactly who you are and what you can do!nYour lucky number is 17nYour lucky color is Cyan",
"Your Zodiac Sign is TaurusnYour partner is in-charge of you todaynYour lucky number is 666nYour lucky color is Red",
"Your Zodiac Sign is GemininTrust your gut. step out of your comfort zone.nYour lucky number is 3nYour 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

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