gpt4 book ai didi

c++ - 大括号和main方法中的返回值

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:37:22 27 4
gpt4 key购买 nike

<分区>

我读了这个code (Bjarne Stroustrup)。我很困惑...main 函数体不在 {} 中,函数不返回值(作为 int)。而且它有效……为什么?

#include "std_lib_facilities.h" 

int main()
try
{
cout<< "please enter two floating-point values separated by an operator\n The operator can be + - * or / : ";
double val1 = 0;
double val2 = 0;
char op = 0;
while (cin>>val1>>op>>val2) { // read number operation number
string oper;
double result;
switch (op) {
case '+':
oper = "sum of ";
result = val1+val2;
break;
case '-':
oper = "difference between ";
result = val1-val2;
break;
case '*':
oper = "product of ";
result = val1*val2;
break;
case '/':
oper = "ratio of";
if (val2==0) error("trying to divide by zero");
result = val1/val2;
break;
//case '%':
// oper = "remainder of ";
// result = val1%val2;
// break;
default:
error("bad operator");
}
cout << oper << val1 << " and " << val2 << " is " << result << '\n';
cout << "Try again: ";
}
}
catch (runtime_error e) { // this code is to produce error messages; it will be described in Chapter 5
cout << e.what() << '\n';
keep_window_open("~"); // For some Windows(tm) setups
}
catch (...) { // this code is to produce error messages; it will be described in Chapter 5
cout << "exiting\n";
keep_window_open("~"); // For some Windows(tm) setups
}

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