gpt4 book ai didi

c++ - 在 C++ 中如何调用错误?

转载 作者:太空狗 更新时间:2023-10-29 23:24:18 26 4
gpt4 key购买 nike

我正在研究 Bjarne Stroustrup 的编程第 3 章:使用 C++ 的原理和实践

Prompt the user to enter the age or the recipient and assign it to an int variable age. Have your program write "I hear you just had a birthday and You are age years old." If age is 0 or less or 110 or more, call error("you're kidding!").

cout<<"Enter the age of "<<friend_name<< ".\n";
int age=0;
cin>>age;
if (age==0)
cout<<"you're kidding!";

if (age>=110)
cout<<"you're kidding!";

if (age!=0, 110)
cout<<"I hear you just had a birthday and you are " <<age<< " years old.\n";

我尝试执行上面的代码,但它一直给我

"you're kidding! I hear you you just had a birthday and you are 0 years old"

但我认为那是不对的。我认为它应该做其中之一而不是两者,但我不知道如何让它做到这一点。

最佳答案

if (age!=0, 110) 是你的问题。逗号运算符返回最右边的表达式,因此该代码等效于 if(110)

关于c++ - 在 C++ 中如何调用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5980395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com