If语句未停止
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。我得到的输出是在白羊座的预期输出之后的一切。
所有变量均未初始化,以将符号与它们进行比较。
还请注意,您不检查仅执行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
当然,也可以将标志放置在地图中,其中键是标志的名称,而不是阵列中,以便于从名称等中搜索标志
相关文章:
- 我的简单if-else语句是如何无法访问的代码
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- 是否可以使用if constexpr删除控制流语句
- 我似乎对if/else的基本语句有问题:/
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果中的多个语句是否与多个 if 相同?
- C++ If/Else 语句被跳过
- 使用语句if时逻辑的问题
- 如何添加语句if-else
- 如何将语句if(isalpha(c))从c++转换为MIPS ?
- 在if语句if C++中创建对象时出现作用域问题
- if语句-if/else和参数放置C++