C++;预期不合格id 我是C++初学者。我正在使用Xcode编译我的代码。现在我正在复习变量并做一个关于这个主题的简短练习。这个练习要求我要求用户输入他们的名字和姓氏以及他们的年龄。作为一项附加要求,我需要对年龄使用双浮点数,以便将年龄乘以月份。下面是我的代码: #include <iostream> #include <math.h> int main() { std::cout << "Please enter your first name, second name, and age (then press enter).\n"; std::string first; std::string last; std::double age; std::cin >> first >> last >> age; std::cout << "Hello, " << first << " " << last << ". Your age is " << age << " and you are " << (age * 12) << " months old."; return 0; } #包括 #包括 int main() { 标准::cout>第一个>>最后一个>>年龄; std::cout
C++;预期不合格id 我是C++初学者。我正在使用Xcode编译我的代码。现在我正在复习变量并做一个关于这个主题的简短练习。这个练习要求我要求用户输入他们的名字和姓氏以及他们的年龄。作为一项附加要求,我需要对年龄使用双浮点数,以便将年龄乘以月份。下面是我的代码: #include <iostream> #include <math.h> int main() { std::cout << "Please enter your first name, second name, and age (then press enter).\n"; std::string first; std::string last; std::double age; std::cin >> first >> last >> age; std::cout << "Hello, " << first << " " << last << ". Your age is " << age << " and you are " << (age * 12) << " months old."; return 0; } #包括 #包括 int main() { 标准::cout>第一个>>最后一个>>年龄; std::cout,c++,double,C++,Double,double不在std命名空间中。您需要 double age; 您还需要为std::string包含string标题。在某些实现中,您可以从iostream间接获得它,但您不能依赖于此:这是一个侥幸。double不存在于std命名空间中。您需要 double age; 您还需要为std::string包含string标题。在某些实现中,您可以从iostream间接获得它,但您不能依赖于此:这是一个侥幸。double是一个内置类型。它不存在于任何名称空间中,也不需要任何限定!只需删除标题即可
double
不在std
命名空间中。您需要
double age;
您还需要为
std::string
包含string
标题。在某些实现中,您可以从iostream
间接获得它,但您不能依赖于此:这是一个侥幸。double
不存在于std
命名空间中。您需要
double age;
您还需要为
std::string
包含string
标题。在某些实现中,您可以从iostream
间接获得它,但您不能依赖于此:这是一个侥幸。double
是一个内置类型。它不存在于任何名称空间中,也不需要任何限定!只需删除标题即可td::
在双人前面:
double age;
注意,您应该测试您的输入是否实际成功:
if (std::cin >> first >> last >> age) {
// process successful input
}
else {
std::cout << "ERROR: failed to read expected input\n";
}
if(std::cin>>第一个>>最后一个>>年龄){
//处理成功的输入
}
否则{
std::coutdouble
是一种内置类型。它不存在于任何命名空间中,也不需要任何限定!只需删除std:
前面的double
:
double age;
注意,您应该测试您的输入是否实际成功:
if (std::cin >> first >> last >> age) {
// process successful input
}
else {
std::cout << "ERROR: failed to read expected input\n";
}
if(std::cin>>第一个>>最后一个>>年龄){
//处理成功的输入
}
否则{
std::cout首先,最好包含标题
或
<>在C++中,这些类型确实是内置的类型,并使用关键字作为代码>双< /代码>指定类型
double age;
虽然我不明白为什么年龄应该是两倍,因为一年中的月数是一个整数值。首先,最好包括页眉
或
<>在C++中,这些类型确实是内置的类型,并使用关键字作为代码>双< /代码>指定类型
double age;
虽然我不明白为什么年龄应该是双倍的,因为年的月份是整数值。)):我相信下面的代码对于学习如何做这类几乎第一个C++程序是非常理想的。
代码的一个小技术问题是,double
是关键字,而不是标准库定义的名称,因此不在名称空间std
中
此外,我还添加了
- 包含以便携方式使用
std::string
所需的
标题
- 一个
使用namespace std;
指令,对于小型探索程序非常方便(但不要将其放在头文件的全局名称空间中!)
- 检查输入操作是否成功(输出也可能失败,但这种情况极为罕见)
使用布尔“or”(即| |
运算符)检查输入操作失败的方式在C++中,它还没有被广泛使用,但在一些其他语言中是常见的。本质上,“代码> > < <代码>的左参数被转换成<代码>布尔-< /代码>,因为这就是 < < /Cuth>所需要的。左手参数是一些输入操作的表达式结果,一般是引用<代码> CIN < /C>流,AN,ANd然后通过定义的转换生成bool
值,该转换相当于写入!cin.fail()
(其中!
是逻辑“非”操作)
例如,getline(cin,first)| | fail(…)
读起来非常好,就像“getline
或者fail
”,除了读起来很好之外,它还具有视觉上的独特性,很容易识别为故障检查
#include <iostream>
#include <string>
#include <stdlib.h> // exit, EXIT_FAILURE
using namespace std;
// Poor man's way to handle failure, but good enough here:
bool fail( string const& message )
{
cerr << "!" << message << endl;
exit( EXIT_FAILURE );
}
int main()
{
cout << "Please enter your first name: ";
string first;
getline( cin, first )
|| fail( "Sorry, input of your first name failed." );
cout << "Please enter your last name: ";
string last;
getline( cin, first )
|| fail( "Sorry, input of your last name failed." );
cout << "Please enter your age in years: ";
double age;
cin >> age
|| fail( "Sorry, input of your age failed." );
cout << "Hello, " << first << " " << last << "." << endl;
cout
<< "Your age is " << age << " years"
<< " and you are "<< (age*12) << " months old."
<< endl;
}
#包括
#包括
#包括//退出,退出\失败
使用名称空间std;
//穷人处理失败的方式,但在这里已经足够好了:
布尔失败(字符串常量和消息)
{
Celr < P>我相信下面的代码对于学习如何完成这类几乎第一个C++程序是非常理想的。
代码的一个小技术问题是,double
是关键字,而不是标准库定义的名称,因此不在名称空间std
中
此外,我还添加了
- 包含以便携方式使用
std::string
所需的
标题
- 一个
使用namespace std;
指令,对于小型探索程序非常方便(但不要将其放在头文件的全局名称空间中!)
- 检查输入操作是否成功(输出也可能失败,但这种情况极为罕见)
使用布尔“or”(即| |
运算符)检查输入操作失败的方式在C++中,它还没有被广泛使用,但在一些其他语言中是常见的。本质上,“代码> > < <代码>的左参数被转换成<代码>布尔-< /代码>,因为这就是 < < /Cuth>所需要的。左手参数是一些输入操作的表达式结果,一般是引用<代码> CIN < /C>流,AN,ANd然后通过定义的转换生成bool
值,该转换相当于写入!cin.fail()
(其中!
是逻辑“非”操作)
例如,getline(cin,first)| | fail(…)
读起来非常好,就像“getline
或者fail
”,除了读起来很好之外,它还具有视觉上的独特性,很容易识别为故障检查
#include <iostream>
#include <string>
#include <stdlib.h> // exit, EXIT_FAILURE
using namespace std;
// Poor man's way to handle failure, but good enough here:
bool fail( string const& message )
{
cerr << "!" << message << endl;
exit( EXIT_FAILURE );
}
int main()
{
cout << "Please enter your first name: ";
string first;
getline( cin, first )
|| fail( "Sorry, input of your first name failed." );
cout << "Please enter your last name: ";
string last;
getline( cin, first )
|| fail( "Sorry, input of your last name failed." );
cout << "Please enter your age in years: ";
double age;
cin >> age
|| fail( "Sorry, input of your age failed." );
cout << "Hello, " << first << " " << last << "." << endl;
cout
<< "Your age is " << age << " years"
<< " and you are "<< (age*12) << " months old."
<< endl;
}
#包括
#包括
#包括//退出,退出\失败
使用名称空间std;
//穷人处理失败的方式,